From 9372fae62e26493a3ec94870f687daac3292720e 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] Get annotation processor version from dependencyManagement --- .../annotation-processor/pom.xml | 34 +++++++++ .../SimpleAnnotationProcessor.java | 75 +++++++++++++++++++ .../annotation-user/pom.xml | 70 +++++++++++++++++ .../java/mcompiler391/SimpleAnnotation.java | 28 +++++++ .../main/java/mcompiler391/SimpleObject.java | 22 ++++++ .../java/mcompiler391/SimpleTestObject.java | 22 ++++++ .../invoker.properties | 18 +++++ .../pom.xml | 56 ++++++++++++++ .../plugin/compiler/AbstractCompilerMojo.java | 23 +++++- 9 files changed, 346 insertions(+), 2 deletions(-) create mode 100644 src/it/MCOMPILER-391-annotation-processors-dep-mgmt/annotation-processor/pom.xml create mode 100644 src/it/MCOMPILER-391-annotation-processors-dep-mgmt/annotation-processor/src/main/java/mcompiler391/SimpleAnnotationProcessor.java create mode 100644 src/it/MCOMPILER-391-annotation-processors-dep-mgmt/annotation-user/pom.xml create mode 100644 src/it/MCOMPILER-391-annotation-processors-dep-mgmt/annotation-user/src/main/java/mcompiler391/SimpleAnnotation.java create mode 100644 src/it/MCOMPILER-391-annotation-processors-dep-mgmt/annotation-user/src/main/java/mcompiler391/SimpleObject.java create mode 100644 src/it/MCOMPILER-391-annotation-processors-dep-mgmt/annotation-user/src/test/java/mcompiler391/SimpleTestObject.java create mode 100644 src/it/MCOMPILER-391-annotation-processors-dep-mgmt/invoker.properties create mode 100644 src/it/MCOMPILER-391-annotation-processors-dep-mgmt/pom.xml diff --git a/src/it/MCOMPILER-391-annotation-processors-dep-mgmt/annotation-processor/pom.xml b/src/it/MCOMPILER-391-annotation-processors-dep-mgmt/annotation-processor/pom.xml new file mode 100644 index 00000000..53816b9b --- /dev/null +++ b/src/it/MCOMPILER-391-annotation-processors-dep-mgmt/annotation-processor/pom.xml @@ -0,0 +1,34 @@ + + + + + 4.0.0 + + + org.apache.maven.plugins.compiler.it + mcompiler391-test + 1.0.0-SNAPSHOT + + + mcompiler391-annotation-processor + + diff --git a/src/it/MCOMPILER-391-annotation-processors-dep-mgmt/annotation-processor/src/main/java/mcompiler391/SimpleAnnotationProcessor.java b/src/it/MCOMPILER-391-annotation-processors-dep-mgmt/annotation-processor/src/main/java/mcompiler391/SimpleAnnotationProcessor.java new file mode 100644 index 00000000..4b88a2be --- /dev/null +++ b/src/it/MCOMPILER-391-annotation-processors-dep-mgmt/annotation-processor/src/main/java/mcompiler391/SimpleAnnotationProcessor.java @@ -0,0 +1,75 @@ +/* + * 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); + + 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(); + } +} diff --git a/src/it/MCOMPILER-391-annotation-processors-dep-mgmt/annotation-user/pom.xml b/src/it/MCOMPILER-391-annotation-processors-dep-mgmt/annotation-user/pom.xml new file mode 100644 index 00000000..df092e80 --- /dev/null +++ b/src/it/MCOMPILER-391-annotation-processors-dep-mgmt/annotation-user/pom.xml @@ -0,0 +1,70 @@ + + + + + 4.0.0 + + + org.apache.maven.plugins.compiler.it + mcompiler391-test + 1.0.0-SNAPSHOT + + + mcompiler391-annotation-user + + + + + 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.SimpleObject + mcompiler391.SimpleTestObject + + + + + + + diff --git a/src/it/MCOMPILER-391-annotation-processors-dep-mgmt/annotation-user/src/main/java/mcompiler391/SimpleAnnotation.java b/src/it/MCOMPILER-391-annotation-processors-dep-mgmt/annotation-user/src/main/java/mcompiler391/SimpleAnnotation.java new file mode 100644 index 00000000..aeb88f98 --- /dev/null +++ b/src/it/MCOMPILER-391-annotation-processors-dep-mgmt/annotation-user/src/main/java/mcompiler391/SimpleAnnotation.java @@ -0,0 +1,28 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package 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 {} diff --git a/src/it/MCOMPILER-391-annotation-processors-dep-mgmt/annotation-user/src/main/java/mcompiler391/SimpleObject.java b/src/it/MCOMPILER-391-annotation-processors-dep-mgmt/annotation-user/src/main/java/mcompiler391/SimpleObject.java new file mode 100644 index 00000000..1c45a5e7 --- /dev/null +++ b/src/it/MCOMPILER-391-annotation-processors-dep-mgmt/annotation-user/src/main/java/mcompiler391/SimpleObject.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 +public class SimpleObject {} diff --git a/src/it/MCOMPILER-391-annotation-processors-dep-mgmt/annotation-user/src/test/java/mcompiler391/SimpleTestObject.java b/src/it/MCOMPILER-391-annotation-processors-dep-mgmt/annotation-user/src/test/java/mcompiler391/SimpleTestObject.java new file mode 100644 index 00000000..3ad60baa --- /dev/null +++ b/src/it/MCOMPILER-391-annotation-processors-dep-mgmt/annotation-user/src/test/java/mcompiler391/SimpleTestObject.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 +public class SimpleTestObject {} diff --git a/src/it/MCOMPILER-391-annotation-processors-dep-mgmt/invoker.properties b/src/it/MCOMPILER-391-annotation-processors-dep-mgmt/invoker.properties new file mode 100644 index 00000000..a0a3964f --- /dev/null +++ b/src/it/MCOMPILER-391-annotation-processors-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-annotation-processors-dep-mgmt/pom.xml b/src/it/MCOMPILER-391-annotation-processors-dep-mgmt/pom.xml new file mode 100644 index 00000000..4c3fc2dc --- /dev/null +++ b/src/it/MCOMPILER-391-annotation-processors-dep-mgmt/pom.xml @@ -0,0 +1,56 @@ + + + + + 4.0.0 + + org.apache.maven.plugins.compiler.it + mcompiler391-test + 1.0.0-SNAPSHOT + pom + + + annotation-processor + annotation-user + + + + + + org.apache.maven.plugins.compiler.it + mcompiler391-annotation-processor + 1.0.0-SNAPSHOT + + + + + + + + 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..3b0e414c 100644 --- a/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java +++ b/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java @@ -36,6 +36,8 @@ 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; @@ -1604,22 +1606,39 @@ private List resolveProcessorPathEntries() throws MojoExecutionException } } - private List convertToDependencies(List annotationProcessorPaths) { + 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();