Ghi nhớ Gradle
Đăng ngày 23 May 2022
Mục lục
- Hiển thị trợ giúp
- Thiết lập thuộc tính hệ thống của JVM: `-D, --system-prop
- Xác định thuộc tính của dự án cho build script: `-Pmyprop=myvalue
- Chỉ định thư mục khởi động của Gradle. Giá trị mặc định là thư mục hiện tại.
- Ghi đè property
param_componentbằng dòng lệnh: - Thêm một thư mục vào source-set trong một dự án bình thường với kotlin dsl
- Thêm một thư mục vào một source-set trong dự án android với groovy dsl
- Tránh xây dựng và kiểm thử một submodule
- Nén một thư mục thành tệp tar
- Hiển thị các phụ thuộc của một build
- Ví dụ sử dụng
reduce: nối một danh sách thành một chuỗi - Lấy mô tả của một tác vụ cụ thể
- Để chạy một tác vụ từ
workingDirroot, hãy tiền tố tác vụ đó bằng dấu `: - Để chạy lại các tác vụ đã thất bại, hãy sử dụng tùy chọn `--rerun-tasks
- Chạy một tác vụ trong một dự án cụ thể từ một thư mục khác
Hiển thị trợ giúp
./gradlew --help
Thiết lập thuộc tính hệ thống của JVM: `-D, --system-prop
./gradlew -Dmyprop=myvalue
#ou
./gradlew --system-prop myvalue
Xác định thuộc tính của dự án cho build script: `-Pmyprop=myvalue
./gradlew -Pmyprop=myvalue
#ou
./gradlew --project-prop myvalue
Chỉ định thư mục khởi động của Gradle. Giá trị mặc định là thư mục hiện tại.
./gradlew -p ~/src/next_startup
#ou
./gradlew --project-dir ~/src/next_startup
Ghi đè property param_component bằng dòng lệnh:
./gradlew -Pparam_component=CUSTOM_VALUE
Thêm một thư mục vào source-set trong một dự án bình thường với kotlin dsl
sourceSets {
getByName("test"){
java.srcDir("src/scripts/groovy")
}
getByName("test"){
java.srcDir("src/scripts/kscript")
}
getByName("test"){
java.srcDir("src/test/javascript")
}
}
Thêm một thư mục vào một source-set trong dự án android với groovy dsl
android {
sourceSets {
main.java.srcDirs += "src/main/../../../../ceelo/domain/src/main/java/"
test.java.srcDirs += "src/test/../../../../ceelo/domain/src/test/java/"
}
}
Tránh xây dựng và kiểm thử một submodule
gradle build -x :excluded-module:check -x :excluded-module:assemble -x :excluded-module:build
Di chuyển các tệp
tasks.register("moveWebappNode") {
doLast {
ant.withGroovyBuilder {
"move"(
"webapp/node_modules" to "$rootDir/webapp-src/node_modules",
)
}
}
}
Nén một thư mục thành tệp tar
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"))
}
}
Hiển thị các phụ thuộc của một 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()
}
}
}
}
Ví dụ sử dụng reduce: nối một danh sách thành một chuỗi
tasks.register("printWebappSrc") {
description = "print webapp sources"
group = WEBAPP
doLast {
webAppSrc
.reduce { acc, s -> "$acc\n\t$s" }
.run { println("$WEBAPP_SRC: $this\n") }
}
}
Lấy mô tả của một tác vụ cụ thể
./gradlew -q help --task foo
Để chạy một tác vụ từ workingDir root, hãy tiền tố tác vụ đó bằng dấu `:
./gradlew :foo
Để chạy lại các tác vụ đã thất bại, hãy sử dụng tùy chọn `--rerun-tasks
./gradlew check --rerun-tasks
Chạy một tác vụ trong một dự án cụ thể từ một thư mục khác
Tùy chọn`-p` (ou --project-dir) cho phép chỉ định đường dẫn đến một dự án Gradle và thực thi một tác vụ tại đó, ngay cả khi thư mục hiện tại là khác.
Ví dụ, để chạy tác vụ`build`của dự án nằm trong`~/projets/mon-autre-projet`trong khi đang ở một thư mục khác:
./gradlew -p ~/projets/mon-autre-projet build
Điều này đặc biệt hữu ích cho việc viết script các hành động trên nhiều dự án từ một vị trí trung tâm.
xem thêm: