Gradle Learning (3)-Basic understanding

Source: Internet
Author: User
1. Use of methods


Task Checksum <<{
        fileList ('.. /test '). each{
                ant.checksum (file:file,property: "Cs_$file.name")
                println "$file. Name Checksum: ${ ant.properties["Cs_$file.name"]} "
        }
}

task LoadFile <<{
        fileList ('.. /test '). Each{file file->
                ant.loadfile (srcfile:file,property:file.name)
                println "I ' m fond of $file. Name"
        }
}

File[] FileList (String dir) {
        file (dir). Listfiles ({file->file.isfile ()} as FileFilter). Sort ()
}

"-a" represents an assignment, somewhat like a lambda syntax.


{File->file.isfile ()} as FileFilter


This sentence is added a judgment condition, for the passed dir is a file name, do not do the processing.


2. Default Tasks


Defaulttasks ' clean ', ' Run '
task clean <<{
        println ' Default cleaning! '
}
Task run <<{
        println ' Default running! '
}
Task other <<{
        println "I-m not  a default task"
}

The default task is that these default tasks are executed when the task name is not specified


qianhuis-mac-mini:1228 qianhui$ gradle-q
Default cleaning!
Default running!

The default task name above identifies that there is no task execution at this time, but it performs the default task.


If you specify a task name, see what the output is


qianhuis-mac-mini:1228 qianhui$ gradle-q Other
I ' m not  a default task
qianhuis-mac-mini:1228 qianhui$ Gradle-q Run
Default running!

It will execute its own, which is a bit like the default value of a property in Java, and if you give a new value, the default value will not work.

In a multi-project build, a subproject can have its own default task, and the default task that inherits the parent project is used by default if the subproject is not.


3. Configuring with a DAG (directed acyclic graph)


Gradle the configuration phase and execution phase, when the configuration phase is complete, gradle knows all the tasks that need to be performed. This allows us to use this information to understand which tasks have been executed. The following example determines whether the release task is executed after the task execution is completed.


Task Distribution <<{
        println "We build the zip with version = $version"
}
Task release (dependsOn: ' Dis Tribution ') <<{
        println ' We release Now '
}
gradle.taskGraph.whenReady {
        taskgraph->
        if (Taskgraph.hastask (release)) {
                Version = ' 1.0 '
        }else {
                version = ' 1.0-snapshot '
        }
}
~   

Output


qianhuis-mac-mini:1228 qianhui$ gradle-q Distribution
We build the zip with version = 1.0-snapshot


Indicates that the release task exists in the above Dag, stating that release will not be executed.


qianhuis-mac-mini:1228 qianhui$ gradle-q Release
We build the zip with Version = 1.0
We Release now


Indicates that a release task exists in the above Dag, stating that release will be executed.

One thing to note in the above code is Whenready, which will work before the task executes.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.