gradle – 如何build立一个与其他jar子里的lib目录的jar子?
在gradle中 – 我怎么能在lib目录中的我的生成输出jar(特别是lib / enttoolkit.jar和lib / mail.jar)中embeddedjar?
提升逐字从: 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) } }
上面的代码片段将只包含该项目的编译依赖项,而不包括任何传递运行时依赖项。 如果您还想合并这些configuration,请使用configurations.runtimereplaceconfigurations.compile。
编辑 :只select你需要的jar子
做一个新的configuration,释放Jars也许
configurations { releaseJars }
添加你想要的configuration的jar子
dependencies { releaseJars group: 'javax.mail', name: 'mail', version: '1.4' //etc }
然后在上面列出的jar任务中使用该configuration。
如果你的项目中有一个目录下的所有jar(让我们称之为libs
),你只需要这个:
jar { into('lib') { from 'libs' } }
我想这些jar子更有可能是某种依赖性的。 那么你可以这样做:
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 } }
简单:
task copyToLib( type: Copy ) { into "$buildDir/libs/lib" from configurations.runtime } jar { dependsOn copyToLib }
运行:
$ 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
我也需要做类似的事情,并没有完全能够得到Guus和stigkjbuild议的工作,但是在他们的帮助下得到了足够的接近以使得这个工作(Guus的例子在dependencies { compile { extendsFrom myLibs }}
了dependencies { compile { extendsFrom myLibs }}
closure为了我。
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 } }
然后运行gradle jar
并检查创build的jar给了我这个输出,显示我得到的jar文件有groovy以及它所依赖的“fat jar”内的所有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
我需要你问的同样的东西,并使用这种方法。 你可能不需要一个自定义的configuration声明,但我需要将本地使用的jar文件与超级生成文件中声明的jar文件分开。
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)
下面的代码可以尝试。 这取决于jar任务,并且是Jartypes的
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" } }
上面的代码将打包不同的目录内的不同的内容。 在gradle上testing2.2
就我而言,我需要将根项目Jar的内容包含到子项目Jar中。 所以,为了使其工作,可以使用这个模板:
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 }
我的例子:
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 }
task <taskname>(type: Jar) { archiveName 'nameofjar.jar' doFirst { manifest { attributes 'Class-Path': configurations.compile.files.collect{ project.uri(it) }.join(' ') } } }