Need advice creating a plugin

plugins

(Matty) #1

Hi all,

So I need some advice on constructing a gradle build / plugin. I have a huge project I need to integrate some tasks into so it can be run in a CI environment and also deploy to a live production server.

Currently I have these tasks defined in a build.gradle file with a bunch of methods to do various things but this is getting messy and there is still a lot more to add.

I have decided to solve this by making a custom plugin which will do a lot of the work but need some advice on how to construct it.

I need to have a bunch of tasks which some rely on others.

The tasks I need to set out are:

  1. Deploy
  2. Build
  3. Change values in a .properties file

The tasks need to rely on each other in that order but also have the ability to be run separately. I also need a parameter passed to each task my plan is to use @Option to do this, is this recommended? (any other way?)

The full process calling Deploy should do the following…

Deploy <> Build <> Change Values

How should this be setup, should I use build.dependsOn changeValues?

Also one other quick question, how do I make a gradle plugin call the “build” task (I think this is part of the Java plugin?) when I added “taskX.dependsOn build” it said that it couldn’t find the “build” task?


(Benjamin Muschko) #2

The tasks need to rely on each other in that order but also have the ability to be run separately.

That’s a typical use case for mustRunAfter instead of dependsOn.

I also need a parameter passed to each task my plan is to use @Option to do this, is this recommended?

The annotation is currently an internal API. If you feel comfortable we using it then it’s a valid option. In the long run we’ll want to make it a public API: https://github.com/gradle/gradle/issues/2402.

Also one other quick question, how do I make a gradle plugin call the “build” task (I think this is part of the Java plugin?)

It’s being added by the JavaBasePlugin. The JavaPlugin internally applies the JavaBasePlugin. You are relying on the build task then you can apply the JavaBasePlugin in your plugin.

Generally speaking I’d recommending reading the following topical guides on plugin development:


(Matty) #3

Thanks that helped.

Issue I am having now is the calling build, it seems to call build on buildSrc project and not the parent project?
e.g

project.task(“someTask”) {
finalizedBy “:build”
}


(Benjamin Muschko) #4

Please provide a minimalistic project on GitHub that reproduces the issue. I’d need to see the code.