Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude files from distZip using Gradle?

Tags:

gradle

jar

I am using Gradle 1.3 and have it working for a small project. It creates the .JAR files exactly as I want them to. However, when I have it create a ZIP file using distZip, all JAR files are being included.

The contents of my build.gradle file:

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

group = 'com.some.project'
version = '1.0.2.0'
description = 'Update Server'
mainClassName = 'com.some.project.updateserver.client.Client'

defaultTasks 'compileJava', 'jar'

[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'

allprojects {
    tasks.withType(Compile) {
        options.debug = true
        options.compilerArgs = ['-Xlint:all']
    }
}

dependencies {
    compile "$commonsCodecGroup:commons-codec:$commonsCodecVersion"
    compile "$commonsConfigurationGroup:commons-configuration:$commonsConfigurationVersion"
    compile "$commonsLangGroup:commons-lang:$commonsLangVersion"
    compile "$commonsLoggingGroup:commons-logging:$commonsLoggingVersion"
    compile "$cxfGroup:cxf-bundle-minimal:$cxfVersion"
    compile "$cxfGroup:cxf-rt-databinding-jaxb:$cxfVersion"
    compile "$cxfGroup:cxf-rt-frontend-jaxrs:$cxfVersion"
    compile "$cxfGroup:cxf-rt-transports-common:$cxfVersion"
    compile "$cxfGroup:cxf-rt-transports-http:$cxfVersion"
    compile "$jacksonGroup:jackson-jaxrs:$jacksonVersion"
    compile "$jmockitGroup:jmockit:$jmockitVersion"
    compile "$logBackGroup:logback-classic:$logBackVersion"
    compile "$logBackGroup:logback-core:$logBackVersion"
    compile "$servletGroup:servlet-api:$servletVersion"
    compile "$slf4jGroup:slf4j-api:$slf4jVersion"
    compile "$springGroup:spring-aop:$springVersion"
    compile "$springGroup:spring-asm:$springVersion"
    compile "$springGroup:spring-beans:$springVersion"
    compile "$springGroup:spring-context:$springVersion"
    compile "$springGroup:spring-core:$springVersion"
    compile "$springGroup:spring-expression:$springVersion"
    compile "$springGroup:spring-tx:$springVersion"
    compile "$springGroup:spring-web:$springVersion"
    compile "$wsRestfulGroup:jsr311-api:$wsRestfulVersion"

    testCompile "$junitGroup:junit:$junitVersion"
}

repositories {
    mavenCentral()
}

task clientJar(type: Jar, description: 'Assembles a jar archive for running a simple client against the Update Server.') {
    appendix = 'client'
    from sourceSets.main.output
    exclude('applicationContext.xml')
    exclude('com/some/project/json')
    exclude('com/some/project/updateserver/jaxrs')
    exclude('com/some/project/updateserver/oauth')
    exclude('com/some/project/updateserver/resource')
    exclude('com/some/project/updateserver/util/ManifestHash*')
}

task modelJar(type: Jar, description: 'Assembles a jar archive for reference by other projects that need to access the API model of Update Server.') {
    appendix = 'model'
    from sourceSets.main.output.classesDir
    exclude('applicationContext.xml')
    exclude('update-server-client.properties')
    exclude('com/some/project/json')
    exclude('com/some/project/updateserver/client')
    exclude('com/some/project/updateserver/jaxrs')
    exclude('com/some/project/updateserver/oauth')
    exclude('com/some/project/updateserver/resource')
}

jar {
    description = 'Assembles the relevant archive files for Update Server'
    dependsOn clientJar, modelJar
    exclude('update-server-client.properties')
    exclude('com/some/project/updateserver/client')
}

test {
    testLogging.exceptionFormat 'full'
    testLogging {
        events 'passed'
        minGranularity = 3
        stackTraceFilters 'groovy', 'entry_point', 'truncate'
        showStandardStreams = true
        debug {
            events 'started'
        }
    }
}

run {
    description = 'Runs the Update Server Client application'
    jvmArgs '-client'
    // optional args can be specified
    // args 'name1', 'value1'
}

distZip {
    archiveName "$project.name-client.zip"
    exclude('**/aopalliance-*.jar')
    exclude('**/asm-3.3.jar')
    exclude('**/bcprov-jdk*.jar')
    exclude('**/commons-codec-*.jar')
    exclude('**/commons-httpclient-*.jar')
    exclude('**/cxf-api-*.jar')
    exclude('**/cxf-common-utilities-*.jar')
    exclude('**/cxf-rt-bindings-*.jar')
    exclude('**/cxf-rt-core*.jar')
    exclude('**/geronimo-*.jar')
    exclude('**/isorelax-*.jar')
    exclude('**/jaxb-*.jar')
    exclude('**/jcip-annotations-*.jar')
    exclude('**/jcl-over-slf4j-*.jar')
    exclude('**/jettison-*.jar')
    exclude('**/jetty-*.jar')
    exclude('**/jmockit-*.jar')
    exclude('**/joda-time-*.jar')
    exclude('**/jul-to-slf4j-*.jar')
    exclude('**/log4j-over-slf4j-*.jar')
    exclude('**/logback-*.jar')
    exclude('**/mimepull-*.jar')
    exclude('**/msv-core-*.jar')
    exclude('**/not-yet-commons-*.jar')
    exclude('**/opensaml-*.jar')
    exclude('**/openws-*.jar')
    exclude('**/relaxngDatatype-*.jar')
    exclude('**/saaj-*.jar')
    exclude('**/serializer-*.jar')
    exclude('**/servlet-api-*.jar')
    exclude('**/slf4j-api*.jar')
    exclude('**/spring-*.jar')
    exclude('**/stax-*.jar')
    exclude('**/woodstox-*.jar')
    exclude('**/wssj4j-*.jar')
    exclude('**/xalan-*.jar')
    exclude('**/xmlbeans-*.jar')
    exclude('**/xml-resolver-*.jar')
    exclude('**/xmlschema-*.jar')
    exclude('**/xmlsec-*.jar')
    exclude('**/xmltooling-*.jar')
    exclude('**/xsdlib-*.jar')
}

The primary JAR file that gets created:

update-server-1.0.2.0.jar
-------------------------
applicationContext.xml
com/pearson/pss/json/JsonUtil.class
com/pearson/pss/updateserver/jaxrs/
com/pearson/pss/updateserver/jaxrs/JacksonObjectMapperFactory.class
com/pearson/pss/updateserver/model/PowerSchoolServerData$UPDATE_CATEGORY.class
com/pearson/pss/updateserver/model/PowerSchoolServerData.class
com/pearson/pss/updateserver/model/SimplePackageDescription.class
com/pearson/pss/updateserver/model/UpdateData.class
com/pearson/pss/updateserver/oauth/OAuthIdentity.class
com/pearson/pss/updateserver/oauth/OAuthTarget.class
com/pearson/pss/updateserver/oauth/OAuthTargetRegistry.class
com/pearson/pss/updateserver/oauth/OAuthTargetUpdateServerDownload.class
com/pearson/pss/updateserver/oauth/OAuthTicketData.class
com/pearson/pss/updateserver/oauth/PowerSourceDLOAuthBackChannel$PostValue.class
com/pearson/pss/updateserver/oauth/PowerSourceDLOAuthBackChannel.class
com/pearson/pss/updateserver/resource/Config$1.class
com/pearson/pss/updateserver/resource/Config.class
com/pearson/pss/updateserver/resource/CurrentDistrict$1.class
com/pearson/pss/updateserver/resource/CurrentDistrict.class
com/pearson/pss/updateserver/resource/DistrictDownloadRestriction.class
com/pearson/pss/updateserver/resource/OAuthDownloadFilter.class
com/pearson/pss/updateserver/resource/UpdateResource$1.class
com/pearson/pss/updateserver/resource/UpdateResource.class
com/pearson/pss/updateserver/util/ManifestHash.class
com/pearson/pss/updateserver/util/PackageUtil$1.class
com/pearson/pss/updateserver/util/PackageUtil$Architecture.class
com/pearson/pss/updateserver/util/PackageUtil$OS.class
com/pearson/pss/updateserver/util/PackageUtil.class
com/pearson/pss/updateserver/util/UpdateServerConstants.class
META-INF/cxf/org.apache.cxf.Logger
META-INF/MANIFEST.MF

The JAR file that is created for running a simple client:

update-server-client-1.0.2.0.jar
--------------------------------
com/pearson/pss/updateserver/client/Client.class
com/pearson/pss/updateserver/model/PowerSchoolServerData$UPDATE_CATEGORY.class
com/pearson/pss/updateserver/model/PowerSchoolServerData.class
com/pearson/pss/updateserver/model/SimplePackageDescription.class
com/pearson/pss/updateserver/model/UpdateData.class
com/pearson/pss/updateserver/util/PackageUtil$1.class
com/pearson/pss/updateserver/util/PackageUtil$Architecture.class
com/pearson/pss/updateserver/util/PackageUtil$OS.class
com/pearson/pss/updateserver/util/PackageUtil.class
com/pearson/pss/updateserver/util/UpdateServerConstants.class
META-INF/cxf/org.apache.cxf.Logger
META-INF/MANIFEST.MF
update-server-client.properties

The JAR file that is created for publishing the API:

update-server-model-1.0.2.0.jar
--------------------------------
com/pearson/pss/updateserver/model/PowerSchoolServerData$UPDATE_CATEGORY.class
com/pearson/pss/updateserver/model/PowerSchoolServerData.class
com/pearson/pss/updateserver/model/SimplePackageDescription.class
com/pearson/pss/updateserver/model/UpdateData.class
com/pearson/pss/updateserver/util/ManifestHash.class
com/pearson/pss/updateserver/util/PackageUtil$1.class
com/pearson/pss/updateserver/util/PackageUtil$Architecture.class
com/pearson/pss/updateserver/util/PackageUtil$OS.class
com/pearson/pss/updateserver/util/PackageUtil.class
com/pearson/pss/updateserver/util/UpdateServerConstants.class
META-INF/MANIFEST.MF

The actual ZIP file that gets created when running 'distZip':

update-server-1.0.2.0.zip (actual contents)
-------------------------------------------
update-server-1.0.2.0/bin/update-server
update-server-1.0.2.0/bin/update-server.bat
update-server-1.0.2.0/lib/aopalliance-1.0.jar
update-server-1.0.2.0/lib/asm-3.3.jar
update-server-1.0.2.0/lib/bcprov-jdk15-1.45.jar
update-server-1.0.2.0/lib/commons-codec-1.4.jar
update-server-1.0.2.0/lib/commons-configuration-1.8.jar
update-server-1.0.2.0/lib/commons-httpclient-3.1.jar
update-server-1.0.2.0/lib/commons-lang-2.6.jar
update-server-1.0.2.0/lib/commons-logging-1.1.1.jar
update-server-1.0.2.0/lib/cxf-api-2.5.2.jar
update-server-1.0.2.0/lib/cxf-bundle-minimal-2.5.2.jar
update-server-1.0.2.0/lib/cxf-common-utilities-2.5.2.jar
update-server-1.0.2.0/lib/cxf-rt-bindings-xml-2.5.2.jar
update-server-1.0.2.0/lib/cxf-rt-core-2.5.2.jar
update-server-1.0.2.0/lib/cxf-rt-databinding-jaxb-2.5.2.jar
update-server-1.0.2.0/lib/cxf-rt-frontend-jaxrs-2.5.2.jar
update-server-1.0.2.0/lib/cxf-rt-transports-common-2.5.2.jar
update-server-1.0.2.0/lib/cxf-rt-transports-http-2.5.2.jar
update-server-1.0.2.0/lib/geronimo-activation_1.1_spec-1.1.jar
update-server-1.0.2.0/lib/geronimo-annotation_1.0_spec-1.1.1.jar
update-server-1.0.2.0/lib/geronimo-javamail_1.4_spec-1.7.1.jar
update-server-1.0.2.0/lib/geronimo-jaxws_2.2_spec-1.1.jar
update-server-1.0.2.0/lib/geronimo-jms_1.1_spec-1.1.1.jar
update-server-1.0.2.0/lib/geronimo-servlet_2.5_spec-1.1.2.jar
update-server-1.0.2.0/lib/geronimo-stax-api_1.0_spec-1.0.1.jar
update-server-1.0.2.0/lib/geronimo-ws-metadata_2.0_spec-1.1.3.jar
update-server-1.0.2.0/lib/isorelax-20030108.jar
update-server-1.0.2.0/lib/jackson-core-asl-1.9.4.jar
update-server-1.0.2.0/lib/jackson-jaxrs-1.9.4.jar
update-server-1.0.2.0/lib/jackson-mapper-asl-1.9.4.jar
update-server-1.0.2.0/lib/jaxb-api-2.2.3.jar
update-server-1.0.2.0/lib/jaxb-impl-2.2.4-1.jar
update-server-1.0.2.0/lib/jaxb-xjc-2.2.4-1.jar
update-server-1.0.2.0/lib/jcip-annotations-1.0.jar
update-server-1.0.2.0/lib/jcl-over-slf4j-1.6.1.jar
update-server-1.0.2.0/lib/jettison-1.3.1.jar
update-server-1.0.2.0/lib/jetty-continuation-7.5.4.v20111024.jar
update-server-1.0.2.0/lib/jetty-http-7.5.4.v20111024.jar
update-server-1.0.2.0/lib/jetty-io-7.5.4.v20111024.jar
update-server-1.0.2.0/lib/jetty-security-7.5.4.v20111024.jar
update-server-1.0.2.0/lib/jetty-server-7.5.4.v20111024.jar
update-server-1.0.2.0/lib/jetty-util-7.5.4.v20111024.jar
update-server-1.0.2.0/lib/jmockit-0.999.19.jar
update-server-1.0.2.0/lib/joda-time-1.6.2.jar
update-server-1.0.2.0/lib/jsr311-api-1.1.1.jar
update-server-1.0.2.0/lib/jul-to-slf4j-1.6.1.jar
update-server-1.0.2.0/lib/log4j-over-slf4j-1.6.1.jar
update-server-1.0.2.0/lib/logback-classic-1.0.0.jar
update-server-1.0.2.0/lib/logback-core-1.0.0.jar
update-server-1.0.2.0/lib/mimepull-1.4.jar
update-server-1.0.2.0/lib/msv-core-2011.1.jar
update-server-1.0.2.0/lib/neethi-3.0.1.jar
update-server-1.0.2.0/lib/not-yet-commons-ssl-0.3.9.jar
update-server-1.0.2.0/lib/opensaml-2.5.1-1.jar
update-server-1.0.2.0/lib/openws-1.4.2-1.jar
update-server-1.0.2.0/lib/relaxngDatatype-20020414.jar
update-server-1.0.2.0/lib/saaj-api-1.3.4.jar
update-server-1.0.2.0/lib/saaj-impl-1.3.12.jar
update-server-1.0.2.0/lib/serializer-2.7.1.jar
update-server-1.0.2.0/lib/servlet-api-2.5.jar
update-server-1.0.2.0/lib/slf4j-api-1.6.4.jar
update-server-1.0.2.0/lib/spring-aop-3.1.1.RELEASE.jar
update-server-1.0.2.0/lib/spring-asm-3.1.1.RELEASE.jar
update-server-1.0.2.0/lib/spring-beans-3.1.1.RELEASE.jar
update-server-1.0.2.0/lib/spring-context-3.1.1.RELEASE.jar
update-server-1.0.2.0/lib/spring-core-3.1.1.RELEASE.jar
update-server-1.0.2.0/lib/spring-expression-3.1.1.RELEASE.jar
update-server-1.0.2.0/lib/spring-jms-3.0.6.RELEASE.jar
update-server-1.0.2.0/lib/spring-tx-3.1.1.RELEASE.jar
update-server-1.0.2.0/lib/spring-web-3.1.1.RELEASE.jar
update-server-1.0.2.0/lib/stax2-api-3.1.1.jar
update-server-1.0.2.0/lib/update-server-1.0.2.0.jar
update-server-1.0.2.0/lib/woodstox-core-asl-4.1.1.jar
update-server-1.0.2.0/lib/wsdl4j-1.6.2.jar
update-server-1.0.2.0/lib/wss4j-1.6.4.jar
update-server-1.0.2.0/lib/xalan-2.7.1.jar
update-server-1.0.2.0/lib/xmlbeans-2.5.0.jar
update-server-1.0.2.0/lib/xml-resolver-1.2.jar
update-server-1.0.2.0/lib/xmlschema-core-2.0.1.jar
update-server-1.0.2.0/lib/xmlsec-1.4.6.jar
update-server-1.0.2.0/lib/xmltooling-1.3.2-1.jar
update-server-1.0.2.0/lib/xsdlib-2010.1.jar

The client does not need all of the JAR files neceesary by the entire project, just a small subset. This is what I want the ZIP file to look like.

update-server-1.0.2.0.zip (wanted contents)
-------------------------------------------
update-server-1.0.2.0/bin/update-server
update-server-1.0.2.0/bin/update-server.bat
update-server-1.0.2.0/lib/commons-configuration-1.8.jar
update-server-1.0.2.0/lib/commons-lang-2.6.jar
update-server-1.0.2.0/lib/commons-logging-1.1.1.jar
update-server-1.0.2.0/lib/cxf-bundle-minimal-2.5.2.jar
update-server-1.0.2.0/lib/cxf-rt-databinding-jaxb-2.5.2.jar
update-server-1.0.2.0/lib/cxf-rt-frontend-jaxrs-2.5.2.jar
update-server-1.0.2.0/lib/cxf-rt-transports-common-2.5.2.jar
update-server-1.0.2.0/lib/cxf-rt-transports-http-2.5.2.jar
update-server-1.0.2.0/lib/jackson-core-asl-1.9.4.jar
update-server-1.0.2.0/lib/jackson-jaxrs-1.9.4.jar
update-server-1.0.2.0/lib/jackson-mapper-asl-1.9.4.jar
update-server-1.0.2.0/lib/jsr311-api-1.1.1.jar
update-server-1.0.2.0/lib/neethi-3.0.1.jar
update-server-1.0.2.0/lib/wsdl4j-1.6.2.jar

Can someone please tell me how to change the the build.gradle file to exclude the unwanted JAR files for the client?

like image 634
Mike Viens Avatar asked Dec 20 '12 23:12

Mike Viens


People also ask

What does Gradle installDist do?

You can run gradle installDist to create an image of the application in build/install/projectName . You can run gradle distZip to create a ZIP containing the distribution, gradle distTar to create an application TAR or gradle assemble to build both.

What is Gradle distZip?

You can run gradle distZip to package the main distribution as a ZIP, or gradle distTar to create a TAR file. To build both types of archives just run gradle assembleDist . The files will be created at $buildDir/distributions/${project.name}-${project. version}. «ext» .

What does apply plugin do in Gradle?

Applying a plugin to a project allows the plugin to extend the project's capabilities. It can do things such as: Extend the Gradle model (e.g. add new DSL elements that can be configured) Configure the project according to conventions (e.g. add new tasks or configure sensible defaults)

What is Fatjar in Gradle?

Basically, a fat jar (also known as uber-jar) is a self-sufficient archive which contains both classes and dependencies needed to run an application.


3 Answers

The application plugin provides a configuration element applicationDistribution, which allows you to exclude specific things from the distribution targets, e.g.:

applicationDistribution.exclude('foo*.jar')

or if you'd like to exclude several things, you can use a Groovy with block:

applicationDistribution.with {
    exclude 'foo*.jar'
    exclude 'bar*.jar'
}
like image 117
Mark Elliot Avatar answered Oct 24 '22 04:10

Mark Elliot


I didn't find any API for this plugin but you can find the full source code of the ApplicationPlugin in the github repository https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/groovy/org/gradle/api/plugins/ApplicationPlugin.groovy This plugin copys all references from "project.configurations.runtime" into the directory "lib". Therefore, you can edit the runtime classpath and exclude all non needed files. This should be possible by adding a new configuration and adding the dependencies that shouldnt be included to this new configuration. You probably want to add the excluded files to the run classpath, so you can execute your application.

configurations {
    providedCompile
}

dependencies {
    compile "define your dependencies that should be added to the zipfile"
    providedCompile "dependencies that shouldnt be added to the zipfile"
}

compileJava {
    sourceSets.main.compileClasspath += configurations.providedCompile
}

run {
    classpath += configurations.providedCompile
}

This script ensures, that you have all librarys on your classpath that are required to run your script and it excludes all librarys defined in "providedCompile" from the distZip scope. Feel free to change the name of the configuration called providedCompile.

Alternativly, you can create your own distTask: Provide own script when using plugin application in Gradle 1.0

like image 38
Dennis Fischer Avatar answered Oct 24 '22 06:10

Dennis Fischer


Try this by adding below lines in build.gradle file:

distributions {
    main {
        baseName = 'ClassName'
        contents {
            exclude("**/*.jar")
        }
    }
}
like image 45
yeralin Avatar answered Oct 24 '22 06:10

yeralin