How to pass command line argument to Gradle Kotlin DSL - kotlin

Here's an example from Groovy that represents exactly what I would like to achieve:
Command line:
./gradlew jib -PmyArg=hello
build.gradle.kts
task myTask {
doFirst {
println myArg
... do what you want
}
}
Source of this example is here - option 3.
How can I read pass and read myArg value in Kotlin DSL ?

After some time found an answer:
build.gradle.kts
val myArg: String by project // Command line argument is always a part of project
task("myTask") {
doFirst {
if (project.hasProperty("myArg")) {
println(myArg)
}
}
}
Command line:
gradle myTask -PmyArg=foo
Output:
$ gradle myTask -PmyArg=foo
> Task :myTask
foo
BUILD SUCCESSFUL in 1s
1 actionable task: 1 executed
Related links:
How to pass arguments from command line to gradle
How to pass parameters or arguments into a gradle task
Gradle task check if property is defined

I retrieved the argument for my task like this (build.gradle.kts with Kotlin DSL):
tasks.create("myCustomTask") {
doLast {
val myArg = properties["myArgName"]
// OR a more verbose form:
val myArg = project.properties["myArgName"]
}
}
./gradlew myCustomTask -PmyArgName=hello

Related

Calling PCEnhancerTask from Kotlin in Gradle

I need to call the OpenJPA PCEnhancerTask class from Kotlin instead of Groovy. The following code works just fine (based on a previous solution documented here):
def openJPAClosure = {
def entityFiles = sourceSets.main.output.classesDirs.asFileTree.matching {
include 'com/company/persist/*Entity.class'
}
println "Enhancing with OpenJPA:"
entityFiles.getFiles().each {
println it
}
ant.taskdef(
name : 'openjpac',
classpath : sourceSets.main.runtimeClasspath.asPath,
classname : 'org.apache.openjpa.ant.PCEnhancerTask'
)
ant.openjpac(
classpath: sourceSets.main.runtimeClasspath.asPath,
addDefaultConstructor: false,
enforcePropertyRestrictions: true) {
entityFiles.addToAntBuilder(ant, 'fileset', FileCollection.AntType.FileSet)
}
}
I was looking at the documentation on how to call Ant tasks from Gradle but I could not translate all the necessary steps using the GroovyBuilder. So instead I tough of calling the PCEnhancer directly:
fun openJPAEnrich() {
val entityFiles = sourceSets.main.get().output.classesDirs.asFileTree.matching {
include("com/company/persist/*Entity.class")
}
println("Enhancing with OpenJPA, the following files...")
entityFiles.getFiles().forEach() {
println(it)
}
org.apache.openjpa.ant.PCEnhancerTask.main(asList(entityFiles))
}
But it complains about not being able to find org.apache.openjpa in the classpath (but is it listed as a compilation dependency)
My questions are:
What is the correct way to translate the original Groovy construct to Kotlin using groovyBuilder
If is not possible, how you can correctly call PCEnhancer from Kotlin in Gradle?
So I ended making it work with a custom JavaExec Gradle task:
tasks.create<JavaExec>("openJPAEnrich") {
val entityFiles = sourceSets.main.get().output.classesDirs.asFileTree.matching {
include("com/company/persist/*Entity.class")
}
println("Enhancing with OpenJPA, the following files...")
entityFiles.files.forEach() {
println(it)
}
classpath = sourceSets.main.get().runtimeClasspath
main = "org.apache.openjpa.enhance.PCEnhancer"
args(listOf("-enforcePropertyRestrictions", "true", "-addDefaultConstructor", "false"))
entityFiles.forEach { classFile -> args?.add(classFile.toString())}
}
I was tempted to build my own custom Gradle task but for this felt overkill.
Thanks.
--Jose

How to process Kotlin code in Gradle KTS?

