Gradle - 为Groovy应用程序创建一个胖JAR

时间:2012-07-19 12:48:09

标签: groovy jar gradle

我有一个使用Gradle构建的Groovy应用程序。我可以使用gradle run正确运行它,但我想创建一个胖JAR来简化部署。我已添加说明

jar {
    dependsOn configurations.runtime
    from { configurations.runtime.collect { it.isDirectory() ? it : zipTree(it) } }
}

到我的build.gradle

似乎生成了包含大量依赖关系的JAR,但是当我尝试使用java -jar myproject.jar运行它时,我收到了错误

Exception in thread "main" java.lang.NoSuchMethodError: org.objectweb.asm.ClassWriter.<init>(Z)V
    at groovy.lang.MetaClassImpl.loadReflector(MetaClassImpl.java:1652)
    at groovy.lang.MetaClassImpl.generateReflector(MetaClassImpl.java:1615)
    at groovy.lang.MetaClassImpl.checkInitialised(MetaClassImpl.java:1532)
    at groovy.lang.MetaClassRegistry.getMetaClass(MetaClassRegistry.java:140)
    at groovy.lang.MetaClassImpl.addNewStaticMethodsFrom(MetaClassImpl.java:1300)
    at groovy.lang.MetaClassImpl.addInheritedMethods(MetaClassImpl.java:201)
    at groovy.lang.MetaClassImpl.checkInitialised(MetaClassImpl.java:1529)
    at groovy.lang.MetaClassRegistry.checkInitialised(MetaClassRegistry.java:201)
    at groovy.lang.MetaClassRegistry.<init>(MetaClassRegistry.java:113)
    at groovy.lang.MetaClassRegistry.<init>(MetaClassRegistry.java:89)
    at org.codehaus.groovy.runtime.Invoker.<init>(Invoker.java:102)
    at org.codehaus.groovy.runtime.InvokerHelper.<clinit>(InvokerHelper.java:80)
    at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToBoolean(DefaultTypeTransformation.java:156)
    at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.booleanUnbox(DefaultTypeTransformation.java:65)
    at mycompany.myproject.Main.main(Main.groovy:6)

更多细节,如果可以提供帮助

供参考,以下是完整的build.gradle

apply plugin: 'groovy'

project.group = 'mycompany.myproject'
archivesBaseName = 'myproject'
project.version = '0.1'
manifest.mainAttributes('Main-Class' : 'mycompany.myproject.Main')

apply plugin: 'application'
mainClassName = 'mycompany.myproject.Main'

repositories {
    mavenCentral()
}

buildscript {
    apply from: 'https://github.com/valkolovos/gradle_cobertura/raw/master/repo/gradle_cobertura/gradle_cobertura/1.2/coberturainit.gradle'
}

jar {
    dependsOn configurations.runtime
    from { configurations.runtime.collect { it.isDirectory() ? it : zipTree(it) } }
}

dependencies {
    groovy group: 'commons-cli', name: 'commons-cli', version: '1.0'
    groovy group: 'org.codehaus.groovy', name: 'groovy', version: '1.8.0'
    compile group: 'log4j', name: 'log4j', version: '1.2.9'
    compile group: 'org.codehaus.groovy.modules.http-builder', name: 'http-builder', version: '0.5.2'
    compile group: 'groovy', name: 'groovy-xmlrpc', version: '0.3'
    testCompile group: 'junit', name: 'junit', version: '4.0'
}


run {
    if (project.hasProperty('args')) {
        args project.args.split('\\s+')
    }
}

cobertura {
    coverageSourceDirs = sourceSets.main.java.srcDirs + sourceSets.main.groovy.srcDirs
}

错误似乎所在的Main类的来源是

package mycompany.myproject


class Main {
    static main(args) {
        def env = args ? args[0] : 'prod'
        new Coordinator().run(env)
    }
}

1 个答案:

答案 0 :(得分:1)

听起来包含错误的ASM版本,虽然我不确定原因。无论如何,使用groovy-all代替groovy通常更简单,更安全,它也应该解决这个问题。

您不应该使用Groovy 1.8.0,而是使用最新的次要版本(1.8.6)。