我正在使用 Gradle 2.12 并从Logging doc中创建了一个示例。该示例未使用 Android Studio 或 BuildKite,但我相信这将有助于解决基本问题。
build.gradle
给定一个典型 Java 项目的简单文件:
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
testCompile 'junit:junit:4.11'
}
compileJava << { println "TRACER example log from compileJava" }
compileTestJava << { println "TRACER example log from compileTestJava" }
test << { println "TRACER example log from test" }
和一个init.gradle
文件:
useLogger(new CustomEventLogger())
class CustomEventLogger extends BuildAdapter implements TaskExecutionListener {
public void beforeExecute(Task task) {
if (task.name ==~ "compileJava") {
println "--- Compiling"
} else if (task.name == "test") {
println "--- Testing"
}
}
public void afterExecute(Task task, TaskState state) {}
public void buildFinished(BuildResult result) {}
}
然后这个命令行:
$ gradle -I init.gradle test
产生这个输出:
--- Compiling
TRACER example log from compileJava
TRACER example log from compileTestJava
--- Testing
TRACER example log from test