Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groovy - How to Build a Jar

I've written a Groovy script which has a dependency on a SQL Server driver (sqljdbc4.jar). I can use the GroovyWrapper (link below) to compile it into a JAR, however how can I get dependencies into the Jar? I'm looking for a "best practice" sort of thing.

https://github.com/sdanzan/groovy-wrapper

Both of the replies below have been helpful, but how can I do this for signed Jar files? For instance:

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

like image 929
Steve Avatar asked Mar 23 '23 07:03

Steve


1 Answers

In the groovy wrapper script, you'll see this line near the bottom:

// add more jars here

That's where you can add your dependencies. If the jar file is in the same directory you're building from, add a line like this:

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

Then rerun 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 the signature, you'll have to keep the external jar. You can't include jar files inside of other jar files without using a custom classloader. You can, however, specify the dependency in the manifest to avoid having to set the classpath, i.e. your jar still executable with java -jar myjar.jar. Update the manifest section in the wrapping script to:

manifest {
    attribute( name: 'Main-Class', value: mainClass )
    attribute( name: 'Class-Path', value: 'sqljdbc4.jar' )
}
like image 150
ataylor Avatar answered Apr 26 '23 18:04

ataylor