AWT AWT - 4 months ago 71
Java Question

I used gradle to build a fat jar with all of my dependencies included in it. Now, when using distZip, how do I exclude these jar files?

So here's my gradle script:

apply plugin: 'java'
apply plugin: 'application'

mainClassName = "com.company.diagnostics.app.client.AppMain"

dependencies {

compile ('commons-codec:commons-codec:1.8')
compile (libraries.jsonSimple)
compile ('org.apache.ant:ant:1.8.2')

compile project(":app-common")

testCompile 'org.powermock:powermock-mockito-release-full:1.6.2'

}

jar {

archiveName = "app-client.jar"

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

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

manifest {
attributes 'Main-Class': 'com.company.diagnostics.app.client.AppMain"'
}

exclude 'META-INF/*.SF', 'META-INF/*.DSA', 'META-INF/*.RSA', 'META-INF/*.MF'
}


When this builds, it produces a distributable zip that looks like this:

macbook-pro:distributions awt$ tree
.
├── app-client
│   ├── bin
│   │   ├── app-client
│   │   └── app-client.bat
│   └── lib
│   ├── ant-1.8.2.jar
│   ├── ant-launcher-1.8.2.jar
│   ├── commons-codec-1.8.jar
│   ├── app-client.jar
│   ├── app-common.jar
│   ├── guava-17.0.jar
│   ├── jetty-2.0.100.v20110502.jar
│   ├── json-simple-1.1.2.jar
│   ├── osgi-3.7.2.v20120110.jar
│   ├── services-3.3.0.v20110513.jar
│   └── servlet-1.1.200.v20110502.jar
└── app-client.zip


Because I've already bundled the dependencies into the jar archive with my own custom jar task, how do I prevent distZip from bundling these jar files a second time?

- ant-1.8.2.jar
- ant-launcher-1.8.2.jar
- commons-codec-1.8.jar
- guava-17.0.jar
- jetty-2.0.100.v20110502.jar
- json-simple-1.1.2.jar
- osgi-3.7.2.v20120110.jar
- services-3.3.0.v20110513.jar
- servlet-1.1.200.v20110502.jar


The reason they're bundled into the jar task is that this was originally meant to be a standalone library. Later on it was decided that it should have a command-line interface as well (hence, the distZip and automatic creation of the wrapper scripts for linux/mac/windows). It still needs to exist as a standalone fatjar with all dependencies bundled into it. I just don't need this extra cruft in /libs.

How can I get distZip to exclude them?

Answer

You can modify your distZip task, to exclude the libs you don't want to be in the distribution archive, like:

distZip {
    exclude 'ant-1.8.2.jar'
    exclude 'ant-launcher-1.8.2.jar'
    exclude 'commons-codec-1.8.jar'
    exclude 'guava-17.0.jar'
    exclude 'jetty-2.0.100.v20110502.jar'
    exclude 'json-simple-1.1.2.jar'
    exclude 'osgi-3.7.2.v20120110.jar'
    exclude 'services-3.3.0.v20110513.jar'
    exclude 'servlet-1.1.200.v20110502.jar'
}

Or may be via applicationDistribution, which provides configuration for whole application plugin:

applicationDistribution.with {
    exclude 'ant-1.8.2.jar'
    exclude 'ant-launcher-1.8.2.jar'
    exclude 'commons-codec-1.8.jar'
    exclude 'guava-17.0.jar'
    exclude 'jetty-2.0.100.v20110502.jar'
    exclude 'json-simple-1.1.2.jar'
    exclude 'osgi-3.7.2.v20120110.jar'
    exclude 'services-3.3.0.v20110513.jar'
    exclude 'servlet-1.1.200.v20110502.jar'
}

You can try to change exclude to include to make a list of files shorter, or try to bind excludes to the dependencies list may be.

Comments