You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by ol...@apache.org on 2022/02/07 02:04:31 UTC

[maven-compiler-plugin] branch master updated: [MCOMPILER-205] Add a boolean to generate missing package-info classes by default (#88)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 40dd50f  [MCOMPILER-205] Add a boolean to generate missing package-info classes by default (#88)
40dd50f is described below

commit 40dd50fd81dc0c7832c90e0744e84d85a384517b
Author: Guillaume Nodet <gn...@gmail.com>
AuthorDate: Mon Feb 7 03:04:23 2022 +0100

    [MCOMPILER-205] Add a boolean to generate missing package-info classes by default (#88)
    
    * [MCOMPILER-205] Add a boolean to generate missing package-info classes by default
    * [MCOMPILER-205] Add an integration test
---
 src/it/MCOMPILER-205/invoker.properties            | 19 +++++
 src/it/MCOMPILER-205/pom.xml                       | 44 ++++++++++
 .../src/main/java/dummy/HelloWorld.java            | 28 +++++++
 .../src/main/java/dummy/package-info.java          | 23 ++++++
 src/it/MCOMPILER-205/verify.groovy                 | 21 +++++
 .../plugin/compiler/AbstractCompilerMojo.java      | 95 ++++++++++++++++++++++
 6 files changed, 230 insertions(+)

diff --git a/src/it/MCOMPILER-205/invoker.properties b/src/it/MCOMPILER-205/invoker.properties
new file mode 100644
index 0000000..8595721
--- /dev/null
+++ b/src/it/MCOMPILER-205/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.goals = clean compile
+invoker.buildResult = success
diff --git a/src/it/MCOMPILER-205/pom.xml b/src/it/MCOMPILER-205/pom.xml
new file mode 100644
index 0000000..2f291f7
--- /dev/null
+++ b/src/it/MCOMPILER-205/pom.xml
@@ -0,0 +1,44 @@
+<!--
+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/xsd/maven-4.0.0.xsd">
+
+  <modelVersion>4.0.0</modelVersion>
+
+  <groupId>blah</groupId>
+  <artifactId>blah</artifactId>
+  <version>1.0</version>
+  <packaging>jar</packaging>
+
+  <properties>
+    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+  </properties>
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-compiler-plugin</artifactId>
+        <version>@pom.version@</version>
+      </plugin>
+    </plugins>
+  </build>
+</project>
diff --git a/src/it/MCOMPILER-205/src/main/java/dummy/HelloWorld.java b/src/it/MCOMPILER-205/src/main/java/dummy/HelloWorld.java
new file mode 100644
index 0000000..f223c74
--- /dev/null
+++ b/src/it/MCOMPILER-205/src/main/java/dummy/HelloWorld.java
@@ -0,0 +1,28 @@
+package dummy;
+
+/*
+ * 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 HelloWorld
+{
+   public static void main(String[] argv) {
+      System.out.println("Hello World");
+   }
+}
diff --git a/src/it/MCOMPILER-205/src/main/java/dummy/package-info.java b/src/it/MCOMPILER-205/src/main/java/dummy/package-info.java
new file mode 100644
index 0000000..ed72542
--- /dev/null
+++ b/src/it/MCOMPILER-205/src/main/java/dummy/package-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.
+ */
+
+/**
+ * This is the package javadoc
+ */
+package dummy;
\ No newline at end of file
diff --git a/src/it/MCOMPILER-205/verify.groovy b/src/it/MCOMPILER-205/verify.groovy
new file mode 100644
index 0000000..1526f86
--- /dev/null
+++ b/src/it/MCOMPILER-205/verify.groovy
@@ -0,0 +1,21 @@
+
+/*
+ * 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.
+ */
+def packageInfoClassFile = new File( basedir, 'target/classes/dummy/package-info.class' )
+assert packageInfoClassFile.exists()
diff --git a/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java b/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java
index 20d92a0..7988a22 100644
--- a/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java
+++ b/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java
@@ -82,6 +82,8 @@ import org.codehaus.plexus.compiler.util.scan.mapping.SourceMapping;
 import org.codehaus.plexus.compiler.util.scan.mapping.SuffixMapping;
 import org.codehaus.plexus.languages.java.jpms.JavaModuleDescriptor;
 import org.codehaus.plexus.languages.java.version.JavaVersion;
+import org.objectweb.asm.ClassWriter;
+import org.objectweb.asm.Opcodes;
 
 /**
  * TODO: At least one step could be optimized, currently the plugin will do two
@@ -531,6 +533,18 @@ public abstract class AbstractCompilerMojo
     private boolean useIncrementalCompilation = true;
 
     /**
+     * Package info source files that only contain javadoc and no annotation on the package
+     * can lead to no class file being generated by the compiler.  This causes a file miss
+     * on the next compilations and forces an unnecessary recompilation. The default value
+     * of <code>true</code> causes an empty class file to be generated.  This behavior can
+     * be changed by setting this parameter to <code>false</code>.
+     *
+     * @since 3.10
+     */
+    @Parameter( defaultValue = "true", property = "maven.compiler.createMissingPackageInfoClass" )
+    private boolean createMissingPackageInfoClass = true;
+
+    /**
      * Resolves the artifacts needed.
      */
     @Component
@@ -1192,6 +1206,21 @@ public abstract class AbstractCompilerMojo
             throw new MojoExecutionException( "Fatal error compiling", e );
         }
 
+        if ( createMissingPackageInfoClass && compilerResult.isSuccess()
+                && compiler.getCompilerOutputStyle() == CompilerOutputStyle.ONE_OUTPUT_FILE_PER_INPUT_FILE )
+        {
+            try
+            {
+                SourceMapping sourceMapping = getSourceMapping( compilerConfiguration, compiler );
+                createMissingPackageInfoClasses( compilerConfiguration, sourceMapping, sources );
+            }
+            catch ( Exception e )
+            {
+                getLog().warn( "Error creating missing package info classes", e );
+
+            }
+        }
+
         if ( useIncrementalCompilation )
         {
             if ( incrementalBuildHelperRequest.getOutputDirectory().exists() )
@@ -1296,6 +1325,72 @@ public abstract class AbstractCompilerMojo
         }
     }
 
