【问题标题】:Gradle Exec : Why it is not running in configuration phase?Gradle Exec:为什么它不在配置阶段运行?
【发布时间】:2015-09-02 01:12:30
【问题描述】:

这是我的:

build.gradle

task makeDirectoryStructure(type:Exec){

    description 'Creates directory structure .'

    commandLine 'mkdir'
    args '-p' ,'top_dir/sub_dir_1/sub_dir_2'
    println "This line is printed in configuration phase."
}

现在,由于我没有使用 '' 或 doFirst/doLast',我希望 mkdir 会被执行在配置阶段,即每当编译构建脚本时。例如如果我这样做了

$gradle tasks

我希望 mkdir 在配置阶段运行,即我的目录结构应该形成但没有发生。

但是我得到了这个输出:

yogeshwardancharan@yogeshwardancharan-Lenovo-G570:~/android_learning_resources/gradle_resources/ud867/1.01-Exercise-RunYourFirstTask$ gradle tasks
This line is printed in configuration phase.
:tasks

------------------------------------------------------------
All tasks runnable from root project
------------------------------------------------------------

Build Setup tasks
-----------------
init - Initializes a new Gradle build. [incubating]
wrapper - Generates Gradle wrapper files. [incubating]

Help tasks
----------
components - Displays the components produced by root project '1.01-Exercise-RunYourFirstTask'. [incubating]
dependencies - Displays all dependencies declared in root project '1.01-Exercise-RunYourFirstTask'.
dependencyInsight - Displays the insight into a specific dependency in root project '1.01-Exercise-RunYourFirstTask'.
help - Displays a help message.
model - Displays the configuration model of root project '1.01-Exercise-RunYourFirstTask'. [incubating]
projects - Displays the sub-projects of root project '1.01-Exercise-RunYourFirstTask'.
properties - Displays the properties of root project '1.01-Exercise-RunYourFirstTask'.
tasks - Displays the tasks runnable from root project '1.01-Exercise-RunYourFirstTask'.

Other tasks
-----------
makeDirectoryStructure - Creates directory structure .

现在,在这一行的输出打印中

此行在配置阶段打印。

确认任务确实在配置阶段得到处理。

而且,当我发出命令时

$gradle makeDirectoryStructure

上面两行都打印出来了,目录结构也形成了。

所以,最后的问题是为什么我的 mkdir 没有在配置阶段运行,或者我是一些非常常见的概念。

【问题讨论】:

  • 我从这里了解到:discuss.gradle.org/t/… 是 Exec 任务仅在配置阶段配置,它们的实际执行发生在执行阶段本身。

标签: gradle exec mkdir


【解决方案1】:

请查看AbstractExecTask 继承自Exec。如您所见,有很多 getter 和 setter。在配置时发生的只是设置该 fields 的值,而不是运行它们。只有在调用带有@TaskAction 注释的exec() 方法时才会使用所有属性集 - 这发生在运行时。为什么println 有效?它只是一个被调用的方法,和上面提到的setter完全一样——println只是有一个可见的效果,而setter只是改变后面使用的属性。

每个任务都有它的动作。不同之处在于,在配置阶段只配置任务,并且在执行任务操作时使用此配置。

【讨论】:

  • 非常感谢您的明确解释!另外,这就是我们得到 execCommand == null 的原因!当这些配置属性被放入 doFirst/doLast 闭包时。
【解决方案2】:

如果您想在配置阶段运行命令,请在任务中使用 exec 块。确切地说,Project.exec() 方法。 例如:

task makeDirectoryStructure {
    exec {
        commandLine 'mkdir'
        args '-p' ,'top_dir/sub_dir_1/sub_dir_2'
    }
    description 'Creates directory structure .'
    println "This line is printed in configuration phase."
}

这将在 Gradle 的配置阶段创建目录。

Project.exec() in Gradle docs

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-29
    • 2016-04-01
    • 2012-03-03
    相关资源
    最近更新 更多