I have a Groovy application that is built with Gradle. I can run it correctly with gradle run, but I would like to create a fat JAR to make deployment easier. I have added the instructions
jar {
dependsOn configurations.runtime
from { configurations.runtime.collect { it.isDirectory() ? it : zipTree(it) } }
}
to my build.gradle.
It seems that a JAR including a lot of dependencied is generated, but when I try to run it with java -jar myproject.jar, I get the error
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)
More details, if they can help
For reference, here is the full 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
}
The source of the Main class, where the error seems to reside, is
package mycompany.myproject
class Main {
static main(args) {
def env = args ? args[0] : 'prod'
new Coordinator().run(env)
}
}
Sounds like the wrong ASM version gets included, although I’m not sure why. Anyway, it’s usually simpler and safer to use
groovy-allinstead ofgroovy, and it should also solve this problem.You shouldn’t use Groovy 1.8.0 but the latest minor release (1.8.6).