+    private void createMissingPackageInfoClasses( CompilerConfiguration compilerConfiguration,
+                                                  SourceMapping sourceMapping,
+                                                  Set<File> sources )
+            throws InclusionScanException, IOException
+    {
+        for ( File source : sources )
+        {
+            String path = source.toString();
+            if ( path.endsWith( File.separator + "package-info.java" ) )
+            {
+                for ( String root : getCompileSourceRoots() )
+                {
+                    root = root + File.separator;
+                    if ( path.startsWith( root ) )
+                    {
+                        String rel = path.substring( root.length() );
+                        Set<File> files = sourceMapping.getTargetFiles( getOutputDirectory(), rel );
+                        for ( File file : files )
+                        {
+                            if ( !file.exists() )
+                            {
+                                byte[] bytes = generatePackage( compilerConfiguration, rel );
+                                Files.write( file.toPath(), bytes );
+                            }
+                        }
+                    }
+                }
+            }
+        }
+    }
+
+    private byte[] generatePackage( CompilerConfiguration compilerConfiguration, String javaFile )
+    {
+        int version = getOpcode( compilerConfiguration );
+        ClassWriter cw = new ClassWriter( 0 );
+        cw.visitSource( "package-info.java", null );
+        cw.visit( version,
+                Opcodes.ACC_SYNTHETIC | Opcodes.ACC_ABSTRACT | Opcodes.ACC_INTERFACE,
+                javaFile.substring( 0, javaFile.length() - ".java".length() ),
+                null, "java/lang/Object", null );
+        return cw.toByteArray();
+    }
+
+    private int getOpcode( CompilerConfiguration compilerConfiguration )
+    {
+        String version = compilerConfiguration.getReleaseVersion();
+        if ( version == null )
+        {
+            version = compilerConfiguration.getTargetVersion();
+            if ( version == null )
+            {
+                version = "1.5";
+            }
+        }
+        if ( version.startsWith( "1." ) )
+        {
+            version = version.substring( 2 );
+        }
+        int iVersion = Integer.parseInt( version );
+        if ( iVersion < 2 )
+        {
+            throw new IllegalArgumentException( "Unsupported java version '" + version + "'" );
+        }
+        return iVersion - 2 + Opcodes.V1_2;
+    }
+
     protected boolean isTestCompile()
     {
         return false;