Gradle Exec : Why it is not running in configuration phase?

2k views Asked by At

Here is my:

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."
}

Now, since I haven't used '<<' or< 'doFirst/doLast', I expect mkdir to be executed in configuration phase i.e. whenever the build script is compiled. For e.g. if I do

$gradle tasks

I expect mkdir to run in configuration phase i.e. my directory structure should get formed but is not happening.

However I get this output :

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 .

Now , in above output printing of this line

This line is printed in configuration phase.

confirms that task was indeed processed during configuration phase .

And also , when I issue command

$gradle makeDirectoryStructure

both above line is printed and directory structure also get formed .

So , finally the question is why is my mkdir not being run in configuration phase or am I some very common concept.

2

There are 2 answers

1
Opal On BEST ANSWER

Please have a look at AbstractExecTask from which Exec inherits. As you can see there are lots of getters and setters. What happens at configuration time is setting the values for that fields only, not running them. All the properties set will be used in only when exec() method annotated with @TaskAction is called - which happens at runtime. Why println works? It's just a method that is invoked, exactly in the same way as the setters mentioned above - println just have a visible effect, while setters just change the properties used later on.

Every task has its actions. The difference is that at configuration phase the task is configured only and this configuration is used while task action is executed.

0
FRG96 On

If you want to to run the command in configuration phase, then use exec block within a Task. To be exact, the Project.exec() method. For example:

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."
}

This will create the directories in the configuration phase of Gradle.

Project.exec() in Gradle docs