我需要同样的东西你问,并使用这种方法。您可能不需要自定义配置声明,但是我需要将本地使用的 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)
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并检查创建的 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
在我的例子中,我需要将根项目 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
}