How to create a Gradle task of type KotlinCompile

Viewed 37

I'm trying to compile generated Kotlin source code in a custom location to a custom location so that I can build a jar file with those class file only.

I had no issues setting it up for Java. Unfortunately, I'm having problems with Kotlin.

So here is the Kotlin version of what worked for me in Java:

public class MyCustomPlugin implements Plugin<Project> {

    private static final String GENERATED_STUB_CLASSES_DIRECTORY = "generated-stub-classes";
    private static final String GENERATED_STUB_SOURCES_DIRECTORY = "generated-stub-sources";

    public void apply(Project project) {
        project.getPluginManager().apply(KotlinPluginWrapper.class);

        createCompileStubsTask(project);
    }

    private void createCompileStubsTask(final Project project) {
        KotlinCompile compileKotlin = (KotlinCompile) project.getRootProject().getTasksByName("compileKotlin", true).iterator().next();
        TaskProvider<KotlinCompile> compileKotlinStubs = project.getTasks().register("compileStubs", KotlinCompile.class,
                compileStubs -> {
                    File stubsClassesDir = new File(project.getBuildDir() + "/" + GENERATED_STUB_CLASSES_DIRECTORY);
                    stubsClassesDir.mkdirs();
                    compileStubs.setClasspath(compileKotlin.getClasspath());
                    compileStubs.source(project.getLayout().getBuildDirectory().dir(GENERATED_STUB_SOURCES_DIRECTORY));
                    compileStubs.getDestinationDirectory().set(stubsClassesDir);
                });
        compileKotlin.finalizedBy(compileKotlinStubs);
    }
}

This fails with:

Unable to determine constructor argument #1: missing parameter of type KotlinJvmOptions, or no service of type KotlinJvmOptions.

I tried to do it in the build.gradle file, like this:

task compileStubs(type: org.jetbrains.kotlin.gradle.tasks.KotlinCompile) {
    File stubsClassesDir = new File(project.getBuildDir().name + "/generated-stub-classes")
    stubsClassesDir.mkdirs()
    compileStubs.setClasspath(compileKotlin.getClasspath())
    compileStubs.source(project.getLayout().getBuildDirectory().dir("generated-stub-sources"))
    compileStubs.getDestinationDirectory().set(stubsClassesDir)
}
compileKotlin.finalizedBy(compileKotlinStubs)

But the result is exactly the same.

Please help...

0 Answers
Related