Copy task in gradle plugin using Kotlin

plugins

(Anindya Chatterjee) #1

I am trying to create a gradle plugin in buildSrc using kotlin. There I need to copy some generated jars to a specific folder.

I am trying to do this as follows:

open class PluginBuild : Plugin<Project> {
    override fun apply(project: Project?) {
        val pluginDir = File("${project?.rootDir}/plugins")
        if (!pluginDir.exists()) {
            pluginDir.mkdirs()
        }

        val jarDir = "${project?.buildDir}/libs"

        val pluginJar = project?.copySpec { copy ->
            copy.from(jarDir)
            copy.include("*.*")
        }

        project?.tasks?.create("copyPlugin", Copy::class.java) {
            println("Copying file from ${project.buildDir}/libs to $pluginDir")
            pluginJar?.into("$pluginDir")
        }

        project?.tasks?.findByPath("jar")?.dependsOn("copyPlugin")
    }
}

Unfortunately it is not copying the generated jar. The println line is only printing this

Configure project :darkside
Copying file from /media/anindya/data/codebase/letterbox/themes/darkside/build/libs to /media/anindya/data/codebase/letterbox/plugins

Looks like the task only running at config phase but I tried to add doLast also as follows

project?.tasks?.create("copyPlugin", Copy::class.java) {
    it.doLast {
        println("Copying file from ${project.buildDir}/libs to $pluginDir")
        pluginJar?.into("$pluginDir")
    }
}

But still it does not work. Any pointer would be great.