How to copy JAR dependency libraries in gradle

I have a runnable jar with this build.gradle

apply plugin: 'java' apply plugin: 'application' manifest.mainAttributes("Main-Class" : "com.test.HelloWorld") repositories { mavenCentral() } dependencies { compile ( 'commons-codec:commons-codec:1.6', 'commons-logging:commons-logging:1.1.1', 'org.apache.httpcomponents:httpclient:4.2.1', 'org.apache.httpcomponents:httpclient:4.2.1', 'org.apache.httpcomponents:httpcore:4.2.1', 'org.apache.httpcomponents:httpmime:4.2.1', 'ch.qos.logback:logback-classic:1.0.6', 'ch.qos.logback:logback-core:1.0.6', 'org.slf4j:slf4j-api:1.6.0', 'junit:junit:4.+' ) } 

but it works unsuccessfully because dependency bans cannot find.

and then add this code:

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

but nothing will change ... I cannot find the output of the / libs folders ...

how can i copy libs jars dependencies to the specified folder or path?

+34
copy dependencies gradle
Feb 03 '13 at 5:36
source share
3 answers

Add:

 build.dependsOn(copyToLib) 

When gradle build Gradle, Gradle creates tasks and all tasks dependent on them (declared in dependsOn on dependsOn ). Without the build.dependsOn(copyToLib) Gradle will not associate the copy task with the build task.

So:

 apply plugin: 'java' apply plugin: 'application' manifest.mainAttributes('Main-Class': 'com.test.HelloWorld') repositories { mavenCentral() } dependencies { compile ( 'commons-codec:commons-codec:1.6', 'commons-logging:commons-logging:1.1.1', 'org.apache.httpcomponents:httpclient:4.2.1', 'org.apache.httpcomponents:httpclient:4.2.1', 'org.apache.httpcomponents:httpcore:4.2.1', 'org.apache.httpcomponents:httpmime:4.2.1', 'ch.qos.logback:logback-classic:1.0.6', 'ch.qos.logback:logback-core:1.0.6', 'org.slf4j:slf4j-api:1.6.0', 'junit:junit:4.+' ) } task copyToLib(type: Copy) { into "${buildDir}/output/libs" from configurations.runtime } build.dependsOn(copyToLib) 
+41
Oct 08 '13 at 6:03
source share

I find the application plugin too cumbersome and too verbose in my output. Here's how I finally got the setting I liked: create a distribution zip file with jar dependencies in the /lib subdirectory and add all the dependencies to the Class-Path entry in the manifest file:

 apply plugin: 'java' apply plugin: 'java-library-distribution' repositories { mavenCentral() } dependencies { compile 'org.apache.commons:commons-lang3:3.3.2' } // Task "distZip" added by plugin "java-library-distribution": distZip.shouldRunAfter(build) jar { // Keep jar clean: exclude 'META-INF/*.SF', 'META-INF/*.DSA', 'META-INF/*.RSA', 'META-INF/*.MF' manifest { attributes 'Main-Class': 'com.somepackage.MainClass', 'Class-Path': configurations.runtime.files.collect { "lib/$it.name" }.join(' ') } // How-to add class path: // http://stackoverflow.com/questions/22659463/add-classpath-in-manifest-using-gradle // https://gist.github.com/simon04/6865179 } 

Hosting is the essence here .

The result can be found in build/distributions and the unzipped content is as follows:

Lib / Common-lang3-3.3.2.jar
MyJarFile.jar

The content of MyJarFile.jar#META-INF/MANIFEST.mf :

Manifest Version: 1.0
Main class: com.somepackage.MainClass
Class Path: lib / commons-lang3-3.3.2.jar

+15
Mar 03 '15 at 6:06
source share

The application plugin requires that you specify the name of the main class as follows:

 mainClassName = "com.test.HelloWorld" 

You need to add this to your build script. Keep in mind that if you try to run the application using the java command, you also need to set the class path using -cp .

The application plug-in simplifies this process by providing the distZip task. If you run this task, you will be created a complete distribution under build/distributions . The distribution contains startup scripts and all the dependencies. The generated startup scripts have already set the class path for you, so you no longer have to deal with this.

+1
Feb 13 '13 at 20:04
source share



All Articles