This is an automated email from the ASF dual-hosted git repository. sjaranowski 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 94eb582 [MCOMPILER-391] Use dep mgmt to resolve annotation processors and their deps 94eb582 is described below commit 94eb5829e404eea9109baea6a1455012e936d340 Author: Petr Široký <petr.sir...@pm.me> AuthorDate: Wed Feb 15 20:49:40 2023 +0100 [MCOMPILER-391] Use dep mgmt to resolve annotation processors and their deps * dependency managemet can be used for two slighty difference use cases (when it comes to annotation processors): -- 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 valid combinations (some of 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 ++++++++ .../main/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 +++++++++++ .../mcompiler391/SimpleAnnotationProcessor.java | 87 +++++++++++++++++++ .../annotation-user1/pom.xml | 89 ++++++++++++++++++++ .../src/main/java/mcompiler391/SimpleObject1.java | 22 +++++ .../test/java/mcompiler391/SimpleTestObject1.java | 22 +++++ .../annotation-user2/pom.xml | 96 +++++++++++++++++++++ .../src/main/java/mcompiler391/SimpleObject2.java | 22 +++++ .../test/java/mcompiler391/SimpleTestObject2.java | 22 +++++ .../annotation-user3/pom.xml | 80 ++++++++++++++++++ .../src/main/java/mcompiler391/SimpleObject3.java | 22 +++++ .../test/java/mcompiler391/SimpleTestObject3.java | 22 +++++ .../annotation-user4/pom.xml | 92 ++++++++++++++++++++ .../src/main/java/mcompiler391/SimpleObject4.java | 22 +++++ .../test/java/mcompiler391/SimpleTestObject4.java | 22 +++++ .../invoker.properties | 18 ++++ .../MCOMPILER-391-processorpath-dep-mgmt/pom.xml | 57 +++++++++++++ .../plugin/compiler/AbstractCompilerMojo.java | 98 ++++++++++++++++++---- 23 files changed, 1008 insertions(+), 14 deletions(-) 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 0000000..0a0cb9e --- /dev/null +++ b/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-api/pom.xml @@ -0,0 +1,34 @@ +<?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/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.maven.plugins.compiler.it</groupId> + <artifactId>mcompiler391-test</artifactId> + <version>1.0.0-SNAPSHOT</version> + </parent> + + <artifactId>mcompiler391-annotation-api</artifactId> + +</project> 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 0000000..849478d --- /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 class (FQCN) is 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 0000000..7e4b582 --- /dev/null +++ b/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-processor-dep-v1/pom.xml @@ -0,0 +1,34 @@ +<?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/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.maven.plugins.compiler.it</groupId> + <artifactId>mcompiler391-test</artifactId> + <version>1.0.0-SNAPSHOT</version> + </parent> + + <artifactId>mcompiler391-annotation-processor-dep</artifactId> + +</project> 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 0000000..0370df9 --- /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 0000000..f1e16a0 --- /dev/null +++ b/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-processor-dep-v2/pom.xml @@ -0,0 +1,37 @@ +<?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/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.maven.plugins.compiler.it</groupId> + <artifactId>mcompiler391-test</artifactId> + <version>1.0.0-SNAPSHOT</version> + </parent> + + <!-- Same groupId + artifactId as for v1 (1.0.0-SNAPSHOT), but different version to test version overrides + via dependency management --> + <artifactId>mcompiler391-annotation-processor-dep</artifactId> + <version>2.0.0-SNAPSHOT</version> + +</project> 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 0000000..e3c7879 --- /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 0000000..5fff874 --- /dev/null +++ b/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-processor/pom.xml @@ -0,0 +1,47 @@ +<?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/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.maven.plugins.compiler.it</groupId> + <artifactId>mcompiler391-test</artifactId> + <version>1.0.0-SNAPSHOT</version> + </parent> + + <artifactId>mcompiler391-annotation-processor</artifactId> + + <dependencies> + <dependency> + <groupId>org.apache.maven.plugins.compiler.it</groupId> + <artifactId>mcompiler391-annotation-api</artifactId> + <version>1.0.0-SNAPSHOT</version> + </dependency> + <dependency> + <groupId>org.apache.maven.plugins.compiler.it</groupId> + <artifactId>mcompiler391-annotation-processor-dep</artifactId> + <version>1.0.0-SNAPSHOT</version> + </dependency> + </dependencies> + +</project> 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 0000000..9ff0f70 --- /dev/null +++ b/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-processor/src/main/java/mcompiler391/SimpleAnnotationProcessor.java @@ -0,0 +1,87 @@ +/* + * 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<? extends TypeElement> annotations, RoundEnvironment roundEnv) { + if (annotations.isEmpty()) { + return true; + } + + Filer filer = processingEnv.getFiler(); + + Elements elementUtils = processingEnv.getElementUtils(); + + Set<? extends Element> 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); + assertThatClassIsOnClasspath(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 assertThatClassIsOnClasspath(String fqcn) { + 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 0000000..5eab142 --- /dev/null +++ b/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user1/pom.xml @@ -0,0 +1,89 @@ +<?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/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.maven.plugins.compiler.it</groupId> + <artifactId>mcompiler391-test</artifactId> + <version>1.0.0-SNAPSHOT</version> + </parent> + + <artifactId>mcompiler391-annotation-user1</artifactId> + <name>Annotation processor version from depMgmt + useDepMgmt=false</name> + + <dependencyManagement> + <dependencies> + <dependency> + <groupId>org.apache.maven.plugins.compiler.it</groupId> + <artifactId>mcompiler391-annotation-processor</artifactId> + <version>1.0.0-SNAPSHOT</version> + </dependency> + </dependencies> + </dependencyManagement> + + <dependencies> + <dependency> + <groupId>org.apache.maven.plugins.compiler.it</groupId> + <artifactId>mcompiler391-annotation-api</artifactId> + <version>1.0.0-SNAPSHOT</version> + </dependency> + </dependencies> + + <build> + <plugins> + <plugin> + <artifactId>maven-compiler-plugin</artifactId> + <configuration> + <annotationProcessors> + <annotationProcessor>mcompiler391.SimpleAnnotationProcessor</annotationProcessor> + </annotationProcessors> + <annotationProcessorPaths> + <path> + <groupId>org.apache.maven.plugins.compiler.it</groupId> + <artifactId>mcompiler391-annotation-processor</artifactId> + </path> + </annotationProcessorPaths> + </configuration> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins.compiler.it</groupId> + <artifactId>annotation-verify-plugin</artifactId> + <version>1.0.0-SNAPSHOT</version> + <executions> + <execution> + <id>verify-annotations</id> + <phase>process-test-classes</phase> + <goals> + <goal>read-source</goal> + </goals> + <configuration> + <sourceClass>mcompiler391.SimpleObject1</sourceClass> + <testSourceClass>mcompiler391.SimpleTestObject1</testSourceClass> + </configuration> + </execution> + </executions> + </plugin> + </plugins> + </build> +</project> diff --git a/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user1/src/main/java/mcompiler391/SimpleObject1.java b/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user1/src/main/java/mcompiler391/SimpleObject1.java new file mode 100644 index 0000000..8bd8a0e --- /dev/null +++ b/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user1/src/main/java/mcompiler391/SimpleObject1.java @@ -0,0 +1,22 @@ +/* + * 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; + +@SimpleAnnotation(onClasspath = "mcompiler391.AnnotationProcessorDependencyV1") +public class SimpleObject1 {} diff --git a/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user1/src/test/java/mcompiler391/SimpleTestObject1.java b/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user1/src/test/java/mcompiler391/SimpleTestObject1.java new file mode 100644 index 0000000..d859211 --- /dev/null +++ b/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user1/src/test/java/mcompiler391/SimpleTestObject1.java @@ -0,0 +1,22 @@ +/* + * 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; + +@SimpleAnnotation(onClasspath = "mcompiler391.AnnotationProcessorDependencyV1") +public class SimpleTestObject1 {} 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 0000000..56958de --- /dev/null +++ b/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user2/pom.xml @@ -0,0 +1,96 @@ +<?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/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.maven.plugins.compiler.it</groupId> + <artifactId>mcompiler391-test</artifactId> + <version>1.0.0-SNAPSHOT</version> + </parent> + + <artifactId>mcompiler391-annotation-user2</artifactId> + <name>Annotation processor version from depMgmt + useDepMgmt=true</name> + + <dependencies> + <dependency> + <groupId>org.apache.maven.plugins.compiler.it</groupId> + <artifactId>mcompiler391-annotation-api</artifactId> + <version>1.0.0-SNAPSHOT</version> + </dependency> + </dependencies> + + <dependencyManagement> + <dependencies> + <dependency> + <groupId>org.apache.maven.plugins.compiler.it</groupId> + <artifactId>mcompiler391-annotation-processor</artifactId> + <version>1.0.0-SNAPSHOT</version> + </dependency> + <dependency> + <!-- Overrides the version defined by mcompiler391-annotation-processor itself --> + <groupId>org.apache.maven.plugins.compiler.it</groupId> + <artifactId>mcompiler391-annotation-processor-dep</artifactId> + <version>2.0.0-SNAPSHOT</version> + </dependency> + </dependencies> + </dependencyManagement> + + <build> + <plugins> + <plugin> + <artifactId>maven-compiler-plugin</artifactId> + <configuration> + <annotationProcessors> + <annotationProcessor>mcompiler391.SimpleAnnotationProcessor</annotationProcessor> + </annotationProcessors> + <annotationProcessorPathsUseDepMgmt>true</annotationProcessorPathsUseDepMgmt> + <annotationProcessorPaths> + <path> + <groupId>org.apache.maven.plugins.compiler.it</groupId> + <artifactId>mcompiler391-annotation-processor</artifactId> + </path> + </annotationProcessorPaths> + </configuration> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins.compiler.it</groupId> + <artifactId>annotation-verify-plugin</artifactId> + <version>1.0.0-SNAPSHOT</version> + <executions> + <execution> + <id>verify-annotations</id> + <phase>process-test-classes</phase> + <goals> + <goal>read-source</goal> + </goals> + <configuration> + <sourceClass>mcompiler391.SimpleObject2</sourceClass> + <testSourceClass>mcompiler391.SimpleTestObject2</testSourceClass> + </configuration> + </execution> + </executions> + </plugin> + </plugins> + </build> +</project> diff --git a/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user2/src/main/java/mcompiler391/SimpleObject2.java b/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user2/src/main/java/mcompiler391/SimpleObject2.java new file mode 100644 index 0000000..35072a3 --- /dev/null +++ b/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user2/src/main/java/mcompiler391/SimpleObject2.java @@ -0,0 +1,22 @@ +/* + * 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; + +@SimpleAnnotation(onClasspath = "mcompiler391.AnnotationProcessorDependencyV2") +public class SimpleObject2 {} diff --git a/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user2/src/test/java/mcompiler391/SimpleTestObject2.java b/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user2/src/test/java/mcompiler391/SimpleTestObject2.java new file mode 100644 index 0000000..fc00690 --- /dev/null +++ b/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user2/src/test/java/mcompiler391/SimpleTestObject2.java @@ -0,0 +1,22 @@ +/* + * 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; + +@SimpleAnnotation(onClasspath = "mcompiler391.AnnotationProcessorDependencyV2") +public class SimpleTestObject2 {} 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 0000000..55b44c1 --- /dev/null +++ b/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user3/pom.xml @@ -0,0 +1,80 @@ +<?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/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.maven.plugins.compiler.it</groupId> + <artifactId>mcompiler391-test</artifactId> + <version>1.0.0-SNAPSHOT</version> + </parent> + + <artifactId>mcompiler391-annotation-user3</artifactId> + <name>Annotation processor version explicitly set + useDepMgmt=false</name> + + <dependencies> + <dependency> + <groupId>org.apache.maven.plugins.compiler.it</groupId> + <artifactId>mcompiler391-annotation-api</artifactId> + <version>1.0.0-SNAPSHOT</version> + </dependency> + </dependencies> + + <build> + <plugins> + <plugin> + <artifactId>maven-compiler-plugin</artifactId> + <configuration> + <annotationProcessors> + <annotationProcessor>mcompiler391.SimpleAnnotationProcessor</annotationProcessor> + </annotationProcessors> + <annotationProcessorPaths> + <path> + <groupId>org.apache.maven.plugins.compiler.it</groupId> + <artifactId>mcompiler391-annotation-processor</artifactId> + <version>1.0.0-SNAPSHOT</version> + </path> + </annotationProcessorPaths> + </configuration> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins.compiler.it</groupId> + <artifactId>annotation-verify-plugin</artifactId> + <version>1.0.0-SNAPSHOT</version> + <executions> + <execution> + <id>verify-annotations</id> + <phase>process-test-classes</phase> + <goals> + <goal>read-source</goal> + </goals> + <configuration> + <sourceClass>mcompiler391.SimpleObject3</sourceClass> + <testSourceClass>mcompiler391.SimpleTestObject3</testSourceClass> + </configuration> + </execution> + </executions> + </plugin> + </plugins> + </build> +</project> diff --git a/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user3/src/main/java/mcompiler391/SimpleObject3.java b/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user3/src/main/java/mcompiler391/SimpleObject3.java new file mode 100644 index 0000000..9334f44 --- /dev/null +++ b/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user3/src/main/java/mcompiler391/SimpleObject3.java @@ -0,0 +1,22 @@ +/* + * 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; + +@SimpleAnnotation(onClasspath = "mcompiler391.AnnotationProcessorDependencyV1") +public class SimpleObject3 {} diff --git a/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user3/src/test/java/mcompiler391/SimpleTestObject3.java b/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user3/src/test/java/mcompiler391/SimpleTestObject3.java new file mode 100644 index 0000000..9f3510c --- /dev/null +++ b/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user3/src/test/java/mcompiler391/SimpleTestObject3.java @@ -0,0 +1,22 @@ +/* + * 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; + +@SimpleAnnotation(onClasspath = "mcompiler391.AnnotationProcessorDependencyV1") +public class SimpleTestObject3 {} 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 0000000..d4fb7da --- /dev/null +++ b/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user4/pom.xml @@ -0,0 +1,92 @@ +<?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/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.maven.plugins.compiler.it</groupId> + <artifactId>mcompiler391-test</artifactId> + <version>1.0.0-SNAPSHOT</version> + </parent> + + <artifactId>mcompiler391-annotation-user4</artifactId> + <name>Annotation processor version explicitly set + useDepMgmt=true</name> + + <dependencies> + <dependency> + <groupId>org.apache.maven.plugins.compiler.it</groupId> + <artifactId>mcompiler391-annotation-api</artifactId> + <version>1.0.0-SNAPSHOT</version> + </dependency> + </dependencies> + + <dependencyManagement> + <dependencies> + <dependency> + <!-- Overrides the version defined by mcompiler391-annotation-processor itself --> + <groupId>org.apache.maven.plugins.compiler.it</groupId> + <artifactId>mcompiler391-annotation-processor-dep</artifactId> + <version>2.0.0-SNAPSHOT</version> + </dependency> + </dependencies> + </dependencyManagement> + + <build> + <plugins> + <plugin> + <artifactId>maven-compiler-plugin</artifactId> + <configuration> + <annotationProcessors> + <annotationProcessor>mcompiler391.SimpleAnnotationProcessor</annotationProcessor> + </annotationProcessors> + <annotationProcessorPathsUseDepMgmt>true</annotationProcessorPathsUseDepMgmt> + <annotationProcessorPaths> + <path> + <groupId>org.apache.maven.plugins.compiler.it</groupId> + <artifactId>mcompiler391-annotation-processor</artifactId> + <version>1.0.0-SNAPSHOT</version> + </path> + </annotationProcessorPaths> + </configuration> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins.compiler.it</groupId> + <artifactId>annotation-verify-plugin</artifactId> + <version>1.0.0-SNAPSHOT</version> + <executions> + <execution> + <id>verify-annotations</id> + <phase>process-test-classes</phase> + <goals> + <goal>read-source</goal> + </goals> + <configuration> + <sourceClass>mcompiler391.SimpleObject4</sourceClass> + <testSourceClass>mcompiler391.SimpleTestObject4</testSourceClass> + </configuration> + </execution> + </executions> + </plugin> + </plugins> + </build> +</project> diff --git a/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user4/src/main/java/mcompiler391/SimpleObject4.java b/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user4/src/main/java/mcompiler391/SimpleObject4.java new file mode 100644 index 0000000..baa0c78 --- /dev/null +++ b/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user4/src/main/java/mcompiler391/SimpleObject4.java @@ -0,0 +1,22 @@ +/* + * 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; + +@SimpleAnnotation(onClasspath = "mcompiler391.AnnotationProcessorDependencyV2") +public class SimpleObject4 {} diff --git a/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user4/src/test/java/mcompiler391/SimpleTestObject4.java b/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user4/src/test/java/mcompiler391/SimpleTestObject4.java new file mode 100644 index 0000000..657b1b3 --- /dev/null +++ b/src/it/MCOMPILER-391-processorpath-dep-mgmt/annotation-user4/src/test/java/mcompiler391/SimpleTestObject4.java @@ -0,0 +1,22 @@ +/* + * 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; + +@SimpleAnnotation(onClasspath = "mcompiler391.AnnotationProcessorDependencyV2") +public class SimpleTestObject4 {} 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 0000000..a0a3964 --- /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 0000000..761081b --- /dev/null +++ b/src/it/MCOMPILER-391-processorpath-dep-mgmt/pom.xml @@ -0,0 +1,57 @@ +<?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/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <groupId>org.apache.maven.plugins.compiler.it</groupId> + <artifactId>mcompiler391-test</artifactId> + <version>1.0.0-SNAPSHOT</version> + <packaging>pom</packaging> + + <url>https://issues.apache.org/jira/browse/MCOMPILER-391</url> + + <modules> + <module>annotation-api</module> + <module>annotation-processor-dep-v1</module> + <module>annotation-processor-dep-v2</module> + <module>annotation-processor</module> + <module>annotation-user1</module> + <module>annotation-user2</module> + <module>annotation-user3</module> + <module>annotation-user4</module> + </modules> + + + <build> + <pluginManagement> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + <version>@project.version@</version> + </plugin> + </plugins> + </pluginManagement> + </build> +</project> + 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 f975bab..0537bc6 100644 --- a/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java +++ b/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java @@ -36,13 +36,19 @@ import java.util.Iterator; 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; import org.apache.maven.execution.MavenSession; +import org.apache.maven.model.Dependency; +import org.apache.maven.model.DependencyManagement; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecution; import org.apache.maven.plugin.MojoExecutionException; @@ -77,11 +83,10 @@ import org.codehaus.plexus.languages.java.jpms.JavaModuleDescriptor; 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; import org.eclipse.aether.graph.Exclusion; -import org.eclipse.aether.resolution.ArtifactResult; import org.eclipse.aether.resolution.DependencyRequest; import org.eclipse.aether.resolution.DependencyResult; import org.eclipse.aether.util.artifact.JavaScopes; @@ -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<DependencyCoordinate> annotationProcessorPaths; + /** + * <p> + * Whether to use the Maven dependency management section when resolving transitive dependencies of annotation + * processor paths. + * </p> + * <p> + * This flag does not enable / disable the ability to resolve the version of annotation processor paths + * from dependency management section. It only influences the resolution o transitive dependencies of those + * top-level paths. + * </p> + * + * @since 3.12.0 + */ + @Parameter(defaultValue = "false") + private boolean annotationProcessorPathsUseDepMgmt; + /** * <p> * Sets the arguments to be passed to the compiler (prepending a dash). @@ -1584,42 +1605,91 @@ public abstract class AbstractCompilerMojo extends AbstractMojo { return null; } - Set<String> elements = new LinkedHashSet<>(); try { - List<Dependency> dependencies = convertToDependencies(annotationProcessorPaths); + List<org.eclipse.aether.graph.Dependency> dependencies = convertToDependencies(annotationProcessorPaths); + List<org.eclipse.aether.graph.Dependency> managedDependencies = + getManagedDependenciesForAnnotationProcessorPaths(); CollectRequest collectRequest = - new CollectRequest(dependencies, Collections.emptyList(), project.getRemoteProjectRepositories()); + new CollectRequest(dependencies, managedDependencies, project.getRemoteProjectRepositories()); DependencyRequest dependencyRequest = new DependencyRequest(); dependencyRequest.setCollectRequest(collectRequest); DependencyResult dependencyResult = repositorySystem.resolveDependencies(session.getRepositorySession(), dependencyRequest); - for (ArtifactResult resolved : dependencyResult.getArtifactResults()) { - elements.add(resolved.getArtifact().getFile().getAbsolutePath()); - } - return new ArrayList<>(elements); + return dependencyResult.getArtifactResults().stream() + .map(resolved -> resolved.getArtifact().getFile().getAbsolutePath()) + .collect(Collectors.toList()); } catch (Exception e) { throw new MojoExecutionException( "Resolution of annotationProcessorPath dependencies failed: " + e.getLocalizedMessage(), e); } } - private List<Dependency> convertToDependencies(List<DependencyCoordinate> annotationProcessorPaths) { - List<Dependency> dependencies = new ArrayList<>(); + private List<org.eclipse.aether.graph.Dependency> convertToDependencies( + List<DependencyCoordinate> annotationProcessorPaths) throws MojoExecutionException { + List<org.eclipse.aether.graph.Dependency> dependencies = new ArrayList<>(); for (DependencyCoordinate annotationProcessorPath : annotationProcessorPaths) { ArtifactHandler handler = artifactHandlerManager.getArtifactHandler(annotationProcessorPath.getType()); + String version = getAnnotationProcessorPathVersion(annotationProcessorPath); Artifact artifact = new DefaultArtifact( annotationProcessorPath.getGroupId(), annotationProcessorPath.getArtifactId(), annotationProcessorPath.getClassifier(), handler.getExtension(), - annotationProcessorPath.getVersion()); + version); Set<Exclusion> exclusions = convertToAetherExclusions(annotationProcessorPath.getExclusions()); - dependencies.add(new Dependency(artifact, JavaScopes.RUNTIME, false, exclusions)); + dependencies.add(new org.eclipse.aether.graph.Dependency(artifact, JavaScopes.RUNTIME, false, exclusions)); } return dependencies; } + private String getAnnotationProcessorPathVersion(DependencyCoordinate annotationProcessorPath) + throws MojoExecutionException { + String configuredVersion = annotationProcessorPath.getVersion(); + if (configuredVersion != null) { + return configuredVersion; + } else { + List<Dependency> managedDependencies = getProjectManagedDependencies(); + return findManagedVersion(annotationProcessorPath, managedDependencies) + .orElseThrow(() -> new MojoExecutionException(String.format( + "Cannot find version for annotation processor path '%s'. The version needs to be either" + + " provided directly in the plugin configuration or via dependency management.", + annotationProcessorPath))); + } + } + + private Optional<String> findManagedVersion( + DependencyCoordinate dependencyCoordinate, List<Dependency> managedDependencies) { + return managedDependencies.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 List<org.eclipse.aether.graph.Dependency> getManagedDependenciesForAnnotationProcessorPaths() { + if (!annotationProcessorPathsUseDepMgmt) { + return Collections.emptyList(); + } + List<Dependency> projectManagedDependencies = getProjectManagedDependencies(); + ArtifactTypeRegistry artifactTypeRegistry = + session.getRepositorySession().getArtifactTypeRegistry(); + + return projectManagedDependencies.stream() + .map(dep -> RepositoryUtils.toDependency(dep, artifactTypeRegistry)) + .collect(Collectors.toList()); + } + + private List<Dependency> getProjectManagedDependencies() { + DependencyManagement dependencyManagement = project.getDependencyManagement(); + if (dependencyManagement == null || dependencyManagement.getDependencies() == null) { + return Collections.emptyList(); + } + return dependencyManagement.getDependencies(); + } + private Set<Exclusion> convertToAetherExclusions(Set<DependencyExclusion> exclusions) { if (exclusions == null || exclusions.isEmpty()) { return Collections.emptySet();