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 <aleksei.cherepanov@jetbrains.com>
This commit is contained in:
Aleksei.Cherepanov
2023-06-22 17:13:24 +00:00
committed by Space Team
parent e4d1e6246e
commit a3583ea79b
7 changed files with 223 additions and 1 deletions
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="UTF-8"?>
<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.jetbrains.kotlin.testkapt</groupId>
<artifactId>annotation-processor-second-version</artifactId>
<version>2.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.1.2</version>
</plugin>
</plugins>
</pluginManagement>
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<plugins>
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>process-sources</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,46 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package example
import java.io.File
import javax.annotation.processing.AbstractProcessor
import javax.annotation.processing.RoundEnvironment
import javax.lang.model.SourceVersion
import javax.lang.model.element.ElementKind
import javax.lang.model.element.TypeElement
import javax.tools.Diagnostic
import kotlin.reflect.KClass
annotation class Anno
class ExampleAnnotationProcessor : AbstractProcessor() {
private companion object {
val KAPT_KOTLIN_GENERATED_OPTION = "kapt.kotlin.generated"
}
override fun process(annotations: MutableSet<out TypeElement>?, roundEnv: RoundEnvironment): Boolean {
val elements = roundEnv.getElementsAnnotatedWith(Anno::class.java)
val kotlinGenerated = processingEnv.options[KAPT_KOTLIN_GENERATED_OPTION]
for (element in elements) {
val packageName = processingEnv.elementUtils.getPackageOf(element).qualifiedName.toString()
val simpleName = element.simpleName.toString()
if (kotlinGenerated != null && element.kind == ElementKind.CLASS) {
File(kotlinGenerated, "$simpleName.kt").writer().buffered().use {
it.appendLine("package $packageName")
it.appendLine("fun $simpleName.customToString() = \"$simpleName: \" + toString()")
}
}
}
return true
}
override fun getSupportedSourceVersion() = SourceVersion.RELEASE_6
override fun getSupportedAnnotationTypes() = setOf(Anno::class.java.name)
override fun getSupportedOptions() = emptySet<String>()
}
@@ -0,0 +1,72 @@
<?xml version="1.0" encoding="UTF-8"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>org.jetbrains.kotlin.testkapt</groupId>
<artifactId>app-with-kapt</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<junit.version>4.13.1</junit.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin.testkapt</groupId>
<artifactId>annotation-processor-second-version</artifactId>
<version>2.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>kapt</id>
<goals>
<goal>kapt</goal>
</goals>
<configuration>
<sourceDirs>
<sourceDir>src/main/kotlin</sourceDir>
</sourceDirs>
<annotationProcessors>
<annotationProcessor>example.ExampleAnnotationProcessor</annotationProcessor>
</annotationProcessors>
<annotationProcessorPaths>
<annotationProcessorPath>
<groupId>org.jetbrains.kotlin.testkapt</groupId>
<artifactId>annotation-processor-second-version</artifactId>
</annotationProcessorPath>
</annotationProcessorPaths>
</configuration>
</execution>
<execution>
<id>compile</id>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<sourceDirs>
<sourceDir>src/main/kotlin</sourceDir>
</sourceDirs>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,8 @@
package test
@example.Anno
class MyClass {
fun test() {
println(this.customToString())
}
}
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<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.jetbrains.kotlin.testkapt</groupId>
<artifactId>test-kapt-annotationProcessorPaths-without-version</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>annotation-processor-second-version</module>
<module>app-with-kapt</module>
</modules>
<packaging>pom</packaging>
</project>
@@ -0,0 +1,6 @@
import java.io.*;
File file = new File(basedir, "app-with-kapt/target/app-with-kapt-1.0-SNAPSHOT.jar");
if (!file.exists() || !file.isFile()) {
throw new FileNotFoundException("Could not find generated JAR: " + file);
}
@@ -25,6 +25,7 @@ import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
import org.apache.maven.artifact.resolver.ResolutionErrorHandler;
import org.apache.maven.artifact.versioning.VersionRange;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Dependency;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.compiler.DependencyCoordinate;
import org.apache.maven.project.MavenProject;
@@ -167,7 +168,7 @@ public class AnnotationProcessingManager {
return new DefaultArtifact(
dependency.getGroupId(),
dependency.getArtifactId(),
VersionRange.createFromVersionSpec(dependency.getVersion()),
VersionRange.createFromVersionSpec(evaluateVersion(dependency)),
Artifact.SCOPE_RUNTIME,
dependency.getType(),
dependency.getClassifier(),
@@ -175,6 +176,21 @@ public class AnnotationProcessingManager {
false);
}
private String evaluateVersion(@NotNull DependencyCoordinate dependency) {
String version = dependency.getVersion();
if (version == null) {
Optional<Dependency> sameButParentDependency = project.getDependencies().stream()
.filter(dep -> dep.getGroupId().equals(dependency.getGroupId())
&& dep.getArtifactId().equals(dependency.getArtifactId())
&& dep.getVersion() != null
).findFirst();
if (sameButParentDependency.isPresent()) {
version = sameButParentDependency.get().getVersion();
}
}
return version;
}
@NotNull
private static String getMavenPluginVersion() throws MojoExecutionException {
ClassLoader classLoader = AnnotationProcessingManager.class.getClassLoader();