Skip to content

Commit 9372fae

Browse files
committed
[MCOMPILER-391] Get annotation processor version from dependencyManagement
1 parent b225b1c commit 9372fae

File tree

9 files changed

+346
-2
lines changed

9 files changed

+346
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Licensed to the Apache Software Foundation (ASF) under one
4+
~ or more contributor license agreements. See the NOTICE file
5+
~ distributed with this work for additional information
6+
~ regarding copyright ownership. The ASF licenses this file
7+
~ to you under the Apache License, Version 2.0 (the
8+
~ "License"); you may not use this file except in compliance
9+
~ with the License. You may obtain a copy of the License at
10+
~
11+
~ http://www.apache.org/licenses/LICENSE-2.0
12+
~
13+
~ Unless required by applicable law or agreed to in writing,
14+
~ software distributed under the License is distributed on an
15+
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
~ KIND, either express or implied. See the License for the
17+
~ specific language governing permissions and limitations
18+
~ under the License.
19+
-->
20+
21+
<project xmlns="http://maven.apache.org/POM/4.0.0"
22+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
23+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
24+
<modelVersion>4.0.0</modelVersion>
25+
26+
<parent>
27+
<groupId>org.apache.maven.plugins.compiler.it</groupId>
28+
<artifactId>mcompiler391-test</artifactId>
29+
<version>1.0.0-SNAPSHOT</version>
30+
</parent>
31+
32+
<artifactId>mcompiler391-annotation-processor</artifactId>
33+
34+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package mcompiler391;
20+
21+
import javax.annotation.processing.AbstractProcessor;
22+
import javax.annotation.processing.Filer;
23+
import javax.annotation.processing.RoundEnvironment;
24+
import javax.annotation.processing.SupportedAnnotationTypes;
25+
import javax.annotation.processing.SupportedSourceVersion;
26+
import javax.lang.model.SourceVersion;
27+
import javax.lang.model.element.Element;
28+
import javax.lang.model.element.Name;
29+
import javax.lang.model.element.PackageElement;
30+
import javax.lang.model.element.TypeElement;
31+
import javax.lang.model.util.Elements;
32+
import javax.tools.FileObject;
33+
import javax.tools.StandardLocation;
34+
35+
import java.io.IOException;
36+
import java.io.Writer;
37+
import java.util.Set;
38+
39+
@SupportedSourceVersion(SourceVersion.RELEASE_6)
40+
@SupportedAnnotationTypes("mcompiler391.SimpleAnnotation")
41+
public class SimpleAnnotationProcessor extends AbstractProcessor {
42+
43+
@Override
44+
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
45+
if (annotations.isEmpty()) {
46+
return true;
47+
}
48+
49+
Filer filer = processingEnv.getFiler();
50+
51+
Elements elementUtils = processingEnv.getElementUtils();
52+
53+
Set<? extends Element> elements =
54+
roundEnv.getElementsAnnotatedWith(annotations.iterator().next());
55+
56+
for (Element element : elements) {
57+
Name name = element.getSimpleName();
58+
59+
PackageElement packageElement = elementUtils.getPackageOf(element);
60+
61+
try {
62+
Name packageName = packageElement.getQualifiedName();
63+
FileObject resource =
64+
filer.createResource(StandardLocation.SOURCE_OUTPUT, packageName, name + ".txt", element);
65+
66+
Writer writer = resource.openWriter();
67+
writer.write(name.toString());
68+
writer.close();
69+
} catch (IOException e) {
70+
throw new RuntimeException(e);
71+
}
72+
}
73+
return !elements.isEmpty();
74+
}
75+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Licensed to the Apache Software Foundation (ASF) under one
4+
~ or more contributor license agreements. See the NOTICE file
5+
~ distributed with this work for additional information
6+
~ regarding copyright ownership. The ASF licenses this file
7+
~ to you under the Apache License, Version 2.0 (the
8+
~ "License"); you may not use this file except in compliance
9+
~ with the License. You may obtain a copy of the License at
10+
~
11+
~ http://www.apache.org/licenses/LICENSE-2.0
12+
~
13+
~ Unless required by applicable law or agreed to in writing,
14+
~ software distributed under the License is distributed on an
15+
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
~ KIND, either express or implied. See the License for the
17+
~ specific language governing permissions and limitations
18+
~ under the License.
19+
-->
20+
21+
<project xmlns="http://maven.apache.org/POM/4.0.0"
22+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
23+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
24+
<modelVersion>4.0.0</modelVersion>
25+
26+
<parent>
27+
<groupId>org.apache.maven.plugins.compiler.it</groupId>
28+
<artifactId>mcompiler391-test</artifactId>
29+
<version>1.0.0-SNAPSHOT</version>
30+
</parent>
31+
32+
<artifactId>mcompiler391-annotation-user</artifactId>
33+
34+
<build>
35+
<plugins>
36+
<plugin>
37+
<artifactId>maven-compiler-plugin</artifactId>
38+
<configuration>
39+
<annotationProcessors>
40+
<annotationProcessor>mcompiler391.SimpleAnnotationProcessor</annotationProcessor>
41+
</annotationProcessors>
42+
<annotationProcessorPaths>
43+
<path>
44+
<groupId>org.apache.maven.plugins.compiler.it</groupId>
45+
<artifactId>mcompiler391-annotation-processor</artifactId>
46+
</path>
47+
</annotationProcessorPaths>
48+
</configuration>
49+
</plugin>
50+
<plugin>
51+
<groupId>org.apache.maven.plugins.compiler.it</groupId>
52+
<artifactId>annotation-verify-plugin</artifactId>
53+
<version>1.0.0-SNAPSHOT</version>
54+
<executions>
55+
<execution>
56+
<id>verify-annotations</id>
57+
<phase>process-test-classes</phase>
58+
<goals>
59+
<goal>read-source</goal>
60+
</goals>
61+
<configuration>
62+
<sourceClass>mcompiler391.SimpleObject</sourceClass>
63+
<testSourceClass>mcompiler391.SimpleTestObject</testSourceClass>
64+
</configuration>
65+
</execution>
66+
</executions>
67+
</plugin>
68+
</plugins>
69+
</build>
70+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package mcompiler391;
20+
21+
import java.lang.annotation.ElementType;
22+
import java.lang.annotation.Retention;
23+
import java.lang.annotation.RetentionPolicy;
24+
import java.lang.annotation.Target;
25+
26+
@Target(ElementType.TYPE)
27+
@Retention(RetentionPolicy.SOURCE)
28+
public @interface SimpleAnnotation {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package mcompiler391;
20+
21+
@SimpleAnnotation
22+
public class SimpleObject {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package mcompiler391;
20+
21+
@SimpleAnnotation
22+
public class SimpleTestObject {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
invoker.goals=process-test-classes
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Licensed to the Apache Software Foundation (ASF) under one
4+
~ or more contributor license agreements. See the NOTICE file
5+
~ distributed with this work for additional information
6+
~ regarding copyright ownership. The ASF licenses this file
7+
~ to you under the Apache License, Version 2.0 (the
8+
~ "License"); you may not use this file except in compliance
9+
~ with the License. You may obtain a copy of the License at
10+
~
11+
~ http://www.apache.org/licenses/LICENSE-2.0
12+
~
13+
~ Unless required by applicable law or agreed to in writing,
14+
~ software distributed under the License is distributed on an
15+
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
~ KIND, either express or implied. See the License for the
17+
~ specific language governing permissions and limitations
18+
~ under the License.
19+
-->
20+
21+
<project xmlns="http://maven.apache.org/POM/4.0.0"
22+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
23+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
24+
<modelVersion>4.0.0</modelVersion>
25+
26+
<groupId>org.apache.maven.plugins.compiler.it</groupId>
27+
<artifactId>mcompiler391-test</artifactId>
28+
<version>1.0.0-SNAPSHOT</version>
29+
<packaging>pom</packaging>
30+
31+
<modules>
32+
<module>annotation-processor</module>
33+
<module>annotation-user</module>
34+
</modules>
35+
36+
<dependencyManagement>
37+
<dependencies>
38+
<dependency>
39+
<groupId>org.apache.maven.plugins.compiler.it</groupId>
40+
<artifactId>mcompiler391-annotation-processor</artifactId>
41+
<version>1.0.0-SNAPSHOT</version>
42+
</dependency>
43+
</dependencies>
44+
</dependencyManagement>
45+
<build>
46+
<pluginManagement>
47+
<plugins>
48+
<plugin>
49+
<groupId>org.apache.maven.plugins</groupId>
50+
<artifactId>maven-compiler-plugin</artifactId>
51+
<version>@project.version@</version>
52+
</plugin>
53+
</plugins>
54+
</pluginManagement>
55+
</build>
56+
</project>

src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java

+21-2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
import java.util.LinkedHashSet;
3737
import java.util.List;
3838
import java.util.Map;
39+
import java.util.Objects;
40+
import java.util.Optional;
3941
import java.util.Properties;
4042
import java.util.Set;
4143

@@ -1604,22 +1606,39 @@ private List<String> resolveProcessorPathEntries() throws MojoExecutionException
16041606
}
16051607
}
16061608

1607-
private List<Dependency> convertToDependencies(List<DependencyCoordinate> annotationProcessorPaths) {
1609+
private List<Dependency> convertToDependencies(List<DependencyCoordinate> annotationProcessorPaths)
1610+
throws MojoExecutionException {
16081611
List<Dependency> dependencies = new ArrayList<>();
16091612
for (DependencyCoordinate annotationProcessorPath : annotationProcessorPaths) {
16101613
ArtifactHandler handler = artifactHandlerManager.getArtifactHandler(annotationProcessorPath.getType());
1614+
String version = annotationProcessorPath.getVersion();
1615+
if (version == null) {
1616+
version = findManagedVersion(annotationProcessorPath)
1617+
.orElseThrow(
1618+
() -> new MojoExecutionException("Cannot find version for " + annotationProcessorPath));
1619+
}
16111620
Artifact artifact = new DefaultArtifact(
16121621
annotationProcessorPath.getGroupId(),
16131622
annotationProcessorPath.getArtifactId(),
16141623
annotationProcessorPath.getClassifier(),
16151624
handler.getExtension(),
1616-
annotationProcessorPath.getVersion());
1625+
version);
16171626
Set<Exclusion> exclusions = convertToAetherExclusions(annotationProcessorPath.getExclusions());
16181627
dependencies.add(new Dependency(artifact, JavaScopes.RUNTIME, false, exclusions));
16191628
}
16201629
return dependencies;
16211630
}
16221631

1632+
private Optional<String> findManagedVersion(DependencyCoordinate dependencyCoordinate) {
1633+
return project.getDependencyManagement().getDependencies().stream()
1634+
.filter(dep -> Objects.equals(dep.getGroupId(), dependencyCoordinate.getGroupId())
1635+
&& Objects.equals(dep.getArtifactId(), dependencyCoordinate.getArtifactId())
1636+
&& Objects.equals(dep.getClassifier(), dependencyCoordinate.getClassifier())
1637+
&& Objects.equals(dep.getType(), dependencyCoordinate.getType()))
1638+
.findAny()
1639+
.map(org.apache.maven.model.Dependency::getVersion);
1640+
}
1641+
16231642
private Set<Exclusion> convertToAetherExclusions(Set<DependencyExclusion> exclusions) {
16241643
if (exclusions == null || exclusions.isEmpty()) {
16251644
return Collections.emptySet();

0 commit comments

Comments
 (0)