I have a package in my app and I'd like to go over all classes in that package to then generate some JSON schema automatically.
I'd like to create a gradle task and with some sort of build-time dependency would allow me to do:
tasks.register("my fancy task") {
doLast {
"my.package.name".readKotlinFiles().classes.forEach { klass ->
klass.properties["id"]... and do something here
}
}
}
How can you do such thing easily?
I was doing something similar, loading classes form the project, but not as a Gradle task or plugin but in Java application itself. To execute this logic as Gradle task / plugin I would do it in the following way:
First you generate jar package from your Kotlin project as a standard process of Kotlin-Gradle build.
As Gradle build is not a part of your project source code you have to load classes of the actual project with properly configured ClassLoader.
Then you can read those classes and generate the report.
Now that would translate into the pseudocode of a Gradle task:
tasks.register("processClasses") {
dependsOn tasks.named("assemble") // generate jar
doLast {
// Initiate classloader of assembled project
// Read target package from gradle properties or other kind of parameters
// Get list of all classes under the specific package
// Load the list of classes from classloader
// Execute custom logic upon the list of classes
// Generate report in specific format (JSON, YAML, HTML, ...)
}
}
I tried to validate the logic and created a POC as the following GitHub project. There are two flavors of build scripts:
Groovy build script is in master branch
Kotlin build script version is in kotlin branch. Note multiple
aspects should be improved and adapted to the specific goal you have
in mind (really curious actually). You are welcome to make any
suggestions, fork the code, do changes..
Excerpt of the code below:
// Pack all the dependencies into jar
tasks.jar {
from(configurations.runtimeClasspath.map { configuration ->
configuration.asFileTree.fold(files().asFileTree) { collection, file ->
if (file.isDirectory) collection else collection.plus(zipTree(file))
}
})
}
val packageToProcess: String by project
tasks.register("processClasses") {
group = "process"
description = "Process classes form a specific package"
dependsOn(tasks.named("assemble"))
doLast {
// Instantiate classloader from jar as you will need other dependencies for loading classes
val file: File = project.projectDir.toPath().resolve(tasks.jar.get().archiveFile.get().toString()).toFile()
println("Packaged Kotlin project jar file: $file")
val classloader: ClassLoader = URLClassLoader(arrayOf(file.toURI().toURL()))
// Iterate through all of the class files in the specified package
val packageToScan: String = packageToProcess.replace(".", File.separator)
val path: java.nio.file.Path = project.file("build/classes/kotlin/main/").toPath().resolve(packageToScan)
val classesFromSpecificPackage = Files.walk(path)
.filter {
Files.isRegularFile(it)
}
.map {
project.file("build/classes/kotlin/main/").toPath().relativize(it)
}
.map { it.toString().substring(0, it.toString().lastIndexOf(".")).replace(File.separator, ".") }
.map {
//println("Class = $it") // print class if necessary before loading it
classloader.loadClass(it)
}
// Do something with the classes form specific package
// Now just basic information is printed directly to the console
println("=======================================")
println("Classes from package: $packageToScan")
println("")
classesFromSpecificPackage.forEach {
println(" Class: ${it.canonicalName}")
val fields = it.declaredFields
for (element in fields) {
println(" - Declared field: $element")
}
println("")
}
println("=======================================")
}
}

Converting Groovy tasks build.gradle to kotlin kts file

I am trying to convert my build.gradle file in Groovy to a kotlin .kts file, I have some tasks in the gradle file that I dont know how to convert correctly
task androidJavadocs(type: Javadoc) {
failOnError = false
source = android.sourceSets.main.java.srcDirs
ext.androidJar = "${android.sdkDirectory}/platforms/${android.compileSdkVersion}/android.jar"
classpath += files(ext.androidJar)
exclude '**/R.html', '**/R.*.html', '**/index.html'
}
task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) {
classifier = 'javadoc'
from androidJavadocs.destinationDir
}
I dont quite understand how to use the "type", I was going down this path
val androidJavadocs by tasks.withType<Javadoc>(){
}
but its giving me the error
Type 'DomainObjectCollection<Javadoc!>' has no method
'getValue(Build_gradle, KProperty<*>)' and thus it cannot serve as a
delegate
How do I convert those groovy tasks to kotlin tasks correctly?
You can use the named() method to configure existing tasks and the register() method to create new ones.
tasks {
register<Javadoc>("androidJavadocs") {
// Task's body here
}
}
// or
tasks.register<Javadoc>("androidJavadocs") {
// Task's body here
}
If you want to use Kotlin delegated properties
val androidJavadocs by tasks.registering(Javadoc::class) {
// Task's body here
}
The full answer to your question
val androidJavadocs by tasks.registering(Javadoc::class) {
isFailOnError = false
source = android.sourceSets.main.java.srcDirs
ext["androidJar"] = "${android.sdkDirectory}/platforms/${android.compileSdkVersion}/android.jar")
classpath += files(ext["androidJar"])
exclude("**/R.html", "**/R.*.html", "**/index.html")
}
val androidJavadocsJar by tasks.registering(Jar::class) {
classifier = "javadoc"
dependsOn(androidJavadocs)
from(androidJavadocs.get().destinationDir)
}

