gradle-如何构建一个带有lib dir以及其他jar的jar?


Answers:


37

从以下网址逐字删除:http : //docs.codehaus.org/display/GRADLE/Cookbook#Cookbook-Creatingafatjar

摇篮0.9:

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

摇篮0.8:

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

上面的代码片段将仅包含该项目的编译依赖项,而不包含任何传递性运行时依赖项。如果您还希望合并它们,请用configuration.runtime替换configuration.compile。

编辑:只选择您需要的罐子

进行新的配置,也许发布Jars

configurations {
    releaseJars
}

将您想要的罐子添加到该配置

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

然后在上述jar任务中使用该配置。


1
我在0.9rc1上。所以尝试了您的第一个建议。拉进了我不想要的各种东西.....
菲尔·斯文森2010年

我认为...问题更近了,问题是我需要lib目录中的jar。这样可以从罐子中提取类。我想在输出jar中使用lib / mail和lib / enttoolkit
phil swenson 2010年

1
我知道这并没有回答问题,而是一个绝妙的秘诀。谢谢卢卡斯。
罗勒·穆萨

2
很好的提示,但完全错过了非常具体的问题。
塞巴斯蒂安·甘斯兰德

2
由于“所有Codehaus服务都已终止”
因此

34

如果您的项目中的所有jar都位于目录中(称为libs),则只需要这样做:

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

我猜这些罐子很可能是某种依赖。然后,您可以这样做:

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
    }
}

2
如果需要所有依赖关系,还可以从复制configurations.runtime,从而为您提供默认的compile依赖关系,从而省去了创建自己的extraLibs配置的麻烦。
Thunderforge

错误:在类型为org.gradle.api.Project的项目':app'上找不到参数[build_bndovev91pu8trwrdngc8qh7i $ _run_closure5 @ 3be2321e]的方法jar()。
naXa

1
@Thunderforge为了获得所有依赖关系,我不得不从而configurations.runtimeClasspath不是从中进行复制configurations.runtime,同时避免了对extraLibs定义的需求。
史蒂夫·皮彻斯

我们只需要在srcDirs processResources{ from(zipTree("src/main/resources/lib/local-jar-name.jar")) }
Lokesh Singal'4

23

简单:

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

9
这会将库复制到输出目录中,而不是复制到JAR文件本身中—这就是问题所在。
TheOperator'May

7

我还需要做类似的事情,但并不能完全按照Guus和stigkj的建议工作,但是在他们的帮助下,他们已经足够亲密,以使这项工作正常进行(Guus的例子dependencies { compile { extendsFrom myLibs }}对我来说是封闭的。

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以及它依赖于“胖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

好的,我已经完成了此处描述的操作,但是我仍然可以得到Exception in thread "main" java.lang.NoClassDefFoundError: com/rabbitmq/client/ConnectionFactory。我应该以某种方式设置classpath吗?
omikron

7

下面的代码可以尝试。它取决于jar任务,属于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"
    }
}

上面的代码会将不同的内容包装在不同的目录中。在gradle 2.2上测试


6

我需要跟您问的一样,并使用了这种方法。您可能不需要自定义配置声明,但是我需要将本地使用的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)

1

我有同样的问题。我这样解决了:

使用copyToLib将文件复制到lib文件夹中,并使用Class-Path引用依赖项

ext.mainClass = 'test.main'

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.google.code.gson:gson:2.8.2'
    //....
}

jar {
    from "$buildDir/libs/lib"
    manifest {
        attributes 'Main-Class': 'test.main',
        'Class-Path': configurations.compile.collect { 'lib/'+it.getName() }.join(' ')
    }
}

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

build.dependsOn(copyToLib)

0

就我而言,我需要将根项目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
}

0

这是我使用Gradle设法实现此构建设置的方法。这会将您的应用程序构建到Jar中,将生成的库jar复制到build / libs下的libs目录中,并配置类路径以将jar包含在创建的build / libs / libs目录中。

 plugins {
    id 'application'
 }

 group 'org.pkg'
 version '1.0-SNAPSHOT'

 ext.mainClass = 'org.pkg.MainClass'

 repositories {
    mavenCentral()
    mavenLocal()
 }

 dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile group: 'junit', name: 'junit', version: '4.12'

    implementation group: 'com.mchange', name: 'c3p0', version: '0.9.5.5'
 }
 
 task copyToLib(type: Copy) {
    into "$buildDir/libs/libs"
    // configurations.runtimeClasspath includes maven jars
    from configurations.runtimeClasspath 
 }

 def archiveVersion = "1.0.0"

 task uberJar(type: Jar) {
    archiveClassifier = 'uber'

    from sourceSets.main.output
    // include the copy task
    dependsOn(copyToLib)

    // use onfigurations.runtimeClasspath to collect all the jars
    manifest {
      attributes(
            'Class-Path': configurations.runtimeClasspath.collect { 'libs/' + it.getName() }.join(' '),
            'Main-Class': 'org.pkg.MainClass',
            "Implementation-Title": "Gradle",
            "Implementation-Version": archiveVersion
      )
    }
 }

-1
task <taskname>(type: Jar) {
    archiveName 'nameofjar.jar'
    doFirst {
    manifest {
            attributes 'Class-Path': configurations.compile.files.collect{ project.uri(it) }.join(' ')
        }
    }
}

1
关于此代码的功能的一些解释可能有助于解释某些特定的位?
汤姆(Tom)
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.