palkarrohan
palkarrohan

Reputation: 499

How to manage external dependencies of a Gradle Plugin

I have created a custom gradle plugin that performs some installation using code that has been defined in another sub-project of our source-code. The build.gradle for the plugin is

dependencies {
    compile project(path: ':installlib', configuration: 'libConfig')
    compile project(path: ':tools', configuration: 'toolConfig')
}

jar {
    from {
        // LINE#1 source-code of plugin
        sourceSets.main.output  
        
        zip64 = true

        // LINE#2 dependencies
        configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } 
    }
    baseName = 'myCustomPlugin'
    version = '1.0'
    destinationDir = new File(project.libDir)
}

The above plugin is consumed as follows - consumer.gradle

buildscript {
    repositories { flatDir name: 'libs', dirs: System.env.CODESOURCE + '/lib/')
    dependencies {
        classpath: ':myCustomPlugin:1.0'
    }
}

apply plugin: 'java'
apply plugin: 'myCustomPlugin'

...
...
//rest of the items of this gradle

Case-A If I run this, I hit > Plugin with id 'myCustomPlugin' not found.

NOTE: I have META-INF/gradle-plugins/myCustomPlugin.properties created correctly with implementation-class pointing to my plugin code and this works fine if I don't get into creating the fat/uber-jar business and just include the sourceSets.main.output statement in my jar task. But since our entire project depends on file based artifacts, I am attempting to create a fat/uber-jar and that's where I start running into these issues.

Case-B If I move the LINE#2 above the LINE#1, which looks like -

from {
        // LINE#2 dependencies
        configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } 

        // LINE#1 source-code of plugin
        sourceSets.main.output  

        zip64 = true
    }

If I run this, I hit ClassNotFoundException for one of the classes coming from the project 'installlib'. I can see the *.class file in the uber jar created but even then the class loader complains about this class.

  1. Could anyone provide some pointers on how to resolve this ? The uber/fat jar creation (or the way I am doing it) is not helping with the plugin-source-code and dependencies of the plugin.
  2. If not a fat jar, could anyone provide some inputs on how to resolve the dependencies in the buildscript of the consuming gradle ?

Upvotes: 2

Views: 617

Answers (0)

Related Questions