Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gradle - how do I build a jar with a lib dir with other jars in it?

In gradle - how can I embed jars inside my build output jar in the lib directory (specifially the lib/enttoolkit.jar and lib/mail.jar)?

like image 889
phil swenson Avatar asked Aug 10 '10 03:08

phil swenson


People also ask

Where are jars stored in Gradle?

Gradle declares dependencies on JAR files inside your project's module_name /libs/ directory (because Gradle reads paths relative to the build.gradle file). This declares a dependency on version 12.3 of the "app-magic" library, inside the "com.example.android" namespace group.

How do I create a runnable jar with Gradle?

In its simplest form, creating an executable JAR with Gradle is just a matter of adding the appropriate entries to the manifest. However, it's much more common to have dependencies that need to be included on the classpath, making this approach tricky in practice.


2 Answers

If you have all the jars inside a directory (lets call it libs) in your project, you only need this:

jar {     into('lib') {         from 'libs'     } } 

I guess it is more likely that these jars are dependencies of some sort. Then you could do it like this:

configurations {     // configuration that holds jars to copy into lib     extraLibs } dependencies {     extraLibs 'org.something:something-dep1:version'     extraLibs 'org.something:something-dep2:version' }  jar {     into('lib') {         from configurations.extraLibs     } } 
like image 156
stigkj Avatar answered Sep 27 '22 17:09

stigkj


Lifted verbatim from: http://docs.codehaus.org/display/GRADLE/Cookbook#Cookbook-Creatingafatjar

Gradle 0.9:

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

Gradle 0.8:

jar.doFirst {     for(file in configurations.compile) {         jar.merge(file)     } } 

The above snippets will only include the compile dependencies for that project, not any transitive runtime dependencies. If you also want to merge those, replace configurations.compile with configurations.runtime.

EDIT: only choosing jars you need

Make a new configuration, releaseJars maybe

configurations {     releaseJars } 

Add the jars you want to that configuration

dependencies {     releaseJars group: 'javax.mail', name: 'mail', version: '1.4'     //etc } 

then use that configuration in the jar task outlined above.

like image 25
lucas Avatar answered Sep 27 '22 19:09

lucas