如何在gradle自定义任务中导入类?

时间:2018-02-15 12:38:35

标签: gradle

我正在尝试使用this代码摘录来拦截gradle日志到文件中。我不想对我的build.gradle进行审核,因此,我创建了一个interceptor.gradle来封装这个逻辑。它看起来像是:

interceptor.gradle

import org.gradle.logging.internal.*

task intercept {
    def outputStream = new File("gradle.log")
        gradle.services.get(LoggingOutputInternal).addStandardOutputListener (new StandardOutputListener () {
                void onOutput(CharSequence output) {
                    outputStream << output
                }
            }
        )
}

在我的build.gradle中,我添加了:

apply from: project.file('tooling/gradle/interceptor.gradle')

问题是:当我尝试./gradlew build时,我得到了:

* What went wrong:
A problem occurred evaluating script.
> Could not get unknown property 'LoggingOutputInternal' for task ':intercept' of type org.gradle.api.DefaultTask.

如何隔离拦截gradle日志的代码并将其保存到单独的gradle脚本中的文件中并使其从主build.gradle执行?

1 个答案:

答案 0 :(得分:1)

尝试使用此导入import org.gradle.internal.logging.*

import org.gradle.internal.logging.*

task intercept {
    def outputStream = new File("gradle.log")
        gradle.services.get(LoggingOutputInternal).addStandardOutputListener (new StandardOutputListener () {
                void onOutput(CharSequence output) {
                    outputStream << output
                }
            }
        )
}