How to exclude files from distZip using Gradle?

匿名 (未验证) 提交于 2019-12-03 02:18:01

问题:

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?

回答1:

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' } 


回答2:

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



回答3:

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

distributions {     main {         baseName = 'ClassName'         contents {             exclude("**/*.jar")         }     } } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!