如何将应用的Gradle脚本的执行延迟/延迟到构建脚本的最后一个

时间:2019-06-04 05:54:36

标签: gradle build

我有一个Gradle构建脚本,如下所示:

build.gradle

apply plugin: java
apply from: some-other-script.gradle


project.ext {
   distDir = "${xxx}"
   buildDir = "${yyy}"
   testLibDir = "${zzzz}"
   webArtifactFile = "directory/aaaa.war"
}

//code to clean, build code. etc.

和其他脚本具有:

some-other-script.gradle

//code to perform some common-tasks;
//Here I want to access the property exported by build.gradle

//like:
def file = webArtifactFile;

some code for that file.

但是当我在顶部应用some-other-script.gradle时,我无法获得build.gradle file导出的属性

但是如果我在apply from: some-other-build-script.gradle的末尾build.gradle像这样:

apply plugin: java


project.ext {
   distDir = "${xxx}"
   buildDir = "${yyy}"
   testLibDir = "${zzzz}"
   webArtifactFile = "directory/aaaa.war"
}

//code to clean, build code. etc.

 apply from: some-other-script.gradle

然后,我可以访问在构建脚本中导出的所有属性(但这会使代码不可读)。

所以我的问题是:

如何将应用的Gradle脚本的执行延迟/推迟到构建脚本的最后一个?

PS。我刚开始学习新手,所以对于专家来说这可能是一个非常疯狂的问题,但我仍在为此寻求帮助。

希望这里的专家会帮助我。

1 个答案:

答案 0 :(得分:1)

检查this Gradle documentation:您的情况下可以使用项目评估侦听器

您有两个选择:

1)在主afterEvaluate脚本的build.gradle侦听器中应用脚本插件:

build.gradle

apply plugin: java
afterEvaluate {
    //  some-other-script.gradle  script will have access to properties exported by main build script
    apply from: some-other-script.gradle
}

2)在脚本插件本身中实现afterEvaluate侦听器逻辑:

build.gradle

apply plugin: java
apply from: some-other-script.gradle

some-other-script.gradle

afterEvaluate {
    def file = webArtifactFile;

    // some code for that file.
}

解决方案2可能更好,因为它将使您的主要build.gradle保持清洁。