What is the correct way of extending the Gradle Java Plugin?

I have a custom type of archive that I want to build with gradle, the structure is equals to a standard java project so I'd like to use it but then I have other resources that go to a custom location, additionally I'd like to package the archive as a zip and not as a jar, I'm thinking of creating a subclass of the Java gradle plugin but I'm not sure if this is the way to go. What would be the appropriate way of creating this new plugin that inherits the Java functionality?

Upvotes: 2

Views: 2888

Answers (1)

Rene Groeschke
Rene Groeschke

Reputation: 28653

if you want to create a custom java plugin that has conventions, that differ from the standard gradle plugin, I recommend to start with the java-base plugin. The java-base plugin does provide the functionality of sourcesets but does not already any of them.

Instead of inheriting a plugin you should apply it in your custom plugin:

class YourCustomJavaPlugin implements Plugin<Project> {
    void apply(Project project){
        // 1st apply the 'java-base' plugin
        project.apply(plugin:'java-base')

        // 2nd apply your own conventions on top
        project.sourceSets.create("customSourceSet")
        Zip myZip = project.tasks.create("zip", Zip)
        ...
    }
}

cheers,

René

Upvotes: 8

Related Questions