Kapt: Support 'kapt.kotlin.generated' option in Maven

This commit is contained in:
Yan Zhulanow
2018-03-17 00:49:17 +03:00
parent b1d7935d4a
commit 39e9d28c63
9 changed files with 232 additions and 4 deletions
@@ -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;
}
@@ -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);
@@ -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());
}