Lex Luthra
05/28/2018, 1:38 AM// Groovy DSL
task buildDocker(type: Docker) {
baseImage = 'develar/java:latest'
push = project.hasProperty('push')
tag = 'geowarin/sout-chuck-norris'
addFile {
from jar
rename {'app.jar'}
}
entryPoint(['java', '-Djava.security.egd=file:/dev/./urandom', '-jar', '/app.jar'])
exposePort(8080)
}
buildDocker.dependsOn(build)
// Kotlin DSL
import se.transmode.gradle.plugins.docker.DockerTask
buildscript {
dependencies {
classpath("se.transmode.gradle:gradle-docker:1.2")
}
repositories {
jcenter()
}
}
tasks {
"buildDocker"(DockerTask::class) {
val appJarName = "app.jar"
dependsOn(tasks["build"])
baseImage = "develar/java:latest"
push = project.hasProperty("push")
tag = "geowarin/sout-chuck-norris"
addFile(closureOf<CopySpec> {
from(tasks["jar"] as CopySpec)
rename { appJarName }
})
entryPoint(mutableListOf("java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/$appJarName"))
exposePort(if (project.hasProperty("server.port")) project.property("server.port") as Int else 8080)
}
}
However, when I run the buildDocker task I receive the following error on the console:
What went wrong: Execution failed for task ':buildDocker'. se.transmode.gradle.plugins.docker.DockerTask$_createTarArchive_closure3_closure11_closure12 cannot be cast to org.gradle.api.file.CopySpec
I have worked around it by copying the file using doFirst and then invoking the form of addFile that accepts a path String instead of a Closure but would like to get to the bottom of this issue as it might help others in the future.Czar
05/28/2018, 6:53 AMJarTask
to CopySpec
, that can't work. Also from what I understand from(...)
expects not a task at all, but a directory or a file
Another problem is you're trying to make a closure into CopySpec
, which is printing the error you mention.
Take a look at this example of configuring copy: https://github.com/gradle/kotlin-dsl/blob/master/samples/copy/build.gradle.ktsLex Luthra
05/28/2018, 8:31 AMJar
is a Gradle task that implements `CopySpec`:
https://pasteboard.co/HneOMK0q.png▾
from
in the CopySpec to a with
but the build fails with the following error (same as before):
Caused by: java.lang.ClassCastException: se.transmode.gradle.plugins.docker.DockerTask$_createTarArchive_closure3_closure11_closure12 cannot be cast to org.gradle.api.file.CopySpec
at Build_gradle$$special$$inlined$invoke$lambda$4.invoke(build.gradle.kts:19)
at org.gradle.kotlin.dsl.KotlinClosure1.doCall(GroovyInteroperability.kt:93)
at se.transmode.gradle.plugins.docker.DockerTask$_createTarArchive_closure3_closure11_closure12.doCall(DockerTask.groovy:141)
The frame in question points to the following code in `DockerTask.groovy`:
https://github.com/Transmode/gradle-docker/blob/1.2/src/main/groovy/se/transmode/gradle/plugins/docker/DockerTask.groovy#L126-L151
The issue is likely to do with how Kotlin passes that copySpec
referenced in line 141 above.