Automatic task execution in Buildship

If you work with Eclipse you are probably familiar with Buildship, the Eclipse plugins for Gradle. The Buildship 3.1 release allows you to run tasks upon project synchronization and auto build, the two most highly voted issues on Github. In this post, we’re going to summarize why this is an essential feature for many and how can you make use of it.

Extending project synchronization

The Buildship project synchronization functionality imports the Gradle projects into the workspace and configures them to work with the Java toolchain. That - of course - is just the tip of the iceberg. There are many other tools and frameworks out there, and Buildship can’t provide configuration for all. Since 3.0, there’s a public API to add new integrations, but that requires writing Eclipse plugins. Most users only want to run custom tasks to generate or update configuration files. Running the tasks manually after each change can be frustrating and error-prone. Automatically running the tasks upon project synchronization helps the developers to stay in the flow.

How it works

To use the new feature, you’ll need Buildship 3.1 and a project using Gradle 5.4 and above. In Gradle 5.4 we introduced a new attribute in the eclipse plugin:

plugins {
    id 'eclipse'
}

task generateCustomConfig {
    doLast {
          println "Generating custom configuration..."
    }
}

eclipse {
    synchronizationTasks generateCustomConfig
}

That’s it. When you import or synchronize the project, you’ll see the tasks being executed.

Executing tasks upon synchronization

Note, that the synchronization task is declared with a task reference. You can actually use different types here: strings specifying the task paths, a list of tasks, and more. Essentially, you can use any task dependency types.

Executing tasks during Eclipse build

Along with the feature above, we also added the option to run tasks every time the user changes a resource in the workspace, and the Eclipse build is triggered. This feature is useful to execute small code generator and validator tasks. The syntax is very similar:

plugins {
    id 'eclipse'
}

task generateCode {
    doLast {
        println 'Generating some code...'
    }
}

eclipse {
    autoBuildTasks generateCode
}

Conclusion

This new feature will enable a lot of developers and build authors to provide a smoother work experience within the IDE. Let us know what you think about it and how would you make use if in your own setup.