Gradle Memo
Published on 23 May 2022
Sommaire
- Show help
- Set JVM system property:
-D, --system-prop - Set project property for the build script:
-Pmyprop=myvalue - Specifies the Gradle startup directory. The default value 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 kotlin dsl
- Add a folder to a source-set in an android project with groovy dsl
- Avoid building and testing a sub-module
- Tar a folder
- Show build dependencies
- Example of using reduce: concatenating a list into a string
- Retrieve the description of a specific task
- To run a task from the root workingDir, prefix the task with a
: - To rerun failed tasks, use the
--rerun-tasksoption - Run a task in a specific project from another directory
Show help
./gradlew --help
Set JVM system property: -D, --system-prop
./gradlew -Dmyprop=myvalue
#ou
./gradlew --system-prop myvalue
Set project property for the build script: -Pmyprop=myvalue
./gradlew -Pmyprop=myvalue
#ou
./gradlew --project-prop myvalue
Specifies the Gradle startup directory. The default value 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 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 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 sub-module
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"))
}
}
Show build dependencies
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 of using reduce: concatenating 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 run a task from the root workingDir, prefix the task with a :
./gradlew :foo
To rerun failed tasks, use the --rerun-tasks option
./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 run 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: