You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by sl...@apache.org on 2026/10/05 16:56:06 UTC

(maven-invoker-plugin) 03/03: Mock MavenSession in the bytecode tests and add an IT for the level

This is an automated email from the ASF dual-hosted git repository.

slachiewicz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-invoker-plugin.git

commit e55f42e59b2a62a618f2ea2ea6cb2225f186b126
Author: Sylwester Lachiewicz <sl...@apache.org>
AuthorDate: Sat Oct 3 20:49:42 2026 +0200

    Mock MavenSession in the bytecode tests and add an IT for the level
---
 src/it/script-target-bytecode/pom.xml              | 65 ++++++++++++++++++++++
 .../script-target-bytecode/src/it/project/pom.xml  | 32 +++++++++++
 .../src/it/project/verify.groovy                   | 26 +++++++++
 src/it/script-target-bytecode/verify.groovy        | 26 +++++++++
 .../maven/plugins/invoker/InvokerMojoTest.java     | 45 ++++++++++-----
 5 files changed, 180 insertions(+), 14 deletions(-)

diff --git a/src/it/script-target-bytecode/pom.xml b/src/it/script-target-bytecode/pom.xml
new file mode 100644
index 0000000..fcf9c50
--- /dev/null
+++ b/src/it/script-target-bytecode/pom.xml
@@ -0,0 +1,65 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+  http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied.  See the License for the
+specific language governing permissions and limitations
+under the License.
+-->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+
+  <groupId>org.apache.maven.plugins.invoker</groupId>
+  <artifactId>script-target-bytecode</artifactId>
+  <version>1.0-SNAPSHOT</version>
+  <packaging>pom</packaging>
+
+  <description>
+    Test that the hook scripts are compiled to the maven.compiler.release level, not to the level of the running JDK.
+  </description>
+
+  <properties>
+    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+    <maven.compiler.release>8</maven.compiler.release>
+  </properties>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-invoker-plugin</artifactId>
+        <version>@project.version@</version>
+        <configuration>
+          <cloneProjectsTo>${project.build.directory}/its</cloneProjectsTo>
+          <postBuildHookScript>verify</postBuildHookScript>
+          <goals>
+            <goal>validate</goal>
+          </goals>
+        </configuration>
+        <executions>
+          <execution>
+            <id>integration-test</id>
+            <phase>initialize</phase>
+            <goals>
+              <goal>run</goal>
+            </goals>
+          </execution>
+        </executions>
+      </plugin>
+    </plugins>
+  </build>
+
+</project>
diff --git a/src/it/script-target-bytecode/src/it/project/pom.xml b/src/it/script-target-bytecode/src/it/project/pom.xml
new file mode 100644
index 0000000..dfa58ed
--- /dev/null
+++ b/src/it/script-target-bytecode/src/it/project/pom.xml
@@ -0,0 +1,32 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+  http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied.  See the License for the
+specific language governing permissions and limitations
+under the License.
+-->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+  <groupId>test</groupId>
+  <artifactId>project</artifactId>
+  <version>0.1-SNAPSHOT</version>
+  <packaging>pom</packaging>
+
+  <properties>
+    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+  </properties>
+</project>
diff --git a/src/it/script-target-bytecode/src/it/project/verify.groovy b/src/it/script-target-bytecode/src/it/project/verify.groovy
new file mode 100644
index 0000000..429d5f7
--- /dev/null
+++ b/src/it/script-target-bytecode/src/it/project/verify.groovy
@@ -0,0 +1,26 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+// Groovy emits a native java.lang.Record only when the target bytecode is 16 or later, and an emulated record
+// extending java.lang.Object below that, so the superclass shows the level this script was compiled to
+record Probe(int value) {}
+
+new File( basedir, "record-superclass.txt" ).text = Probe.superclass.name
+
+return true
diff --git a/src/it/script-target-bytecode/verify.groovy b/src/it/script-target-bytecode/verify.groovy
new file mode 100644
index 0000000..e2b4961
--- /dev/null
+++ b/src/it/script-target-bytecode/verify.groovy
@@ -0,0 +1,26 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+// the outer project sets maven.compiler.release=8, so the hook script must not have been compiled to a level with
+// native records, whatever JDK runs this build
+File result = new File( basedir, "target/its/project/record-superclass.txt" )
+assert result.exists() : "the post-build hook script did not run"
+assert result.text == "java.lang.Object"
+
+return true
diff --git a/src/test/java/org/apache/maven/plugins/invoker/InvokerMojoTest.java b/src/test/java/org/apache/maven/plugins/invoker/InvokerMojoTest.java
index 1130f6c..935ccdd 100644
--- a/src/test/java/org/apache/maven/plugins/invoker/InvokerMojoTest.java
+++ b/src/test/java/org/apache/maven/plugins/invoker/InvokerMojoTest.java
@@ -25,14 +25,11 @@ import java.util.List;
 import java.util.Map;
 import java.util.Properties;
 
-import org.apache.maven.execution.DefaultMavenExecutionRequest;
-import org.apache.maven.execution.MavenExecutionRequest;
 import org.apache.maven.execution.MavenSession;
 import org.apache.maven.plugins.invoker.model.BuildJob;
 import org.apache.maven.project.MavenProject;
 import org.apache.maven.settings.Settings;
 import org.codehaus.plexus.util.ReflectionUtils;
