Kapt: Support 'kapt.kotlin.generated' option in Maven
This commit is contained in:
+58
@@ -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</artifactId>
|
||||
<version>1.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>
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. 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.appendln("package $packageName")
|
||||
it.appendln("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>()
|
||||
}
|
||||
+73
@@ -0,0 +1,73 @@
|
||||
<?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</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<junit.version>4.12</junit.version>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.kotlin.testkapt</groupId>
|
||||
<artifactId>annotation-processor</artifactId>
|
||||
<version>1.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</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</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>
|
||||
+8
@@ -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-generateKotlinCode</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<modules>
|
||||
<module>annotation-processor</module>
|
||||
<module>app</module>
|
||||
</modules>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
</project>
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
import java.io.*;
|
||||
|
||||
File file = new File(basedir, "app/target/app-1.0-SNAPSHOT.jar");
|
||||
if (!file.exists() || !file.isFile()) {
|
||||
throw new FileNotFoundException("Could not find generated JAR: " + file);
|
||||
}
|
||||
+6
-2
@@ -123,14 +123,18 @@ public class K2JVMCompileMojo extends KotlinCompileMojoBase<K2JVMCompilerArgumen
|
||||
|
||||
@Override
|
||||
protected List<String> getSourceFilePaths() {
|
||||
List<String> paths = super.getSourceFilePaths();
|
||||
List<String> paths = new ArrayList<>(super.getSourceFilePaths());
|
||||
|
||||
File sourcesDir = AnnotationProcessingManager.getGeneratedSourcesDirectory(project, getSourceSetName());
|
||||
if (sourcesDir.isDirectory()) {
|
||||
paths = new ArrayList<>(paths);
|
||||
paths.add(sourcesDir.getAbsolutePath());
|
||||
}
|
||||
|
||||
File kotlinSourcesDir = AnnotationProcessingManager.getGeneratedKotlinSourcesDirectory(project, getSourceSetName());
|
||||
if (kotlinSourcesDir.isDirectory()) {
|
||||
paths.add(kotlinSourcesDir.getAbsolutePath());
|
||||
}
|
||||
|
||||
return paths;
|
||||
}
|
||||
|
||||
|
||||
+5
@@ -77,6 +77,11 @@ public class AnnotationProcessingManager {
|
||||
return new File(getTargetDirectory(project), "generated-sources/kapt/" + sourceSetName);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static File getGeneratedKotlinSourcesDirectory(@NotNull MavenProject project, @NotNull String sourceSetName) {
|
||||
return new File(getTargetDirectory(project), "generated-sources/kaptKotlin/" + sourceSetName);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
static File getStubsDirectory(@NotNull MavenProject project, @NotNull String sourceSetName) {
|
||||
return new File(getTargetDirectory(project), "kaptStubs/" + sourceSetName);
|
||||
|
||||
+14
-2
@@ -110,20 +110,26 @@ public class KaptJVMCompilerMojo extends K2JVMCompileMojo {
|
||||
|
||||
String sourceSetName = getSourceSetName();
|
||||
File sourcesDirectory = getGeneratedSourcesDirectory(project, sourceSetName);
|
||||
File kotlinSourcesDirectory = getGeneratedKotlinSourcesDirectory(project, sourceSetName);
|
||||
File classesDirectory = getGeneratedClassesDirectory(project, sourceSetName);
|
||||
File stubsDirectory = getStubsDirectory(project, sourceSetName);
|
||||
|
||||
addKaptSourcesDirectory(sourcesDirectory.getPath());
|
||||
addKaptSourcesDirectory(kotlinSourcesDirectory.getPath());
|
||||
|
||||
mkdirsSafe(classesDirectory);
|
||||
mkdirsSafe(stubsDirectory);
|
||||
mkdirsSafe(kotlinSourcesDirectory);
|
||||
|
||||
options.add(new KaptOption("sources", sourcesDirectory.getAbsolutePath()));
|
||||
options.add(new KaptOption("classes", classesDirectory.getAbsolutePath()));
|
||||
options.add(new KaptOption("stubs", stubsDirectory.getAbsolutePath()));
|
||||
|
||||
options.add(new KaptOption("javacArguments", encodeOptionList(parseOptionList(javacOptions))));
|
||||
options.add(new KaptOption("apoptions", encodeOptionList(parseOptionList(annotationProcessorArgs))));
|
||||
|
||||
Map<String, String> allApOptions = parseOptionList(annotationProcessorArgs);
|
||||
allApOptions.put("kapt.kotlin.generated", kotlinSourcesDirectory.getAbsolutePath());
|
||||
options.add(new KaptOption("apoptions", encodeOptionList(allApOptions)));
|
||||
|
||||
return options;
|
||||
}
|
||||
@@ -141,6 +147,7 @@ public class KaptJVMCompilerMojo extends K2JVMCompileMojo {
|
||||
String sourceSetName = getSourceSetName();
|
||||
recreateDirectorySafe(getGeneratedSourcesDirectory(project, sourceSetName));
|
||||
recreateDirectorySafe(getStubsDirectory(project, sourceSetName));
|
||||
recreateDirectorySafe(getGeneratedKotlinSourcesDirectory(project, sourceSetName));
|
||||
|
||||
return super.execCompiler(compiler, messageCollector, arguments, sourceRoots);
|
||||
}
|
||||
@@ -148,10 +155,15 @@ public class KaptJVMCompilerMojo extends K2JVMCompileMojo {
|
||||
@Override
|
||||
protected List<String> getSourceFilePaths() {
|
||||
File generatedSourcesDirectory = getGeneratedSourcesDirectory(project, getSourceSetName());
|
||||
File generatedKotlinSourcesDirectory = getGeneratedKotlinSourcesDirectory(project, getSourceSetName());
|
||||
|
||||
return super.getSourceFilePaths()
|
||||
.stream()
|
||||
.filter(path -> !new File(path).equals(generatedSourcesDirectory))
|
||||
.filter(path -> {
|
||||
File pathFile = new File(path);
|
||||
return !pathFile.equals(generatedSourcesDirectory)
|
||||
&& !pathFile.equals(generatedKotlinSourcesDirectory);
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user