Excluding some dependencies when building fat jar

I’ll first describe the general problem I face, as the approach I am taking might not be the best one. Feel free to point me in the right direction if what I’m doing is misguided.

The thing I try to do is this: I have built a plugin in a separate project. This plugin is used in several of our main projects. The plugin has a list of dependencies, some jars from local flatDirs for example. When I use the plugin in other projects, I have a buildscript closure that in its dependencies closure defines not only the plugin but also the dependencies of the plugin. This bothered me a bit as I found it redundant: ideally the plugin should contain whatever it needed, and I should be able to cut down the dependency list in the main project’s buildscript to only the plugin in itself.

It then occured to me that I could build the plugin as a fat jar, including all it’s dependencies. However, the plugin depends on gradleApi(), and I don’t think I want all of those dependencies in my fat jar. But how do I go about filtering away the gradleApi()-dependencies when building the fat jar?

The relevant parts of the plugin’s build.gradle looks like this:

jar {

from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } } } …

dependencies {

compile gradleApi()

compile ‘com.google.guava:guava:10.0.1’

compile ‘org.tmatesoft.svnkit:svnkit:1.7.6’

… }

One solution is to publish the plugin to a Maven or Ivy repository. Then you’ll get transitive dependency management for free, including correct handling of ‘gradleApi’.

Another option is to introduce a ‘provided’ configuration. Something like:

configurations {
    provided.extendsFrom(compile)
    testCompile.extendsFrom(provided)
}
  sourceSets.main {
    compileClasspath = configurations.provided
}
  dependencies {
    provided gradleApi()
    compile ...
}

Excludes are not supported for gradleApi(). Until they are, you could use file dependencies as a workaround.

dependencies {

compile fileTree(gradle.gradleHomeDir.absolutePath) {

include “lib/*.jar”

include “lib/plugins/*.jar”

exclude …

}

}