-import org.eclipse.aether.RepositorySystemSession;
 import org.junit.jupiter.api.Test;
 
 import static org.apache.maven.plugins.invoker.TestUtil.getBasedir;
@@ -224,8 +221,13 @@ class InvokerMojoTest {
     }
 
     private MavenSession sessionWithUserProperties(Properties userProperties) {
+        return sessionWithProperties(userProperties, new Properties());
+    }
+
+    private MavenSession sessionWithProperties(Properties userProperties, Properties systemProperties) {
         MavenSession session = mock(MavenSession.class);
         when(session.getUserProperties()).thenReturn(userProperties);
+        when(session.getSystemProperties()).thenReturn(systemProperties);
         return session;
     }
 
@@ -236,7 +238,7 @@ class InvokerMojoTest {
         mavenProject.getProperties().setProperty("maven.compiler.release", "17");
         mavenProject.getProperties().setProperty("maven.compiler.target", "11");
         setVariableValueToObject(invokerMojo, "project", mavenProject);
-        setVariableValueToObject(invokerMojo, "session", newSession(new DefaultMavenExecutionRequest()));
+        setVariableValueToObject(invokerMojo, "session", sessionWithUserProperties(new Properties()));
         setVariableValueToObject(invokerMojo, "scriptTargetBytecode", "8");
 
         // when
@@ -253,7 +255,7 @@ class InvokerMojoTest {
         mavenProject.getProperties().setProperty("maven.compiler.release", "17");
         mavenProject.getProperties().setProperty("maven.compiler.target", "11");
         setVariableValueToObject(invokerMojo, "project", mavenProject);
-        setVariableValueToObject(invokerMojo, "session", newSession(new DefaultMavenExecutionRequest()));
+        setVariableValueToObject(invokerMojo, "session", sessionWithUserProperties(new Properties()));
 
         // when
         String targetBytecode = invokeHandleScriptRunnerAndGetGroovyTargetBytecode();
@@ -268,7 +270,7 @@ class InvokerMojoTest {
         MavenProject mavenProject = getMavenProject();
         mavenProject.getProperties().setProperty("maven.compiler.target", "11");
         setVariableValueToObject(invokerMojo, "project", mavenProject);
-        setVariableValueToObject(invokerMojo, "session", newSession(new DefaultMavenExecutionRequest()));
+        setVariableValueToObject(invokerMojo, "session", sessionWithUserProperties(new Properties()));
 
         // when
         String targetBytecode = invokeHandleScriptRunnerAndGetGroovyTargetBytecode();
@@ -282,10 +284,12 @@ class InvokerMojoTest {
         // given
         MavenProject mavenProject = getMavenProject();
         mavenProject.getProperties().setProperty("maven.compiler.release", "17");
-        MavenExecutionRequest request = new DefaultMavenExecutionRequest();
-        request.getUserProperties().setProperty("maven.compiler.release", "21");
+        Properties userProperties = new Properties();
+        userProperties.setProperty("maven.compiler.release", "21");
+        Properties systemProperties = new Properties();
+        systemProperties.setProperty("maven.compiler.release", "17");
         setVariableValueToObject(invokerMojo, "project", mavenProject);
-        setVariableValueToObject(invokerMojo, "session", newSession(request));
+        setVariableValueToObject(invokerMojo, "session", sessionWithProperties(userProperties, systemProperties));
 
         // when
         String targetBytecode = invokeHandleScriptRunnerAndGetGroovyTargetBytecode();
@@ -295,21 +299,34 @@ class InvokerMojoTest {
     }
 
     @Test
-    void scriptTargetBytecodeIsUnsetWhenNeitherParameterNorPropertyIsPresent() throws Exception {
+    void scriptTargetBytecodeSystemPropertyWinsOverProjectProperty() throws Exception {
         // given
         MavenProject mavenProject = getMavenProject();
+        mavenProject.getProperties().setProperty("maven.compiler.release", "11");
+        Properties systemProperties = new Properties();
+        systemProperties.setProperty("maven.compiler.release", "17");
         setVariableValueToObject(invokerMojo, "project", mavenProject);
-        setVariableValueToObject(invokerMojo, "session", newSession(new DefaultMavenExecutionRequest()));
+        setVariableValueToObject(invokerMojo, "session", sessionWithProperties(new Properties(), systemProperties));
 
         // when
         String targetBytecode = invokeHandleScriptRunnerAndGetGroovyTargetBytecode();
 
         // then
-        assertThat(targetBytecode).isNull();
+        assertThat(targetBytecode).isEqualTo("17");
     }
 
-    private static MavenSession newSession(MavenExecutionRequest request) {
-        return new MavenSession(null, (RepositorySystemSession) null, request, null);
+    @Test
+    void scriptTargetBytecodeIsUnsetWhenNeitherParameterNorPropertyIsPresent() throws Exception {
+        // given
+        MavenProject mavenProject = getMavenProject();
+        setVariableValueToObject(invokerMojo, "project", mavenProject);
+        setVariableValueToObject(invokerMojo, "session", sessionWithUserProperties(new Properties()));
+
+        // when
+        String targetBytecode = invokeHandleScriptRunnerAndGetGroovyTargetBytecode();
+
+        // then
+        assertThat(targetBytecode).isNull();
     }
 
     private String invokeHandleScriptRunnerAndGetGroovyTargetBytecode() throws Exception {