I am using Java to write custom plugin in similar lines to JavaPlugin. Something like
public class MyGradlePlugin implements Plugin<Project>{
public void apply(Project project) {
project.getPlugins().apply(JavaPlugin.class);
............
}
}
Now how do I set sourceSets, repositories, version, group etc using Java?
My first question is whether or not you need to write your plugin in Java. The primary benefit of writing your plugin in Groovy is that you can use all of the exact same syntax that you use in your build.gradle file in your plugin, which makes it much easier for future maintenance of the plugin as anyone familiar with using Gradle will understand what it is doing.
That said, it is possible to use Java. However, you’ll have to go underneath the DSL layer and start working directly with the objects that support it.
For example, to add some repositories you’d need to work directly with the RepositoryHandler.
I am using Eclipse with Groovy plugin to write code. I am new to it and have some doubts. “project” dot ctrl + space (content assist / auto-complete) gives me options for version, group, repositories but not for sourceSets. Am I doing something wrong or have I missed including any library in build path? Apart from Groovy libraries I have included the following -
Eclipse can’t autocomplete it because it’s not part of Project’s API.
Gradle makes heavy use of dynamism to enable the DSL, and currently IDEs do not understand this very well.
You’re best strategy is to write your plugin like you would write your build script, using “project.” to derefence any top level method calls/properties.
As modern Java IDEs get better at working with DSLs over time you’ll get more and more completion, but right now it’s not as good as it is with Java.