From 4090001d30a22dc3bb8bc2c545e368f6467a3aad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20=C5=A0irok=C3=BD?= Date: Wed, 15 Feb 2023 20:49:40 +0100 Subject: [PATCH] [MCOMPILER-391] Use dep mgmt when resolving annotation processors and their deps * dependency managemet can be used for two slighty difference use cases (when it comes to annotation processor dependencies): -- getting the version of the top-level processor path elements -- passing the list of managed dependencies to maven-resolver when resolving the whole processorpath These two can be combined, so there is total of 4 combinations (some of the them make more sense than the others, but generally there is no reason to not support all of them) * using dependency management when resolving transitive dependencies of annotation processors is something that may or may not be desired. For that reason, there is a new flag that explicitly enables this behavior, while default is to _not_ use dependency management (the current behavior) --- .../annotation-api/pom.xml | 34 +++++++ .../java/mcompiler391/SimpleAnnotation.java | 35 +++++++ .../annotation-processor-dep-v1/pom.xml | 34 +++++++ .../AnnotationProcessorDependencyV1.java | 21 ++++ .../annotation-processor-dep-v2/pom.xml | 37 +++++++ .../AnnotationProcessorDependencyV2.java | 21 ++++ .../annotation-processor/pom.xml | 47 +++++++++ .../SimpleAnnotationProcessor.java | 89 +++++++++++++++++ .../annotation-user1/pom.xml | 89 +++++++++++++++++ .../java/mcompiler391_1/SimpleObject.java | 24 +++++ .../java/mcompiler391_1/SimpleTestObject.java | 24 +++++ .../annotation-user2/pom.xml | 96 +++++++++++++++++++ .../java/mcompiler391_2/SimpleObject.java | 24 +++++ .../java/mcompiler391_2/SimpleTestObject.java | 24 +++++ .../annotation-user3/pom.xml | 80 ++++++++++++++++ .../java/mcompiler391_3/SimpleObject.java | 24 +++++ .../java/mcompiler391_3/SimpleTestObject.java | 24 +++++ .../annotation-user4/pom.xml | 92 ++++++++++++++++++ .../java/mcompiler391_4/SimpleObject.java | 24 +++++ .../java/mcompiler391_4/SimpleTestObject.java | 24 +++++ .../invoker.properties | 18 ++++ .../pom.xml | 57 +++++++++++ .../plugin/compiler/AbstractCompilerMojo.java | 61 +++++++++++- 23 files changed, 999 insertions(+), 4 deletions(-) create mode 100644 src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-api/pom.xml create mode 100644 src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-api/src/main/java/mcompiler391/SimpleAnnotation.java create mode 100644 src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-processor-dep-v1/pom.xml create mode 100644 src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-processor-dep-v1/src/main/java/mcompiler391/AnnotationProcessorDependencyV1.java create mode 100644 src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-processor-dep-v2/pom.xml create mode 100644 src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-processor-dep-v2/src/main/java/mcompiler391/AnnotationProcessorDependencyV2.java create mode 100644 src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-processor/pom.xml create mode 100644 src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-processor/src/main/java/mcompiler391/SimpleAnnotationProcessor.java create mode 100644 src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user1/pom.xml create mode 100644 src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user1/src/main/java/mcompiler391_1/SimpleObject.java create mode 100644 src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user1/src/test/java/mcompiler391_1/SimpleTestObject.java create mode 100644 src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user2/pom.xml create mode 100644 src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user2/src/main/java/mcompiler391_2/SimpleObject.java create mode 100644 src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user2/src/test/java/mcompiler391_2/SimpleTestObject.java create mode 100644 src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user3/pom.xml create mode 100644 src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user3/src/main/java/mcompiler391_3/SimpleObject.java create mode 100644 src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user3/src/test/java/mcompiler391_3/SimpleTestObject.java create mode 100644 src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user4/pom.xml create mode 100644 src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user4/src/main/java/mcompiler391_4/SimpleObject.java create mode 100644 src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user4/src/test/java/mcompiler391_4/SimpleTestObject.java create mode 100644 src/it/MCOMPILER-391-processorpath-dep-mgmt/invoker.properties create mode 100644 src/it/MCOMPILER-391-processorpath-dep-mgmt/pom.xml diff --git a/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-api/pom.xml b/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-api/pom.xml new file mode 100644 index 00000000..0a0cb9e9 --- /dev/null +++ b/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-api/pom.xml @@ -0,0 +1,34 @@ + + + + + 4.0.0 + + + org.apache.maven.plugins.compiler.it + mcompiler391-test + 1.0.0-SNAPSHOT + + + mcompiler391-annotation-api + + diff --git a/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-api/src/main/java/mcompiler391/SimpleAnnotation.java b/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-api/src/main/java/mcompiler391/SimpleAnnotation.java new file mode 100644 index 00000000..7c98cfd2 --- /dev/null +++ b/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-api/src/main/java/mcompiler391/SimpleAnnotation.java @@ -0,0 +1,35 @@ +/* + * 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 mcompiler391; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.SOURCE) +public @interface SimpleAnnotation { + /** + * Specifies which classes (FQCNs) are supposed to be on classpath during annotation processing. + * Used to check if the dependency resolution mechanism of maven-compiler-plugin is constructing + * the classpath (processorpath) correctly, based on the plugin configuration and dependency management. + */ + String[] onClasspath() default ""; +} diff --git a/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-processor-dep-v1/pom.xml b/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-processor-dep-v1/pom.xml new file mode 100644 index 00000000..7e4b5821 --- /dev/null +++ b/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-processor-dep-v1/pom.xml @@ -0,0 +1,34 @@ + + + + + 4.0.0 + + + org.apache.maven.plugins.compiler.it + mcompiler391-test + 1.0.0-SNAPSHOT + + + mcompiler391-annotation-processor-dep + + diff --git a/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-processor-dep-v1/src/main/java/mcompiler391/AnnotationProcessorDependencyV1.java b/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-processor-dep-v1/src/main/java/mcompiler391/AnnotationProcessorDependencyV1.java new file mode 100644 index 00000000..0370df95 --- /dev/null +++ b/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-processor-dep-v1/src/main/java/mcompiler391/AnnotationProcessorDependencyV1.java @@ -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. + */ +package mcompiler391; + +public class AnnotationProcessorDependencyV1 {} diff --git a/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-processor-dep-v2/pom.xml b/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-processor-dep-v2/pom.xml new file mode 100644 index 00000000..f1e16a01 --- /dev/null +++ b/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-processor-dep-v2/pom.xml @@ -0,0 +1,37 @@ + + + + + 4.0.0 + + + org.apache.maven.plugins.compiler.it + mcompiler391-test + 1.0.0-SNAPSHOT + + + + mcompiler391-annotation-processor-dep + 2.0.0-SNAPSHOT + + diff --git a/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-processor-dep-v2/src/main/java/mcompiler391/AnnotationProcessorDependencyV2.java b/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-processor-dep-v2/src/main/java/mcompiler391/AnnotationProcessorDependencyV2.java new file mode 100644 index 00000000..e3c7879c --- /dev/null +++ b/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-processor-dep-v2/src/main/java/mcompiler391/AnnotationProcessorDependencyV2.java @@ -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. + */ +package mcompiler391; + +public class AnnotationProcessorDependencyV2 {} diff --git a/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-processor/pom.xml b/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-processor/pom.xml new file mode 100644 index 00000000..5fff8746 --- /dev/null +++ b/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-processor/pom.xml @@ -0,0 +1,47 @@ + + + + + 4.0.0 + + + org.apache.maven.plugins.compiler.it + mcompiler391-test + 1.0.0-SNAPSHOT + + + mcompiler391-annotation-processor + + + + org.apache.maven.plugins.compiler.it + mcompiler391-annotation-api + 1.0.0-SNAPSHOT + + + org.apache.maven.plugins.compiler.it + mcompiler391-annotation-processor-dep + 1.0.0-SNAPSHOT + + + + diff --git a/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-processor/src/main/java/mcompiler391/SimpleAnnotationProcessor.java b/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-processor/src/main/java/mcompiler391/SimpleAnnotationProcessor.java new file mode 100644 index 00000000..219bf160 --- /dev/null +++ b/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-processor/src/main/java/mcompiler391/SimpleAnnotationProcessor.java @@ -0,0 +1,89 @@ +/* + * 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 mcompiler391; + +import javax.annotation.processing.AbstractProcessor; +import javax.annotation.processing.Filer; +import javax.annotation.processing.RoundEnvironment; +import javax.annotation.processing.SupportedAnnotationTypes; +import javax.annotation.processing.SupportedSourceVersion; +import javax.lang.model.SourceVersion; +import javax.lang.model.element.Element; +import javax.lang.model.element.Name; +import javax.lang.model.element.PackageElement; +import javax.lang.model.element.TypeElement; +import javax.lang.model.util.Elements; +import javax.tools.FileObject; +import javax.tools.StandardLocation; + +import java.io.IOException; +import java.io.Writer; +import java.util.Set; + +@SupportedSourceVersion(SourceVersion.RELEASE_6) +@SupportedAnnotationTypes("mcompiler391.SimpleAnnotation") +public class SimpleAnnotationProcessor extends AbstractProcessor { + + @Override + public boolean process(Set annotations, RoundEnvironment roundEnv) { + if (annotations.isEmpty()) { + return true; + } + + Filer filer = processingEnv.getFiler(); + + Elements elementUtils = processingEnv.getElementUtils(); + + Set elements = + roundEnv.getElementsAnnotatedWith(annotations.iterator().next()); + + for (Element element : elements) { + Name name = element.getSimpleName(); + PackageElement packageElement = elementUtils.getPackageOf(element); + + SimpleAnnotation annotation = element.getAnnotation(SimpleAnnotation.class); + assertClassesAreOnClasspath(annotation.onClasspath()); + + try { + Name packageName = packageElement.getQualifiedName(); + FileObject resource = + filer.createResource(StandardLocation.SOURCE_OUTPUT, packageName, name + ".txt", element); + + Writer writer = resource.openWriter(); + writer.write(name.toString()); + writer.close(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + return !elements.isEmpty(); + } + + private void assertClassesAreOnClasspath(String[] fqcns) { + for (String fqcn : fqcns) { + try { + getClass().getClassLoader().loadClass(fqcn); + } catch (ClassNotFoundException cnfe) { + String msg = String.format( + "Expected class '%s' to be on the classpath (processorpath), " + "but it wasn't.", fqcn); + throw new RuntimeException(msg, cnfe); + } + } + } +} diff --git a/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user1/pom.xml b/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user1/pom.xml new file mode 100644 index 00000000..38158c97 --- /dev/null +++ b/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user1/pom.xml @@ -0,0 +1,89 @@ + + + + + 4.0.0 + + + org.apache.maven.plugins.compiler.it + mcompiler391-test + 1.0.0-SNAPSHOT + + + mcompiler391-annotation-user1 + Annotation processor version from depMgmt + useDepMgmt=false + + + + + org.apache.maven.plugins.compiler.it + mcompiler391-annotation-processor + 1.0.0-SNAPSHOT + + + + + + + org.apache.maven.plugins.compiler.it + mcompiler391-annotation-api + 1.0.0-SNAPSHOT + + + + + + + maven-compiler-plugin + + + mcompiler391.SimpleAnnotationProcessor + + + + org.apache.maven.plugins.compiler.it + mcompiler391-annotation-processor + + + + + + org.apache.maven.plugins.compiler.it + annotation-verify-plugin + 1.0.0-SNAPSHOT + + + verify-annotations + process-test-classes + + read-source + + + mcompiler391_1.SimpleObject + mcompiler391_1.SimpleTestObject + + + + + + + diff --git a/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user1/src/main/java/mcompiler391_1/SimpleObject.java b/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user1/src/main/java/mcompiler391_1/SimpleObject.java new file mode 100644 index 00000000..63d3a4b4 --- /dev/null +++ b/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user1/src/main/java/mcompiler391_1/SimpleObject.java @@ -0,0 +1,24 @@ +/* + * 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 mcompiler391_1; + +import mcompiler391.SimpleAnnotation; + +@SimpleAnnotation(onClasspath = "mcompiler391.AnnotationProcessorDependencyV1") +public class SimpleObject {} diff --git a/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user1/src/test/java/mcompiler391_1/SimpleTestObject.java b/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user1/src/test/java/mcompiler391_1/SimpleTestObject.java new file mode 100644 index 00000000..98fe8613 --- /dev/null +++ b/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user1/src/test/java/mcompiler391_1/SimpleTestObject.java @@ -0,0 +1,24 @@ +/* + * 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 mcompiler391_1; + +import mcompiler391.SimpleAnnotation; + +@SimpleAnnotation(onClasspath = "mcompiler391.AnnotationProcessorDependencyV1") +public class SimpleTestObject {} diff --git a/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user2/pom.xml b/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user2/pom.xml new file mode 100644 index 00000000..88fbcdd8 --- /dev/null +++ b/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user2/pom.xml @@ -0,0 +1,96 @@ + + + + + 4.0.0 + + + org.apache.maven.plugins.compiler.it + mcompiler391-test + 1.0.0-SNAPSHOT + + + mcompiler391-annotation-user2 + Annotation processor version from depMgmt + useDepMgmt=true + + + + org.apache.maven.plugins.compiler.it + mcompiler391-annotation-api + 1.0.0-SNAPSHOT + + + + + + + org.apache.maven.plugins.compiler.it + mcompiler391-annotation-processor + 1.0.0-SNAPSHOT + + + + org.apache.maven.plugins.compiler.it + mcompiler391-annotation-processor-dep + 2.0.0-SNAPSHOT + + + + + + + + maven-compiler-plugin + + + mcompiler391.SimpleAnnotationProcessor + + true + + + org.apache.maven.plugins.compiler.it + mcompiler391-annotation-processor + + + + + + org.apache.maven.plugins.compiler.it + annotation-verify-plugin + 1.0.0-SNAPSHOT + + + verify-annotations + process-test-classes + + read-source + + + mcompiler391_2.SimpleObject + mcompiler391_2.SimpleTestObject + + + + + + + diff --git a/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user2/src/main/java/mcompiler391_2/SimpleObject.java b/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user2/src/main/java/mcompiler391_2/SimpleObject.java new file mode 100644 index 00000000..a3c6f518 --- /dev/null +++ b/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user2/src/main/java/mcompiler391_2/SimpleObject.java @@ -0,0 +1,24 @@ +/* + * 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 mcompiler391_2; + +import mcompiler391.SimpleAnnotation; + +@SimpleAnnotation(onClasspath = "mcompiler391.AnnotationProcessorDependencyV2") +public class SimpleObject {} diff --git a/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user2/src/test/java/mcompiler391_2/SimpleTestObject.java b/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user2/src/test/java/mcompiler391_2/SimpleTestObject.java new file mode 100644 index 00000000..834891fb --- /dev/null +++ b/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user2/src/test/java/mcompiler391_2/SimpleTestObject.java @@ -0,0 +1,24 @@ +/* + * 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 mcompiler391_2; + +import mcompiler391.SimpleAnnotation; + +@SimpleAnnotation(onClasspath = "mcompiler391.AnnotationProcessorDependencyV2") +public class SimpleTestObject {} diff --git a/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user3/pom.xml b/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user3/pom.xml new file mode 100644 index 00000000..4dd9f736 --- /dev/null +++ b/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user3/pom.xml @@ -0,0 +1,80 @@ + + + + + 4.0.0 + + + org.apache.maven.plugins.compiler.it + mcompiler391-test + 1.0.0-SNAPSHOT + + + mcompiler391-annotation-user3 + Annotation processor version explicitly set + useDepMgmt=false + + + + org.apache.maven.plugins.compiler.it + mcompiler391-annotation-api + 1.0.0-SNAPSHOT + + + + + + + maven-compiler-plugin + + + mcompiler391.SimpleAnnotationProcessor + + + + org.apache.maven.plugins.compiler.it + mcompiler391-annotation-processor + 1.0.0-SNAPSHOT + + + + + + org.apache.maven.plugins.compiler.it + annotation-verify-plugin + 1.0.0-SNAPSHOT + + + verify-annotations + process-test-classes + + read-source + + + mcompiler391_3.SimpleObject + mcompiler391_3.SimpleTestObject + + + + + + + diff --git a/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user3/src/main/java/mcompiler391_3/SimpleObject.java b/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user3/src/main/java/mcompiler391_3/SimpleObject.java new file mode 100644 index 00000000..c5b80158 --- /dev/null +++ b/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user3/src/main/java/mcompiler391_3/SimpleObject.java @@ -0,0 +1,24 @@ +/* + * 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 mcompiler391_3; + +import mcompiler391.SimpleAnnotation; + +@SimpleAnnotation(onClasspath = "mcompiler391.AnnotationProcessorDependencyV1") +public class SimpleObject {} diff --git a/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user3/src/test/java/mcompiler391_3/SimpleTestObject.java b/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user3/src/test/java/mcompiler391_3/SimpleTestObject.java new file mode 100644 index 00000000..a09ab375 --- /dev/null +++ b/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user3/src/test/java/mcompiler391_3/SimpleTestObject.java @@ -0,0 +1,24 @@ +/* + * 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 mcompiler391_3; + +import mcompiler391.SimpleAnnotation; + +@SimpleAnnotation(onClasspath = "mcompiler391.AnnotationProcessorDependencyV1") +public class SimpleTestObject {} diff --git a/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user4/pom.xml b/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user4/pom.xml new file mode 100644 index 00000000..27a10508 --- /dev/null +++ b/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user4/pom.xml @@ -0,0 +1,92 @@ + + + + + 4.0.0 + + + org.apache.maven.plugins.compiler.it + mcompiler391-test + 1.0.0-SNAPSHOT + + + mcompiler391-annotation-user4 + Annotation processor version explicitly set + useDepMgmt=true + + + + org.apache.maven.plugins.compiler.it + mcompiler391-annotation-api + 1.0.0-SNAPSHOT + + + + + + + + org.apache.maven.plugins.compiler.it + mcompiler391-annotation-processor-dep + 2.0.0-SNAPSHOT + + + + + + + + maven-compiler-plugin + + + mcompiler391.SimpleAnnotationProcessor + + true + + + org.apache.maven.plugins.compiler.it + mcompiler391-annotation-processor + 1.0.0-SNAPSHOT + + + + + + org.apache.maven.plugins.compiler.it + annotation-verify-plugin + 1.0.0-SNAPSHOT + + + verify-annotations + process-test-classes + + read-source + + + mcompiler391_4.SimpleObject + mcompiler391_4.SimpleTestObject + + + + + + + diff --git a/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user4/src/main/java/mcompiler391_4/SimpleObject.java b/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user4/src/main/java/mcompiler391_4/SimpleObject.java new file mode 100644 index 00000000..c791361e --- /dev/null +++ b/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user4/src/main/java/mcompiler391_4/SimpleObject.java @@ -0,0 +1,24 @@ +/* + * 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 mcompiler391_4; + +import mcompiler391.SimpleAnnotation; + +@SimpleAnnotation(onClasspath = "mcompiler391.AnnotationProcessorDependencyV2") +public class SimpleObject {} diff --git a/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user4/src/test/java/mcompiler391_4/SimpleTestObject.java b/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user4/src/test/java/mcompiler391_4/SimpleTestObject.java new file mode 100644 index 00000000..32eefa58 --- /dev/null +++ b/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user4/src/test/java/mcompiler391_4/SimpleTestObject.java @@ -0,0 +1,24 @@ +/* + * 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 mcompiler391_4; + +import mcompiler391.SimpleAnnotation; + +@SimpleAnnotation(onClasspath = "mcompiler391.AnnotationProcessorDependencyV2") +public class SimpleTestObject {} diff --git a/src/it/MCOMPILER-391-processorpath-dep-mgmt/invoker.properties b/src/it/MCOMPILER-391-processorpath-dep-mgmt/invoker.properties new file mode 100644 index 00000000..a0a3964f --- /dev/null +++ b/src/it/MCOMPILER-391-processorpath-dep-mgmt/invoker.properties @@ -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. + +invoker.goals=process-test-classes diff --git a/src/it/MCOMPILER-391-processorpath-dep-mgmt/pom.xml b/src/it/MCOMPILER-391-processorpath-dep-mgmt/pom.xml new file mode 100644 index 00000000..761081bd --- /dev/null +++ b/src/it/MCOMPILER-391-processorpath-dep-mgmt/pom.xml @@ -0,0 +1,57 @@ + + + + + 4.0.0 + + org.apache.maven.plugins.compiler.it + mcompiler391-test + 1.0.0-SNAPSHOT + pom + + https://issues.apache.org/jira/browse/MCOMPILER-391 + + + annotation-api + annotation-processor-dep-v1 + annotation-processor-dep-v2 + annotation-processor + annotation-user1 + annotation-user2 + annotation-user3 + annotation-user4 + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + @project.version@ + + + + + + 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 ad68b4f0..8158355f 100644 --- a/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java +++ b/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java @@ -36,9 +36,13 @@ import java.util.LinkedHashSet; import java.util.List; import java.util.Map; +import java.util.Objects; +import java.util.Optional; import java.util.Properties; import java.util.Set; +import java.util.stream.Collectors; +import org.apache.maven.RepositoryUtils; import org.apache.maven.artifact.handler.ArtifactHandler; import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager; import org.apache.maven.execution.MavenExecutionRequest; @@ -77,6 +81,7 @@ import org.codehaus.plexus.languages.java.version.JavaVersion; import org.eclipse.aether.RepositorySystem; import org.eclipse.aether.artifact.Artifact; +import org.eclipse.aether.artifact.ArtifactTypeRegistry; import org.eclipse.aether.artifact.DefaultArtifact; import org.eclipse.aether.collection.CollectRequest; import org.eclipse.aether.graph.Dependency; @@ -309,7 +314,7 @@ public abstract class AbstractCompilerMojo extends AbstractMojo { * <path> * <groupId>org.sample</groupId> * <artifactId>sample-annotation-processor</artifactId> - * <version>1.2.3</version> + * <version>1.2.3</version> <!-- Optional - taken from dependency management if not specified --> * <!-- Optionally exclude transitive dependencies --> * <exclusions> * <exclusion> @@ -330,6 +335,22 @@ public abstract class AbstractCompilerMojo extends AbstractMojo { @Parameter private List annotationProcessorPaths; + /** + *

+ * Whether to use the Maven dependency management section when resolving transitive dependencies of annotation + * processor paths. + *

+ *

+ * This flag does not enable / disable the ability to get the version of annotation processor paths + * (the top-level entries) from dependency management section. It only influences the resolution of + * transitive dependencies of those top-level paths. + *

+ * + * @since 3.12.0 + */ + @Parameter(defaultValue = "false") + private boolean annotationProcessorPathsUseDepMgmt; + /** *

* Sets the arguments to be passed to the compiler (prepending a dash). @@ -1587,8 +1608,9 @@ private List resolveProcessorPathEntries() throws MojoExecutionException Set elements = new LinkedHashSet<>(); try { List dependencies = convertToDependencies(annotationProcessorPaths); + List managedDependencies = getManagedDependencies(); CollectRequest collectRequest = - new CollectRequest(dependencies, Collections.emptyList(), project.getRemoteProjectRepositories()); + new CollectRequest(dependencies, managedDependencies, project.getRemoteProjectRepositories()); DependencyRequest dependencyRequest = new DependencyRequest(); dependencyRequest.setCollectRequest(collectRequest); DependencyResult dependencyResult = @@ -1604,22 +1626,53 @@ private List resolveProcessorPathEntries() throws MojoExecutionException } } - private List convertToDependencies(List annotationProcessorPaths) { + private List getManagedDependencies() { + if (!annotationProcessorPathsUseDepMgmt + || project.getDependencyManagement() == null + || project.getDependencyManagement().getDependencies() == null) { + return Collections.emptyList(); + } + ArtifactTypeRegistry artifactTypeRegistry = + session.getRepositorySession().getArtifactTypeRegistry(); + + return project.getDependencyManagement().getDependencies().stream() + .map(dep -> RepositoryUtils.toDependency(dep, artifactTypeRegistry)) + .collect(Collectors.toList()); + } + + private List convertToDependencies(List annotationProcessorPaths) + throws MojoExecutionException { List dependencies = new ArrayList<>(); for (DependencyCoordinate annotationProcessorPath : annotationProcessorPaths) { ArtifactHandler handler = artifactHandlerManager.getArtifactHandler(annotationProcessorPath.getType()); + String version = annotationProcessorPath.getVersion(); + if (version == null) { + version = findManagedVersion(annotationProcessorPath) + .orElseThrow( + () -> new MojoExecutionException("Cannot find version for " + annotationProcessorPath)); + } Artifact artifact = new DefaultArtifact( annotationProcessorPath.getGroupId(), annotationProcessorPath.getArtifactId(), annotationProcessorPath.getClassifier(), handler.getExtension(), - annotationProcessorPath.getVersion()); + version); Set exclusions = convertToAetherExclusions(annotationProcessorPath.getExclusions()); dependencies.add(new Dependency(artifact, JavaScopes.RUNTIME, false, exclusions)); } return dependencies; } + private Optional findManagedVersion(DependencyCoordinate dependencyCoordinate) { + return project.getDependencyManagement().getDependencies().stream() + .filter(dep -> Objects.equals(dep.getGroupId(), dependencyCoordinate.getGroupId()) + && Objects.equals(dep.getArtifactId(), dependencyCoordinate.getArtifactId()) + && Objects.equals(dep.getClassifier(), dependencyCoordinate.getClassifier()) + && Objects.equals(dep.getType(), dependencyCoordinate.getType())) + .findAny() + .map(org.apache.maven.model.Dependency::getVersion); + } + private Set convertToAetherExclusions(Set exclusions) { if (exclusions == null || exclusions.isEmpty()) { return Collections.emptySet();