Skip to content

Commit 8cf1abc

Browse files
author
Aleksei.Cherepanov
committed
Add the possibility to omit explicitly specifying a version in annotationProcessorPaths
Resolve the version of the annotation processor path (specified in annotationProcessorPaths) in kapt plugin if it is already specified in dependencies of this module or parent module. Plugin management is also supported. #KT-59521 In Fixed Merge-request: KT-MR-10761 Merged-by: Aleksei Cherepanov <[email protected]> (cherry picked from commit a3583ea)
1 parent 4823fc3 commit 8cf1abc

File tree

7 files changed

+223
-1
lines changed

7 files changed

+223
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>org.jetbrains.kotlin.testkapt</groupId>
8+
<artifactId>annotation-processor-second-version</artifactId>
9+
<version>2.0-SNAPSHOT</version>
10+
11+
<dependencies>
12+
<dependency>
13+
<groupId>org.jetbrains.kotlin</groupId>
14+
<artifactId>kotlin-stdlib</artifactId>
15+
<version>${kotlin.version}</version>
16+
</dependency>
17+
</dependencies>
18+
19+
<build>
20+
<pluginManagement>
21+
<plugins>
22+
<plugin>
23+
<groupId>org.apache.maven.plugins</groupId>
24+
<artifactId>maven-compiler-plugin</artifactId>
25+
<version>2.3.2</version>
26+
<configuration>
27+
<source>1.6</source>
28+
<target>1.6</target>
29+
</configuration>
30+
</plugin>
31+
<plugin>
32+
<groupId>org.apache.maven.plugins</groupId>
33+
<artifactId>maven-source-plugin</artifactId>
34+
<version>2.1.2</version>
35+
</plugin>
36+
</plugins>
37+
</pluginManagement>
38+
39+
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
40+
<plugins>
41+
<plugin>
42+
<artifactId>kotlin-maven-plugin</artifactId>
43+
<groupId>org.jetbrains.kotlin</groupId>
44+
<version>${kotlin.version}</version>
45+
<executions>
46+
<execution>
47+
<id>compile</id>
48+
<phase>process-sources</phase>
49+
<goals>
50+
<goal>compile</goal>
51+
</goals>
52+
</execution>
53+
</executions>
54+
</plugin>
55+
</plugins>
56+
</build>
57+
58+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
3+
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
4+
*/
5+
6+
package example
7+
8+
import java.io.File
9+
import javax.annotation.processing.AbstractProcessor
10+
import javax.annotation.processing.RoundEnvironment
11+
import javax.lang.model.SourceVersion
12+
import javax.lang.model.element.ElementKind
13+
import javax.lang.model.element.TypeElement
14+
import javax.tools.Diagnostic
15+
import kotlin.reflect.KClass
16+
17+
annotation class Anno
18+
19+
class ExampleAnnotationProcessor : AbstractProcessor() {
20+
private companion object {
21+
val KAPT_KOTLIN_GENERATED_OPTION = "kapt.kotlin.generated"
22+
}
23+
24+
override fun process(annotations: MutableSet<out TypeElement>?, roundEnv: RoundEnvironment): Boolean {
25+
val elements = roundEnv.getElementsAnnotatedWith(Anno::class.java)
26+
val kotlinGenerated = processingEnv.options[KAPT_KOTLIN_GENERATED_OPTION]
27+
28+
for (element in elements) {
29+
val packageName = processingEnv.elementUtils.getPackageOf(element).qualifiedName.toString()
30+
val simpleName = element.simpleName.toString()
31+
32+
if (kotlinGenerated != null && element.kind == ElementKind.CLASS) {
33+
File(kotlinGenerated, "$simpleName.kt").writer().buffered().use {
34+
it.appendLine("package $packageName")
35+
it.appendLine("fun $simpleName.customToString() = \"$simpleName: \" + toString()")
36+
}
37+
}
38+
}
39+
40+
return true
41+
}
42+
43+
override fun getSupportedSourceVersion() = SourceVersion.RELEASE_6
44+
override fun getSupportedAnnotationTypes() = setOf(Anno::class.java.name)
45+
override fun getSupportedOptions() = emptySet<String>()
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
4+
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
5+
6+
<modelVersion>4.0.0</modelVersion>
7+
8+
<groupId>org.jetbrains.kotlin.testkapt</groupId>
9+
<artifactId>app-with-kapt</artifactId>
10+
<version>1.0-SNAPSHOT</version>
11+
12+
<properties>
13+
<junit.version>4.13.1</junit.version>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>org.jetbrains.kotlin.testkapt</groupId>
20+
<artifactId>annotation-processor-second-version</artifactId>
21+
<version>2.0-SNAPSHOT</version>
22+
</dependency>
23+
<dependency>
24+
<groupId>org.jetbrains.kotlin</groupId>
25+
<artifactId>kotlin-stdlib</artifactId>
26+
<version>${kotlin.version}</version>
27+
</dependency>
28+
</dependencies>
29+
30+
<build>
31+
<plugins>
32+
<plugin>
33+
<artifactId>kotlin-maven-plugin</artifactId>
34+
<groupId>org.jetbrains.kotlin</groupId>
35+
<version>${kotlin.version}</version>
36+
<executions>
37+
<execution>
38+
<id>kapt</id>
39+
<goals>
40+
<goal>kapt</goal>
41+
</goals>
42+
<configuration>
43+
<sourceDirs>
44+
<sourceDir>src/main/kotlin</sourceDir>
45+
</sourceDirs>
46+
<annotationProcessors>
47+
<annotationProcessor>example.ExampleAnnotationProcessor</annotationProcessor>
48+
</annotationProcessors>
49+
<annotationProcessorPaths>
50+
<annotationProcessorPath>
51+
<groupId>org.jetbrains.kotlin.testkapt</groupId>
52+
<artifactId>annotation-processor-second-version</artifactId>
53+
</annotationProcessorPath>
54+
</annotationProcessorPaths>
55+
</configuration>
56+
</execution>
57+
<execution>
58+
<id>compile</id>
59+
<goals>
60+
<goal>compile</goal>
61+
</goals>
62+
<configuration>
63+
<sourceDirs>
64+
<sourceDir>src/main/kotlin</sourceDir>
65+
</sourceDirs>
66+
</configuration>
67+
</execution>
68+
</executions>
69+
</plugin>
70+
</plugins>
71+
</build>
72+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package test
2+
3+
@example.Anno
4+
class MyClass {
5+
fun test() {
6+
println(this.customToString())
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>org.jetbrains.kotlin.testkapt</groupId>
8+
<artifactId>test-kapt-annotationProcessorPaths-without-version</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
<modules>
11+
<module>annotation-processor-second-version</module>
12+
<module>app-with-kapt</module>
13+
</modules>
14+
<packaging>pom</packaging>
15+
16+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import java.io.*;
2+
3+
File file = new File(basedir, "app-with-kapt/target/app-with-kapt-1.0-SNAPSHOT.jar");
4+
if (!file.exists() || !file.isFile()) {
5+
throw new FileNotFoundException("Could not find generated JAR: " + file);
6+
}

libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/kapt/AnnotationProcessingManager.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.apache.maven.artifact.resolver.ResolutionErrorHandler;
2626
import org.apache.maven.artifact.versioning.VersionRange;
2727
import org.apache.maven.execution.MavenSession;
28+
import org.apache.maven.model.Dependency;
2829
import org.apache.maven.plugin.MojoExecutionException;
2930
import org.apache.maven.plugin.compiler.DependencyCoordinate;
3031
import org.apache.maven.project.MavenProject;
@@ -167,14 +168,29 @@ private Artifact getArtifact(
167168
return new DefaultArtifact(
168169
dependency.getGroupId(),
169170
dependency.getArtifactId(),
170-
VersionRange.createFromVersionSpec(dependency.getVersion()),
171+
VersionRange.createFromVersionSpec(evaluateVersion(dependency)),
171172
Artifact.SCOPE_RUNTIME,
172173
dependency.getType(),
173174
dependency.getClassifier(),
174175
handler,
175176
false);
176177
}
177178

179+
private String evaluateVersion(@NotNull DependencyCoordinate dependency) {
180+
String version = dependency.getVersion();
181+
if (version == null) {
182+
Optional<Dependency> sameButParentDependency = project.getDependencies().stream()
183+
.filter(dep -> dep.getGroupId().equals(dependency.getGroupId())
184+
&& dep.getArtifactId().equals(dependency.getArtifactId())
185+
&& dep.getVersion() != null
186+
).findFirst();
187+
if (sameButParentDependency.isPresent()) {
188+
version = sameButParentDependency.get().getVersion();
189+
}
190+
}
191+
return version;
192+
}
193+
178194
@NotNull
179195
private static String getMavenPluginVersion() throws MojoExecutionException {
180196
ClassLoader classLoader = AnnotationProcessingManager.class.getClassLoader();

0 commit comments

Comments
 (0)