Gradle - how do I create a jar with a lib distribution with other banks?

In gradle - how can I embed jars inside my assembly jar assembly in lib (specifically lib / enttoolkit.jar and lib / mail.jar)?

+57
jar gradle uberjar
Aug 10 '10 at 3:28
source share
8 answers

Refurbished to: 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) } } 

In the above snippets, only compilation-dependent dependencies for this project will be included, and not any transitive execution dependencies. If you also want to combine them, replace configurations.compile with configurations.runtime.

EDIT : select only the banks you need

Create a new configuration, possibly releaseJars

 configurations { releaseJars } 

Add the jars you want to set up

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

then use this configuration in the jar task described above.

+34
Aug 10 2018-10-10
source share

If you have all the banks inside the directory (allows you to call it libs ) in your project, you only need this:

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

I suppose it is more likely that these banks are some kind of addiction. Then you can do it as follows:

 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 } } 
+29
Feb 20 2018-12-12T00:
source share

simply:

 task copyToLib( type: Copy ) { into "$buildDir/libs/lib" from configurations.runtime } jar { dependsOn copyToLib } 

run it:

 $ gradle jar ... $ tree build/libs build/libs ├── your-project-0.0.1.BUILD-SNAPSHOT.jar └── lib ├── akka-actor-2.0.jar ├── akka-camel-2.0.jar ├── ... ... ... ├── spring-expression-3.1.0.RELEASE.jar └── zmq-2.1.9.jar 1 directory, 46 files 
+18
Mar 19 2018-12-12T00:
source share

I also needed to do something similar, and I was not able to get what Guus and Stigch offered to work, but close enough with their help to get this work (Guus example exploded on closing dependencies { compile { extendsFrom myLibs }} for me.

 apply plugin: 'groovy' repositories { mavenCentral() } configurations { // custom config of files we want to include in our fat jar that we send to hadoop includeInJar } dependencies { includeInJar 'org.codehaus.groovy:groovy:1.8.6' configurations.compile.extendsFrom(configurations.includeInJar) } jar { into('lib') { println "includeInJar: " + configurations.includeInJar.collect { File file -> file } from configurations.includeInJar } } 

Then running the gradle jar and looking at the created jar gives me this result, showing that I get the jar file with groovy, as well as all the jars that it depends on inside the "thick jar":

 % gradle jar includeInJar: [/Users/tnaleid/.gradle/caches/artifacts-8/filestore/org.codehaus.groovy/groovy/1.8.6/jar/553ca93e0407c94c89b058c482a404427ac7fc72/groovy-1.8.6.jar, /Users/tnaleid/.gradle/caches/artifacts-8/filestore/antlr/antlr/2.7.7/jar/83cd2cd674a217ade95a4bb83a8a14f351f48bd0/antlr-2.7.7.jar, /Users/tnaleid/.gradle/caches/artifacts-8/filestore/asm/asm/3.2/jar/9bc1511dec6adf302991ced13303e4140fdf9ab7/asm-3.2.jar, /Users/tnaleid/.gradle/caches/artifacts-8/filestore/asm/asm-tree/3.2/jar/cd792e29c79d170c5d0bdd05adf5807cf6875c90/asm-tree-3.2.jar, /Users/tnaleid/.gradle/caches/artifacts-8/filestore/asm/asm-commons/3.2/jar/e7a19b8c60589499e35f5d2068d09013030b8891/asm-commons-3.2.jar, /Users/tnaleid/.gradle/caches/artifacts-8/filestore/asm/asm-util/3.2/jar/37ebfdad34d5f1f45109981465f311bbfbe82dcf/asm-util-3.2.jar, /Users/tnaleid/.gradle/caches/artifacts-8/filestore/asm/asm-analysis/3.2/jar/c624956db93975b7197699dcd7de6145ca7cf2c8/asm-analysis-3.2.jar] :compileJava UP-TO-DATE :compileGroovy UP-TO-DATE :processResources UP-TO-DATE :classes UP-TO-DATE :jar BUILD SUCCESSFUL Total time: 3.387 secs % jar tvf build/libs/gradletest.jar 0 Mon Mar 12 11:40:00 CDT 2012 META-INF/ 25 Mon Mar 12 11:40:00 CDT 2012 META-INF/MANIFEST.MF 0 Mon Mar 12 11:40:00 CDT 2012 lib/ 5546084 Mon Mar 05 13:13:32 CST 2012 lib/groovy-1.8.6.jar 445288 Mon Mar 05 13:13:38 CST 2012 lib/antlr-2.7.7.jar 43398 Mon Mar 05 13:13:40 CST 2012 lib/asm-3.2.jar 21878 Mon Mar 05 13:13:40 CST 2012 lib/asm-tree-3.2.jar 33094 Mon Mar 05 13:13:40 CST 2012 lib/asm-commons-3.2.jar 36551 Mon Mar 05 13:13:40 CST 2012 lib/asm-util-3.2.jar 17985 Mon Mar 05 13:13:40 CST 2012 lib/asm-analysis-3.2.jar 
+7
Mar 12 2018-12-12T00:
source share

Below you can try to use the code. It depends on the jar task and is of type Jar

 task createJobJar(dependsOn:jar,type:Jar) { manifest { attributes( "Implementation-Title": 'Job ' ,"Implementation-Version": version ) } classifier 'job' destinationDir new File("$buildDir") into('libs'){ from configurations.compile } into('classes'){ from "$buildDir/classes" } into('resources'){ from "$projectDir/src/main/resources" } into('scripts'){ from "$projectDir/src/main/scripts" } } 

The above code will host different content inside different directories. Tested on gradle 2.2

+7
Dec 18 '14 at 16:20
source share

I needed the same thing that you asked and used this method. you may not need a special configuration declaration, but I needed to separate the locally used jar files from those declared in the super assembly file.

 configurations{ //declare custom config if necessary, otherwise just use compile myLibs } dependencies { //add lib/*.jar files to myLibs myLibs fileTree(dir: 'lib', include: '*.jar') compile { //set compile configuration to extend from myLibs extendsFrom myLibs } } // task to copy libs to output/lib dir task copyToLib(type: Copy) { into "$buildDir/output/lib" from configurations.myLibs } jar { //include contents of output dir from "$buildDir/output" manifest { //... } } //set build task to depend on copyToLib build.dependsOn(copyToLib) 
+6
May 24 '11 at 2:18
source share

In my case, I needed to include the contents of the root Jar project in the Jar subproject. So, to make it work, you can use this template:

 jar{ manifest{ attributes 'Main-Class':'<main class>' } def conf= configurations.find {it.name.equals('compile') } File jar= conf.files.find {it.name.contains('<name or part of the name of produced Jar>')} FileTree fileTree=zipTree(jar) from fileTree } 

My example:

 jar{ manifest{ attributes 'Main-Class':'alexiy.jace.Jace' } description='Make a runnable API Jar' def conf= configurations.find {it.name.equals('compile') } File tools= conf.files.find {it.name.contains('Tools')} FileTree fileTree=zipTree(tools) from fileTree } 
0
Apr 12 '17 at 16:10
source share
 task <taskname>(type: Jar) { archiveName 'nameofjar.jar' doFirst { manifest { attributes 'Class-Path': configurations.compile.files.collect{ project.uri(it) }.join(' ') } } } 
-one
Jun 27 '17 at 9:27
source share



All Articles