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 2021/07/04 07:45:45 UTC
[maven-compiler-plugin] 01/01: [MCOMPILER-447] Unable to compile
modularized test for a multi-version jar that is not a java module by
default
This is an automated email from the ASF dual-hosted git repository.
slachiewicz pushed a commit to branch MCOMPILER-447
in repository https://gitbox.apache.org/repos/asf/maven-compiler-plugin.git
commit ca2bddd110c50b8329a3ca2d095226a9aa8070ef
Author: Claudio Corsi <cl...@yahoo.com>
AuthorDate: Wed Dec 16 20:02:58 2020 -0500
[MCOMPILER-447] Unable to compile modularized test for a multi-version jar that is not a java module by default
The following changes resolves two different issues. The first is the case that
the we are creating a multi-version jar file that by default is not modularized.
If tests are created that will be testing the modularized part of the multi-version
jar file. Then the build was generating an UnsupportedOperationException because
it assumed that we were using a non-modularized jar file with a modularized test.
The second issue was related to the fact that the test are using dependency jars
that are not modularized. The generated javac command was only producing the
required module path and nulled out the classpath. This was causing the building
of the test sources to fail. The was caused because those non-modularized jar
files were not part of the classpath.
The current set of changes solves the first issue by determining if the project
jar file is a multi-version jar file. If it is, it will then set the main module
descriptor to the latest version less than or equal to the defined release
attribute.
It solves the second issue by adding all non-modularized jar files to the
class path. It does not remove them from the defined module path to be
consistent with how it was working.
Finally an integration test was created that reproduced the above issues.
---
src/it/MCOMPILER-447/invoker.properties | 19 ++++
src/it/MCOMPILER-447/pom.xml | 113 +++++++++++++++++++++
.../MCOMPILER-447/src/main/java/org/foo/Main.java | 26 +++++
.../MCOMPILER-447/src/main/java9/module-info.java | 23 +++++
.../MCOMPILER-447/src/main/java9/org/foo/Main.java | 26 +++++
.../MCOMPILER-447/src/test/test7/org/bar/Test.java | 30 ++++++
.../MCOMPILER-447/src/test/test9/module-info.java | 25 +++++
.../MCOMPILER-447/src/test/test9/org/bar/Test.java | 32 ++++++
.../maven/plugin/compiler/TestCompilerMojo.java | 78 +++++++++++++-
9 files changed, 371 insertions(+), 1 deletion(-)
diff --git a/src/it/MCOMPILER-447/invoker.properties b/src/it/MCOMPILER-447/invoker.properties
new file mode 100644
index 0000000..a9082ff
--- /dev/null
+++ b/src/it/MCOMPILER-447/invoker.properties
@@ -0,0 +1,19 @@
+# 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.
+
+invoker.java.version = 10+
+invoker.goals = test-compile
diff --git a/src/it/MCOMPILER-447/pom.xml b/src/it/MCOMPILER-447/pom.xml
new file mode 100644
index 0000000..9f63218
--- /dev/null
+++ b/src/it/MCOMPILER-447/pom.xml
@@ -0,0 +1,113 @@
+<?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.compiler.it</groupId>
+ <artifactId>mcompiler447</artifactId>
+ <version>1.0-SNAPSHOT</version>
+
+ <url>https://issues.apache.org/jira/browse/MCOMPILER-447</url>
+
+ <properties>
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+ </properties>
+
+ <dependencies>
+ <!-- referencing a non-module jar file in the test source -->
+ <dependency>
+ <groupId>log4j</groupId>
+ <artifactId>log4j</artifactId>
+ <version>1.2.17</version>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <version>@project.version@</version>
+ <configuration>
+ <release>7</release>
+ </configuration>
+ <executions>
+ <execution>
+ <id>compile-java-9</id>
+ <goals>
+ <goal>compile</goal>
+ </goals>
+ <configuration>
+ <release>9</release>
+ <compileSourceRoots>
+ <compilerSourceRoot>${project.basedir}/src/main/java9</compilerSourceRoot>
+ </compileSourceRoots>
+ <multiReleaseOutput>true</multiReleaseOutput>
+ </configuration>
+ </execution>
+ <execution>
+ <id>test-compile-java-7</id>
+ <goals>
+ <goal>testCompile</goal>
+ </goals>
+ <configuration>
+ <release>7</release>
+ <compileSourceRoots>
+ <compilerSourceRoot>${project.basedir}/src/test/test7</compilerSourceRoot>
+ </compileSourceRoots>
+ <outputDirectory>${project.basedir}/target/test7</outputDirectory>
+ </configuration>
+ </execution>
+ <execution>
+ <id>test-compile-java-9</id>
+ <goals>
+ <goal>testCompile</goal>
+ </goals>
+ <configuration>
+ <release>9</release>
+ <compileSourceRoots>
+ <compilerSourceRoot>${project.basedir}/src/test/test9</compilerSourceRoot>
+ </compileSourceRoots>
+ <outputDirectory>${project.basedir}/target/test9</outputDirectory>
+ </configuration>
+ </execution>
+ <execution>
+ <id>test-compile-java-10</id>
+ <goals>
+ <goal>testCompile</goal>
+ </goals>
+ <configuration>
+ <release>10</release>
+ <compileSourceRoots>
+ <compilerSourceRoot>${project.basedir}/src/test/test9</compilerSourceRoot>
+ </compileSourceRoots>
+ <outputDirectory>${project.basedir}/target/test10</outputDirectory>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+
+</project>
diff --git a/src/it/MCOMPILER-447/src/main/java/org/foo/Main.java b/src/it/MCOMPILER-447/src/main/java/org/foo/Main.java
new file mode 100644
index 0000000..c1bd94f
--- /dev/null
+++ b/src/it/MCOMPILER-447/src/main/java/org/foo/Main.java
@@ -0,0 +1,26 @@
+package org.foo;
+/*
+ * 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.
+ */
+
+
+public class Main {
+ public static void main(String[] args) {
+ System.out.println("Hello, world! Java 9 style!");
+ }
+}
diff --git a/src/it/MCOMPILER-447/src/main/java9/module-info.java b/src/it/MCOMPILER-447/src/main/java9/module-info.java
new file mode 100644
index 0000000..a5132bd
--- /dev/null
+++ b/src/it/MCOMPILER-447/src/main/java9/module-info.java
@@ -0,0 +1,23 @@
+/*
+ * 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.
+ */
+
+module org.foo {
+ requires java.base;
+ exports org.foo;
+}
diff --git a/src/it/MCOMPILER-447/src/main/java9/org/foo/Main.java b/src/it/MCOMPILER-447/src/main/java9/org/foo/Main.java
new file mode 100644
index 0000000..ae9d8db
--- /dev/null
+++ b/src/it/MCOMPILER-447/src/main/java9/org/foo/Main.java
@@ -0,0 +1,26 @@
+package org.foo;
+/*
+ * 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.
+ */
+
+
+public class Main {
+ public static void main(String[] args) {
+ System.out.println("Hello, world!");
+ }
+}
diff --git a/src/it/MCOMPILER-447/src/test/test7/org/bar/Test.java b/src/it/MCOMPILER-447/src/test/test7/org/bar/Test.java
new file mode 100644
index 0000000..8647f49
--- /dev/null
+++ b/src/it/MCOMPILER-447/src/test/test7/org/bar/Test.java
@@ -0,0 +1,30 @@
+package org.bar;
+/*
+ * 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.
+ */
+
+
+import org.foo.Main;
+
+public class Test {
+
+ public void execute() {
+ Main.main(new String[0]);
+ }
+
+}
diff --git a/src/it/MCOMPILER-447/src/test/test9/module-info.java b/src/it/MCOMPILER-447/src/test/test9/module-info.java
new file mode 100644
index 0000000..eb1eac2
--- /dev/null
+++ b/src/it/MCOMPILER-447/src/test/test9/module-info.java
@@ -0,0 +1,25 @@
+/*
+ * 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.
+ */
+
+module org.bar {
+ requires org.foo;
+
+ requires log4j;
+}
+
diff --git a/src/it/MCOMPILER-447/src/test/test9/org/bar/Test.java b/src/it/MCOMPILER-447/src/test/test9/org/bar/Test.java
new file mode 100644
index 0000000..2a63b22
--- /dev/null
+++ b/src/it/MCOMPILER-447/src/test/test9/org/bar/Test.java
@@ -0,0 +1,32 @@
+package org.bar;
+/*
+ * 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.
+ */
+
+
+import org.foo.Main;
+import org.apache.log4j.LogManager;
+
+public class Test {
+
+ public void execute() {
+ Main.main(new String[0]);
+ LogManager.getLogger(Main.class);
+ }
+
+}
diff --git a/src/main/java/org/apache/maven/plugin/compiler/TestCompilerMojo.java b/src/main/java/org/apache/maven/plugin/compiler/TestCompilerMojo.java
index 9430ac2..b23f626 100644
--- a/src/main/java/org/apache/maven/plugin/compiler/TestCompilerMojo.java
+++ b/src/main/java/org/apache/maven/plugin/compiler/TestCompilerMojo.java
@@ -32,6 +32,7 @@ import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
+import java.util.jar.JarFile;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
@@ -224,6 +225,36 @@ public class TestCompilerMojo
}
}
+ // It is possible that we are building a multi-version jar file in which the main classes are not module based.
+ if ( ! mainModuleDescriptorClassFile.exists() && release != null )
+ {
+ int version = Integer.valueOf( release );
+ while ( version >= 9 )
+ {
+ File metaInfoDir = new File( getProject().getBuild().getOutputDirectory(), "META-INF/versions/"
+ + version );
+
+ File file = new File ( metaInfoDir, "module-info.class" );
+
+ if ( file.exists() )
+ {
+ // @todo how to add the releaseOutputDirectory automatically?
+ // add releaseOutputDirectory to the beginning of the test classpath.
+ testPath.add( 0, metaInfoDir.getAbsolutePath() );
+ mainModuleDescriptorClassFile = file;
+ if ( getLog().isDebugEnabled() )
+ {
+ getLog().debug( "Updated main module " + mainModuleDescriptorClassFile );
+ }
+ // break out of the while loop since we are done...
+ break;
+ }
+
+ // decrement the version and keep checking....
+ version--;
+ }
+ }
+
// Get additional information from the main module descriptor, if available
if ( mainModuleDescriptorClassFile.exists() )
{
@@ -316,7 +347,15 @@ public class TestCompilerMojo
if ( testModuleDescriptor != null )
{
modulepathElements = testPath;
- classpathElements = Collections.emptyList();
+ classpathElements = null;
+
+ // add non-modularized jar files from the module path to the class path
+ checkAndAddJarToClassPath();
+
+ if ( classpathElements == null )
+ {
+ classpathElements = Collections.emptyList();
+ }
if ( mainModuleDescriptor != null )
{
@@ -401,6 +440,43 @@ public class TestCompilerMojo
}
}
+ private void checkAndAddJarToClassPath()
+ {
+ /*
+ This method is used to update the classpath for a modularized build with non-modularized jar files.
+ This resolves issues found in MCOMPILER-447.
+ */
+ for ( String path : modulepathElements )
+ {
+ File file = new File( path );
+ if ( file.exists() && ! file.isDirectory() && file.getName().endsWith( ".jar" ) )
+ {
+ // check if the jar file contains the module-info.class
+ try
+ {
+ JarFile jarFile = new JarFile( file );
+ if ( jarFile.getJarEntry( "module-info.class" ) == null )
+ {
+ if ( getLog().isDebugEnabled() )
+ {
+ getLog().debug( "Add non-module jar dependency " + path + " to class path." );
+ }
+ if ( classpathElements == null )
+ {
+ classpathElements = new ArrayList<>();
+ }
+ // add the path to the classpathElements list.
+ classpathElements.add( path );
+ }
+ }
+ catch ( IOException e )
+ {
+ // do nothing
+ }
+ }
+ }
+ }
+
protected SourceInclusionScanner getSourceInclusionScanner( int staleMillis )
{
SourceInclusionScanner scanner;