0

一个非常简短的搜索解释了如何打包运行时依赖项以进行分发

thufir@mordor:~/NetBeansProjects/hello_client$ 
thufir@mordor:~/NetBeansProjects/hello_client$ gradle clean build
Changed strategy of configuration ':compile' after it has been resolved. This behaviour has been deprecated and is scheduled to be removed in Gradle 3.0
:clean
:compileJava
:processResources UP-TO-DATE
:classes
:jar
:startScripts
:distTar
:distZip
:assemble
:compileTestJava UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test UP-TO-DATE
:check UP-TO-DATE
:build

BUILD SUCCESSFUL

Total time: 9.237 secs
thufir@mordor:~/NetBeansProjects/hello_client$ 
thufir@mordor:~/NetBeansProjects/hello_client$ java -jar build/libs/hello_client.jar
hello [fred]
thufir@mordor:~/NetBeansProjects/hello_client$ 

具有所需的输出。然而,建议的解决方案,一个“胖罐子”,导致了这个怪物:

thufir@mordor:~/NetBeansProjects/hello_client$ 
thufir@mordor:~/NetBeansProjects/hello_client$ jar -ft build/libs/hello_client.jar 
META-INF/
META-INF/MANIFEST.MF
net/
net/bounceme/
net/bounceme/mordor/
net/bounceme/mordor/hello/
net/bounceme/mordor/hello/client/
net/bounceme/mordor/hello/client/HelloClient.class
net/bounceme/mordor/hello/library/
net/bounceme/mordor/hello/library/HelloLibrary.class
META-INF/ANTLR-LICENSE.txt
META-INF/ASM-LICENSE.txt
META-INF/CLI-LICENSE.txt
META-INF/JSR223-LICENSE.txt
META-INF/LICENSE.txt
META-INF/NOTICE.txt
META-INF/dgminfo
META-INF/groovy-release-info.properties
META-INF/maven/
META-INF/maven/commons-cli/
META-INF/maven/commons-cli/commons-cli/
META-INF/maven/commons-cli/commons-cli/pom.properties
META-INF/maven/commons-cli/commons-cli/pom.xml
META-INF/services/
META-INF/services/javax.script.ScriptEngineFactory
META-INF/services/org.codehaus.groovy.plugins.Runners
META-INF/services/org.codehaus.groovy.runtime.ExtensionModule
META-INF/services/org.codehaus.groovy.source.Extensions
META-INF/services/org.codehaus.groovy.transform.ASTTransformation
groovy/
groovy/beans/
groovy/beans/Bindable.class
groovy/beans/BindableASTTransformation.class

然后似乎无休止地继续下去。显然,我不想在胖罐中包含 groovy。如何从构建的 JAR 中排除 groovy?

构建文件:

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

mainClassName = 'net.bounceme.mordor.hello.client.HelloClient'

sourceCompatibility = '1.8'
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
if (!hasProperty(mainClassName)) {
    ext.mainClass = mainClassName
}

repositories {
    mavenCentral()
    maven { url "https://jitpack.io" }
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.10'
    compile 'com.github.THUFIR:hello_api:dev'
}

jar {

    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
    manifest {
        attributes ('Main-Class': mainClassName,
            "Class-Path": configurations.compile.collect { it.getName() }.join(' '))
    }
}

assemble.dependsOn (jar)

configurations.all {
    resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
}

有什么替代方案:

from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }

诚然,我还没有学习过 Groovy DSL,到目前为止我只是在拼凑一些零碎的东西。

我相信我看到提到从另一组中减去一组依赖项以最小化包含的库。但是,在奇怪的情况下,有些库不需要编译,但需要执行 JAR,我不确定它是如何工作的。

4

0 回答 0