DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report

Gradle Goodness: Working with Live Task Collection

Hubert Klein Ikkink user avatar by
Hubert Klein Ikkink
·
Jun. 28, 12 · Interview
Like (0)
Save
Tweet
Share
5.53K Views

Join the DZone community and get the full member experience.

Join For Free

Gradle supports the definition of so called live collections. These collections are mostly created based on criteria like with a filter() or matching() method. The collection content can change if the content of the source collection changes. For example the org.gradle.api.DomainObjectCollection interface has a matching method that returns a live collection. The list of tasks in a project implements this interface so we can have a live collection of tasks that match a certain criteria.

The nice thing about the live collection of tasks is that we can add tasks to the project after we have defined the task collection and they will be included in the collection. Normally if we would access the list of tasks in a project and use for example a findAll method than the returned list of tasks will not change. So if we add a new task to the project it will not be added to the list of task that apply to the condition of the findAll method.

Now if we use the matching() method on a list of tasks the result will be a live list of tasks. Even if we add tasks to the project after the definition of the list of tasks, they will be added to the collection.

Let's see the following Gradle build file that defines the tasks allCompile and allCopy. Each task has dependencies on other tasks. We use the dependsOn() method of a task to set those dependencies. The dependsOn() method accepts a collection of other tasks. For the allCompile task we don't use a live collection and for the allCopy task we use a live collection.

task allCompile << {
    println 'All is compiled.'
}
allCompile.dependsOn project.tasks.findAll {
    it.name.startsWith('compile')
}

task allCopy << {
    println 'Everything is copied.'
}
// Set dependencies with live collection of tasks.
allCopy.dependsOn project.tasks.matching {
    it.name.startsWith('copy')
}

// Add dependency tasks.
5.times {
    // Dependencies for compileAll task.
    task "compile${it}" << { println 'Compile the code.' }

    // Dependencies for copyAll task.
    task "copy${it}" << { println 'Copy something.' }
}

If we run the build script we get the following output:

$ gradle allCompile allCopy
:allCompile
All is compiled.
:copy0
Copy something.
:copy1
Copy something.
:copy2
Copy something.
:copy3
Copy something.
:copy4
Copy something.
:allCopy
Everything is copied.
 
BUILD SUCCESSFUL
 
Total time: 2.232 secs

We notice the allCompile tasks doesn't have any dependencies, because those task dependencies didn't exist when we used the dependsOn() method. The allCopy task has all task dependencies even though we created them later in the build script.

Bonus: we can use the findAll method to look for task dependencies, but we have to let Gradle evaluate this condition in a closure. So we can change our build script and use a closure with the dependsOn() method. Gradle will invoke the closure at execution time and not a configuration time. The dependencies tasks are then available and assigned as dependencies to the allCompile task:

task allCompile << {
    println 'All is compiled.'
}
// Use closure for resolving task dependencies.
allCompile.dependsOn {
    project.tasks.findAll {
        it.name.startsWith('compile')
    }
}

task allCopy << {
    println 'Everything is copied.'
}
// Set dependencies with live collection of tasks.
allCopy.dependsOn project.tasks.matching {
    it.name.startsWith('copy')
}

// Add dependency tasks.
5.times {
    // Dependencies for compileAll task.
    task "compile${it}" << { println 'Compile the code.' }

    // Dependencies for copyAll task.
    task "copy${it}" << { println 'Copy something.' }
}

If we invoke both tasks with Gradle we get the following output:

:compile0
Compile the code.
:compile1
Compile the code.
:compile2
Compile the code.
:compile3
Compile the code.
:compile4
Compile the code.
:allCompile
All is compiled.
:copy0
Copy something.
:copy1
Copy something.
:copy2
Copy something.
:copy3
Copy something.
:copy4
Copy something.
:allCopy
Everything is copied.
 
BUILD SUCCESSFUL
 
Total time: 2.258 secs

This blog post is based on Gradle version 1.0-rc-3

 

 

 

 

 

 

Task (computing) Gradle

Published at DZone with permission of Hubert Klein Ikkink, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Introduction to Containerization
  • File Uploads for the Web (2): Upload Files With JavaScript
  • Best Navicat Alternative for Windows
  • Custom Validators in Quarkus

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: