我有一个doNecessaryJob
任务,该任务取决于preProcessing
任务。如果preProcessing
中的某个条件为true,我想立即停止同步。我该怎么办?
我的部分礼物在这里:
task preProcessing() {
Gradle gradle = getGradle()
String tskReqStr = gradle.getStartParameter().getTaskRequests().toString()
Pattern pattern
if (tskReqStr.contains("assemble"))
pattern = Pattern.compile("assemble(\\w+)(Release|Debug)")
else
pattern = Pattern.compile("generate(\\w+)(Release|Debug)")
Matcher matcher = pattern.matcher(tskReqStr)
if (matcher.find()) {
// do processing here
} else {
println "Error, stopping the sync."
// !STOP THE GRADLE SYNC HERE DUE TO ERROR!
}
}
task doNecessaryJob() {
dependsOn preProcessing
// do necessary processing here that depends on variables from preProcessing task
}
我在SO中检查了其他解决方案,但似乎所有解决方案都指向诸如How to stop Gradle task execution in Android Studio?之类的命令行解决方案。
答案 0 :(得分:0)
我找到了。我像这样使用GradleException
:
task preProcessing() {
Gradle gradle = getGradle()
String tskReqStr = gradle.getStartParameter().getTaskRequests().toString()
Pattern pattern
if (tskReqStr.contains("assemble"))
pattern = Pattern.compile("assemble(\\w+)(Release|Debug)")
else
pattern = Pattern.compile("generate(\\w+)(Release|Debug)")
Matcher matcher = pattern.matcher(tskReqStr)
if (matcher.find()) {
// do processing here
} else {
throw new GradleException("Error, stopping the sync.") // <--- THE FIX
}
}
答案 1 :(得分:0)
您可以在终端中调用./gradlew --stop,它将杀死所有gradle进程