所以这是我的gradle脚本:
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'
}
构建后,它会生成一个可分发的zip,看起来像这样:
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
因为我已经使用自己的自定义jar任务将依赖项 bundle 到jar存档中,所以如何防止distZip再次 bundle 这些jar文件?
- 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
之所以将它们 bundle 到jar任务中,是因为它原本是一个独立的库。后来确定它也应该具有命令行界面(因此,distZip和自动为linux / mac / windows创建包装器脚本)。它仍然需要作为独立的fatjar存在,并将所有依赖项 bundle 在一起。我只是不需要/ libs中的多余内容。
如何获得distZip排除它们?
最佳答案
您可以修改distZip任务,以排除不想包含在分发存档中的库,例如:
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'
}
或者可以通过
applicationDistribution
,它为整个应用程序插件提供配置: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'
}
您可以尝试将
exclude
更改为include
以使文件列表更短,或者尝试将排除项绑定(bind)到依赖项列表。