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..849478d4 --- /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 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..9ff0f704 --- /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 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); + 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 00000000..5eab1425 --- /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.SimpleObject1 + mcompiler391.SimpleTestObject1 + + + + + + + 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 00000000..8bd8a0e4 --- /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 00000000..d8592118 --- /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 00000000..56958dec --- /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.SimpleObject2 + mcompiler391.SimpleTestObject2 + + + + + + + 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 00000000..35072a3d --- /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 00000000..fc006907 --- /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 00000000..55b44c10 --- /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.SimpleObject3 + mcompiler391.SimpleTestObject3 + + + + + + + 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 00000000..9334f44c --- /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 00000000..9f3510cf --- /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 00000000..d4fb7dae --- /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.SimpleObject4 + mcompiler391.SimpleTestObject4 + + + + + + + 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 00000000..baa0c782 --- /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 00000000..657b1b30 --- /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 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 f975bab8..0537bc65 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.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.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 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 resolve the version of annotation processor paths + * from dependency management section. It only influences the resolution o 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). @@ -1584,42 +1605,91 @@ private List resolveProcessorPathEntries() throws MojoExecutionException return null; } - Set elements = new LinkedHashSet<>(); try { - List dependencies = convertToDependencies(annotationProcessorPaths); + List dependencies = convertToDependencies(annotationProcessorPaths); + List 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 convertToDependencies(List annotationProcessorPaths) { - List dependencies = new ArrayList<>(); + private List convertToDependencies( + List annotationProcessorPaths) throws MojoExecutionException { + List 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 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 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 findManagedVersion( + DependencyCoordinate dependencyCoordinate, List 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 getManagedDependenciesForAnnotationProcessorPaths() { + if (!annotationProcessorPathsUseDepMgmt) { + return Collections.emptyList(); + } + List projectManagedDependencies = getProjectManagedDependencies(); + ArtifactTypeRegistry artifactTypeRegistry = + session.getRepositorySession().getArtifactTypeRegistry(); + + return projectManagedDependencies.stream() + .map(dep -> RepositoryUtils.toDependency(dep, artifactTypeRegistry)) + .collect(Collectors.toList()); + } + + private List getProjectManagedDependencies() { + DependencyManagement dependencyManagement = project.getDependencyManagement(); + if (dependencyManagement == null || dependencyManagement.getDependencies() == null) { + return Collections.emptyList(); + } + return dependencyManagement.getDependencies(); + } + private Set convertToAetherExclusions(Set exclusions) { if (exclusions == null || exclusions.isEmpty()) { return Collections.emptySet();