Groovy - How to Build a Jar

I wrote a Groovy script that has a dependency on the SQL Server driver (sqljdbc4.jar). I can use GroovyWrapper (link below) to compile it into a JAR, however how can I get the dependencies in a Jar? I was looking for something like "best practice."

http://groovy.codehaus.org/WrappingGroovyScript

Both answers below were useful, but how can I do this for signed Jar files? For instance:

Exception in thread "main" java.lang.SecurityException: invalid d igest signature file for core attributes Manifest

+4
source share
2 answers

In the groovy script wrapper, you will see this line below:

// add more jars here 

This is where you can add your dependencies. If the jar file is in the same directory you are building from, add the following line:

 zipgroupfileset( dir: '.', includes: 'sqljdbc4.jar' ) 

Then restart the script and your jar will include the classes from sqljdbc4.jar .

Edit:

If the jar file you depend on is signed and you need to maintain a signature, you will have to keep an external jar. You cannot include jar files inside other jar files without using a custom class loader. However, you can specify the dependency in the manifest to avoid having to set the class path, i.e. Your jar is still executable with java -jar myjar.jar . Update the manifest section in the script wrapper to:

 manifest { attribute( name: 'Main-Class', value: mainClass ) attribute( name: 'Class-Path', value: 'sqljdbc4.jar' ) } 
+6
source

From your link, if you look at the source of the GroovyWrapper script, this line is there:

 zipgroupfileset( dir: GROOVY_HOME, includes: 'embeddable/groovy-all-*.jar' ) zipgroupfileset( dir: GROOVY_HOME, includes: 'lib/commons*.jar' ) // add more jars here 

I would add it there.

+1
source

All Articles