From f001261c727ddf43a6db436a7977cd0cf6b26c73 Mon Sep 17 00:00:00 2001 From: Daniel Mensinger Date: Mon, 4 Nov 2024 14:46:22 +0100 Subject: [PATCH 1/2] [MCOMPILER-540] Add test case --- src/it/MCOMPILER-540/code/pom.xml | 87 +++++++++++++++++++ .../src/main/java/bar/MyAnnotatedClass.java | 29 +++++++ src/it/MCOMPILER-540/invoker.properties | 23 +++++ src/it/MCOMPILER-540/pom.xml | 70 +++++++++++++++ src/it/MCOMPILER-540/processor/pom.xml | 43 +++++++++ .../src/main/java/foo/MyAnnotation.java | 28 ++++++ .../src/main/java/foo/MyProcessor.java | 67 ++++++++++++++ .../javax.annotation.processing.Processor | 18 ++++ 8 files changed, 365 insertions(+) create mode 100644 src/it/MCOMPILER-540/code/pom.xml create mode 100644 src/it/MCOMPILER-540/code/src/main/java/bar/MyAnnotatedClass.java create mode 100644 src/it/MCOMPILER-540/invoker.properties create mode 100644 src/it/MCOMPILER-540/pom.xml create mode 100644 src/it/MCOMPILER-540/processor/pom.xml create mode 100644 src/it/MCOMPILER-540/processor/src/main/java/foo/MyAnnotation.java create mode 100644 src/it/MCOMPILER-540/processor/src/main/java/foo/MyProcessor.java create mode 100644 src/it/MCOMPILER-540/processor/src/main/resources/META-INF/services/javax.annotation.processing.Processor diff --git a/src/it/MCOMPILER-540/code/pom.xml b/src/it/MCOMPILER-540/code/pom.xml new file mode 100644 index 00000000..e0847e01 --- /dev/null +++ b/src/it/MCOMPILER-540/code/pom.xml @@ -0,0 +1,87 @@ + + + + 4.0.0 + + foo + bar + 1.0-SNAPSHOT + + 1.0-SNAPSHOT + jar + code + + + + + + org.apache.maven.plugins + maven-antrun-plugin + 3.1.0 + + + + + + + + actualBuild + + + foo + processor + 1.0-SNAPSHOT + provided + + + + + + touchFiles + + false + + + + + org.apache.maven.plugins + maven-antrun-plugin + + + touch-gen-src + + run + + validate + + + + + + + + + + + + + + diff --git a/src/it/MCOMPILER-540/code/src/main/java/bar/MyAnnotatedClass.java b/src/it/MCOMPILER-540/code/src/main/java/bar/MyAnnotatedClass.java new file mode 100644 index 00000000..cc4126d8 --- /dev/null +++ b/src/it/MCOMPILER-540/code/src/main/java/bar/MyAnnotatedClass.java @@ -0,0 +1,29 @@ +/* + * 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. + */ +package bar; + +import foo.MyAnnotation; + +@MyAnnotation +public class MyAnnotatedClass { + MyGeneratedClass generatedClass = new MyGeneratedClass(); + + // foo + +} diff --git a/src/it/MCOMPILER-540/invoker.properties b/src/it/MCOMPILER-540/invoker.properties new file mode 100644 index 00000000..bf1b5941 --- /dev/null +++ b/src/it/MCOMPILER-540/invoker.properties @@ -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. + +# NOTE: The first time, we run up to "integration-test" phase which includes the AntRun execution which saves the +# timestamp of the first JAR for comparison with the timestamp of the JAR from the final "package" invocation. +# Note: +invoker.goals.1 = clean compile -PactualBuild +invoker.goals.2 = validate -PtouchFiles +invoker.goals.3 = compile -PactualBuild diff --git a/src/it/MCOMPILER-540/pom.xml b/src/it/MCOMPILER-540/pom.xml new file mode 100644 index 00000000..3f3e5777 --- /dev/null +++ b/src/it/MCOMPILER-540/pom.xml @@ -0,0 +1,70 @@ + + + + 4.0.0 + foo + bar + 1.0-SNAPSHOT + pom + + + UTF-8 + + + + processor + code + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + @project.version@ + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + UTF-8 + true + true + true + false + + -Xlint:all,-processing + -Xdoclint:all,-missing,-accessibility + -Xmaxerrs + 2147483647 + -Xmaxwarns + 2147483647 + + + + + + diff --git a/src/it/MCOMPILER-540/processor/pom.xml b/src/it/MCOMPILER-540/processor/pom.xml new file mode 100644 index 00000000..8c49c7da --- /dev/null +++ b/src/it/MCOMPILER-540/processor/pom.xml @@ -0,0 +1,43 @@ + + + + 4.0.0 + + foo + bar + 1.0-SNAPSHOT + + 1.0-SNAPSHOT + processor + jar + + + + + org.apache.maven.plugins + maven-compiler-plugin + + none + + + + + diff --git a/src/it/MCOMPILER-540/processor/src/main/java/foo/MyAnnotation.java b/src/it/MCOMPILER-540/processor/src/main/java/foo/MyAnnotation.java new file mode 100644 index 00000000..88431aef --- /dev/null +++ b/src/it/MCOMPILER-540/processor/src/main/java/foo/MyAnnotation.java @@ -0,0 +1,28 @@ +/* + * 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. + */ +package foo; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.SOURCE) +@Target(ElementType.TYPE) +public @interface MyAnnotation {} diff --git a/src/it/MCOMPILER-540/processor/src/main/java/foo/MyProcessor.java b/src/it/MCOMPILER-540/processor/src/main/java/foo/MyProcessor.java new file mode 100644 index 00000000..88db3576 --- /dev/null +++ b/src/it/MCOMPILER-540/processor/src/main/java/foo/MyProcessor.java @@ -0,0 +1,67 @@ +/* + * 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. + */ +package foo; + +import javax.annotation.processing.*; +import javax.lang.model.SourceVersion; +import javax.lang.model.element.TypeElement; +import javax.tools.Diagnostic; +import javax.tools.JavaFileObject; + +import java.io.IOException; +import java.io.Writer; +import java.util.Set; + +@SupportedAnnotationTypes("foo.MyAnnotation") +public class MyProcessor extends AbstractProcessor { + + private int procRun = 0; + + @Override + public SourceVersion getSupportedSourceVersion() { + return SourceVersion.latestSupported(); + } + + @Override + public boolean process(Set annotations, RoundEnvironment roundEnv) { + try { + generate(annotations, roundEnv); + } catch (IOException e) { + throw new RuntimeException(e); + } + + return false; + } + + private void generate(Set annotations, RoundEnvironment roundEnv) throws IOException { + Messager msg = processingEnv.getMessager(); + msg.printMessage(Diagnostic.Kind.NOTE, String.format("[%d]: Hello Info", procRun)); + msg.printMessage(Diagnostic.Kind.WARNING, String.format("[%d]: Hello Warning", procRun)); + procRun++; + + if (procRun > 1) { + return; + } + + JavaFileObject file = processingEnv.getFiler().createSourceFile("bar.MyGeneratedClass"); + try (Writer writer = file.openWriter()) { + writer.write("package bar;\npublic class MyGeneratedClass {}"); + } + } +} diff --git a/src/it/MCOMPILER-540/processor/src/main/resources/META-INF/services/javax.annotation.processing.Processor b/src/it/MCOMPILER-540/processor/src/main/resources/META-INF/services/javax.annotation.processing.Processor new file mode 100644 index 00000000..678af7ba --- /dev/null +++ b/src/it/MCOMPILER-540/processor/src/main/resources/META-INF/services/javax.annotation.processing.Processor @@ -0,0 +1,18 @@ +# 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. + +foo.MyProcessor From d6262c36bc979a229ef77f19abcc62fded66937b Mon Sep 17 00:00:00 2001 From: Daniel Mensinger Date: Fri, 16 Jun 2023 14:32:32 +0200 Subject: [PATCH 2/2] [MCOMPILER-540] - useIncrementalCompilation=false may add generated sources to the sources list --- .../maven/plugin/compiler/AbstractCompilerMojo.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) 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 56318925..6546de96 100644 --- a/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java +++ b/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java @@ -969,6 +969,7 @@ public void execute() throws MojoExecutionException, CompilationFailureException return; } + staleSources = filterSourceFiles(staleSources); compilerConfiguration.setSourceFiles(staleSources); try { @@ -1854,6 +1855,17 @@ public void setRelease(String release) { targetOrReleaseSet = true; } + private Set filterSourceFiles(Set sourceFiles) { + final File generatedSources = getGeneratedSourcesDirectory(); + if (generatedSources == null) { + return sourceFiles; + } + final String generatedSourcesPath = generatedSources.getAbsolutePath(); + return sourceFiles.stream() + .filter(x -> !x.getAbsolutePath().startsWith(generatedSourcesPath)) + .collect(Collectors.toSet()); + } + final String getImplicit() { return implicit; }