Gradle memo
Published on 23 May 2022
Sommaire
- Display help
- Set the JVM system property: `-D, --system-prop
- Set the project property for the build script: `-Pmyprop=myvalue
- Specifies Gradle’s startup directory. The default is the current directory.
- Override the 'param_component' property via the command line:
- Add a folder to a source set in a normal project with the kotlin DSL
- Add a folder to a source set in an Android project with the Groovy DSL
- Avoid building and testing a submodule
- Tar a folder
- Display the dependencies of a build
- Example usage of reduce: concatenation of a list into a string
- Retrieve the description of a specific task
- To launch a task from the workingDir root, then prefix the task with a `:
- To rerun the tasks that failed, use the option `--rerun-tasks
- Run a task in a specific project from another directory
Display help
./gradlew --help
Set the JVM system property: `-D, --system-prop
./gradlew -Dmyprop=myvalue
#ou
./gradlew --system-prop myvalue
Set the project property for the build script: `-Pmyprop=myvalue
./gradlew -Pmyprop=myvalue
#ou
./gradlew --project-prop myvalue
Specifies Gradle’s startup directory. The default is the current directory.
./gradlew -p ~/src/next_startup
#ou
./gradlew --project-dir ~/src/next_startup
Override the 'param_component' property via the command line:
./gradlew -Pparam_component=CUSTOM_VALUE
Add a folder to a source set in a normal project with the kotlin DSL
sourceSets {
getByName("test"){
java.srcDir("src/scripts/groovy")
}
getByName("test"){
java.srcDir("src/scripts/kscript")
}
getByName("test"){
java.srcDir("src/test/javascript")
}
}
Add a folder to a source set in an Android project with the Groovy DSL
android {
sourceSets {
main.java.srcDirs += "src/main/../../../../ceelo/domain/src/main/java/"
test.java.srcDirs += "src/test/../../../../ceelo/domain/src/test/java/"
}
}
Avoid building and testing a submodule
gradle build -x :excluded-module:check -x :excluded-module:assemble -x :excluded-module:build
Move files
tasks.register("moveWebappNode") {
doLast {
ant.withGroovyBuilder {
"move"(
"webapp/node_modules" to "$rootDir/webapp-src/node_modules",
)
}
}
}
Tar a folder
tasks.register<Tar>("tarWebapp") {
dependsOn("moveWebappNpm")
group = WEBAPP
description = "tar webapp"
doLast {
setOf(
"build",
"target",
"node_modules"
).forEach { dir -> exclude { it.name .dir } }
archiveFileName.set("webapp.tar")
destinationDirectory.set(File("${rootDir.absolutePath}$sep$WEBAPP_SRC"))
}
}
Display the dependencies of a build
tasks.register("printDependencies") {
description = "print project dependencies"
group = WEBAPP
doLast {
println("${project.name} dependencies")
mutableMapOf<String, Map<String, String>>(
"buildDependencies" to buildDependencies,
"domainDeps" to domainDeps,
"domainTestDeps" to domainTestDeps,
).apply { putAll(appModules) }
.forEach { module ->
if (module.value.isNotEmpty()) {
println("${module.key}:")
module.value.forEach { println(dependency(it)) }
println()
}
}
}
}
Example usage of reduce: concatenation of a list into a string
tasks.register("printWebappSrc") {
description = "print webapp sources"
group = WEBAPP
doLast {
webAppSrc
.reduce { acc, s -> "$acc\n\t$s" }
.run { println("$WEBAPP_SRC: $this\n") }
}
}
Retrieve the description of a specific task
./gradlew -q help --task foo
To launch a task from the workingDir root, then prefix the task with a `:
./gradlew :foo
To rerun the tasks that failed, use the option `--rerun-tasks
./gradlew check --rerun-tasks
Run a task in a specific project from another directory
The option`-p` (ou --project-dir) allows specifying the path to a Gradle project and executing a task there, even if the current directory is different.
For example, to launch the task`build`of the project located in`~/projets/mon-autre-projet`while being in another directory:
./gradlew -p ~/projets/mon-autre-projet build
This is particularly useful for scripting actions across multiple projects from a central location.
see also :