How to add task dependencies in Gradle?

I'm trying to write a task named stage like this:
plugins {
base
}
val clean = "clean"
val build = "build"
tasks.register("stage") {
dependsOn(clean, build)
}
tasks[build].dependsOn(clean)
The problem is that when I run ./gradlew stage it doesn't run clean, nor build. This is a multiproject build and I have 3 subprojects with Kotlin code.
How do I get Gradle to run ./gradlew clean build whenever I type ./gradlew stage? This is clearly not a solution, and I don't see what I'm doing wrong.
I also tried this, but it didn't work either. It runs the tasks I want but at the end it runs clean and I end up with no build folder:
tasks.register("stage") {
subprojects.forEach { project ->
val clean = project.tasks.first { it.name.contains("clean") }
val build = project.tasks.first { it.name.contains("build") }
build.dependsOn(clean)
dependsOn(build)
}
}
I think one of the issues in your code is running the command:
build.dependsOn(clean)
Instead try:
tasks{
register("stage"){
dependsOn(clean, build)
}
}
Remember that tasks{} is a container and by scoping the code inside we have access to the other tasks as well.
You likely need to define the ordering using mustRunAfter
tasks.register("stage") {
subprojects.forEach { project ->
val clean = project.tasks.first { it.name.contains("clean") }
val build = project.tasks.first { it.name.contains("build") }
build.dependsOn(clean)
build.mustRunAfter(clean)
dependsOn(build)
}
}
From the docs of Task Dependencies and Task Ordering:
Dependencies to a task are controlled using Task.dependsOn(java.lang.Object[]) or Task.setDependsOn(java.lang.Iterable), and Task.mustRunAfter(java.lang.Object[]), Task.setMustRunAfter(java.lang.Iterable), Task.shouldRunAfter(java.lang.Object[]) and Task.setShouldRunAfter(java.lang.Iterable) are used to specify ordering between tasks.
This documentation in Gradle about ordering tasks can explain tasks ordering, and why you need a combination of dependsOn and mustRunAfter for your use case:
Note that “B.mustRunAfter(A)” or “B.shouldRunAfter(A)” does not imply any execution dependency between the tasks:
It is possible to execute tasks A and B independently. The ordering rule only has an effect when both tasks are scheduled for execution.
its much simpler
plugins {
base
}
allprojects {
task stage() {
dependsOn(clean, build)
}
}
so when you run stage - it should do clean and build

How to run a command line command with Kotlin DSL in Gradle 6.1.1?

I am trying to run the code block below, after reading multiple posts on the topic and the Gradle manual. I run the below and get the following error: execCommand == null!
Any ideas on what I am doing wrong with the below code block?
open class BuildDataClassFromAvro : org.gradle.api.tasks.Exec() {
#TaskAction
fun build() {
println("Building data classes.....")
commandLine("date")
}
}
tasks.register<BuildDataClassFromAvro>("buildFromAvro") {
description = "Do stuff"
}
To define a Gradle task that runs a command-line using the Gradle Kotlin DSL do something like this in your build file:
task<Exec>("buildFromAvro") {
commandLine("echo", "test")
}
In the example above the commandLine will simply run echo, outputting the value test. So replace that with whatever you want to actually do.
You can then run that with gradle buildFromAvro
More info here: https://docs.gradle.org/current/dsl/org.gradle.api.tasks.Exec.html
If adding to an existing task:
exec {
commandLine("echo", "hi")
}
Another approach is to use the Java ProcessBuilder API:
tasks.create("MyTask") {
val command = "echo Hello"
doLast {
val process = ProcessBuilder()
.command(command.split(" "))
.directory(rootProject.projectDir)
.redirectOutput(Redirect.INHERIT)
.redirectError(Redirect.INHERIT)
.start()
.waitFor(60, TimeUnit.SECONDS)
val result = process.inputStream.bufferedReader().readText()
println(result) // Prints Hello
}
}