15

我正在使用 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 文件吗?

4

3 回答 3

12

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

applicationDistribution.exclude('foo*.jar')

或者如果您想排除几件事,您可以使用 Groovywith块:

applicationDistribution.with {
    exclude 'foo*.jar'
    exclude 'bar*.jar'
}
于 2014-10-19T16:32:08.830 回答
9

我没有找到该插件的任何 API,但您可以在 github 存储库https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/groovy中找到 ApplicationPlugin 的完整源代码/org/gradle/api/plugins/ApplicationPlugin.groovy 这个插件将所有引用从“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”中定义的所有库。随意更改名为 providedCompile 的配置名称。

或者,您可以创建自己的 distTask:在 Gradle 1.0 中使用插件应用程序时提供自己的脚本

于 2012-12-21T16:34:46.790 回答
4

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

distributions {
    main {
        baseName = 'ClassName'
        contents {
            exclude("**/*.jar")
        }
    }
}
于 2017-08-30T15:12:06.430 回答