如何使用Gradle从distZip中排除文件?

时间:2012-12-20 23:59:57

标签: gradle jar

我正在使用Gradle 1.3并让它适用于一个小项目。它完全按照我的要求创建.JAR文件。但是,当我使用distZip创建一个ZIP文件时,所有JAR文件都被包含在内。

build.gradle文件的内容:

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

创建的主要JAR文件:

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

为运行简单客户端而创建的JAR文件:

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

为发布API而创建的JAR文件:

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

运行'distZip'时创建的实际ZIP文件:

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

客户端不需要整个项目所需的所有JAR文件,只需要一小部分。这就是我想要的ZIP文件。

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

有人可以告诉我如何更改build.gradle文件以排除客户端不需要的JAR文件吗?

3 个答案:

答案 0 :(得分:10)

应用程序插件提供了一个配置元素applicationDistribution,允许您从分发目标中排除特定内容,例如:

applicationDistribution.exclude('foo*.jar')

或者如果您要排除多个内容,可以使用Groovy with块:

applicationDistribution.with {
    exclude 'foo*.jar'
    exclude 'bar*.jar'
}

答案 1 :(得分:9)

我没有为此插件找到任何API,但您可以在github存储库https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/groovy/org/gradle/api/plugins/ApplicationPlugin.groovy中找到ApplicationPlugin的完整源代码 这个插件复制来自" project.configurations.runtime"的所有引用。进入目录" lib"。 因此,您可以编辑运行时类路径并排除所有不需要的文件。这应该可以通过添加新配置并添加不应包含在此新配置中的依赖项来实现。您可能希望将排除的文件添加到运行类路径中,以便执行应用程序。

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
}

此脚本确保您在类路径中拥有运行脚本所需的所有库,并且它排除了"中提供的所有库;提供了编译"来自distZip范围。 您可以随意更改名为providedCompile的配置名称。

另外,您可以创建自己的distTask:Provide own script when using plugin application in Gradle 1.0

答案 2 :(得分:4)

通过在build.gradle文件中添加以下行来尝试此操作:

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