Kapt, Maven: Clean directories for generated sources and stubs before AP
This commit is contained in:
+53
-7
@@ -24,7 +24,10 @@ import org.apache.maven.plugin.compiler.DependencyCoordinate;
|
|||||||
import org.apache.maven.plugins.annotations.*;
|
import org.apache.maven.plugins.annotations.*;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
import org.jetbrains.kotlin.cli.common.CLICompiler;
|
||||||
|
import org.jetbrains.kotlin.cli.common.ExitCode;
|
||||||
import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments;
|
import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments;
|
||||||
|
import org.jetbrains.kotlin.cli.common.messages.MessageCollector;
|
||||||
import org.jetbrains.kotlin.maven.K2JVMCompileMojo;
|
import org.jetbrains.kotlin.maven.K2JVMCompileMojo;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
@@ -99,7 +102,6 @@ public class KaptJVMCompilerMojo extends K2JVMCompileMojo {
|
|||||||
|
|
||||||
addKaptSourcesDirectory(sourcesDirectory.getPath());
|
addKaptSourcesDirectory(sourcesDirectory.getPath());
|
||||||
|
|
||||||
mkdirsSafe(sourcesDirectory);
|
|
||||||
mkdirsSafe(classesDirectory);
|
mkdirsSafe(classesDirectory);
|
||||||
mkdirsSafe(stubsDirectory);
|
mkdirsSafe(stubsDirectory);
|
||||||
|
|
||||||
@@ -110,14 +112,25 @@ public class KaptJVMCompilerMojo extends K2JVMCompileMojo {
|
|||||||
return options;
|
return options;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void addKaptSourcesDirectory(@NotNull String path) {
|
@NotNull
|
||||||
project.addCompileSourceRoot(path);
|
@Override
|
||||||
|
protected ExitCode execCompiler(
|
||||||
|
CLICompiler<K2JVMCompilerArguments> compiler,
|
||||||
|
MessageCollector messageCollector,
|
||||||
|
K2JVMCompilerArguments arguments,
|
||||||
|
List<File> sourceRoots
|
||||||
|
) throws MojoExecutionException {
|
||||||
|
// Annotation processing can't run incrementally so we need to clear the directory for our stubs and generated sources
|
||||||
|
// TODO separate directories for the generated class files, and recreate the generated classfile dir also
|
||||||
|
String sourceSetName = getSourceSetName();
|
||||||
|
recreateDirectorySafe(getGeneratedSourcesDirectory(project, sourceSetName));
|
||||||
|
recreateDirectorySafe(getStubsDirectory(project, sourceSetName));
|
||||||
|
|
||||||
|
return super.execCompiler(compiler, messageCollector, arguments, sourceRoots);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void mkdirsSafe(@NotNull File directory) {
|
protected void addKaptSourcesDirectory(@NotNull String path) {
|
||||||
if (!directory.mkdirs()) {
|
project.addCompileSourceRoot(path);
|
||||||
getLog().warn("Unable to create directory " + directory);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -187,4 +200,37 @@ public class KaptJVMCompilerMojo extends K2JVMCompileMojo {
|
|||||||
protected boolean isIncremental() {
|
protected boolean isIncremental() {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void mkdirsSafe(@NotNull File directory) {
|
||||||
|
if (!directory.isDirectory() && !directory.mkdirs()) {
|
||||||
|
getLog().warn("Unable to create directory " + directory);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void deleteRecursivelySafe(@NotNull File file) {
|
||||||
|
if (!file.exists()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (file.isDirectory()) {
|
||||||
|
File[] children = file.listFiles();
|
||||||
|
|
||||||
|
if (children != null) {
|
||||||
|
for (File child : children) {
|
||||||
|
deleteRecursivelySafe(child);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!file.delete()) {
|
||||||
|
getLog().warn("Unable to delete file " + file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void recreateDirectorySafe(@NotNull File file) {
|
||||||
|
if (file.exists()) {
|
||||||
|
deleteRecursivelySafe(file);
|
||||||
|
}
|
||||||
|
mkdirsSafe(file);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user