Nice programing

gradle-다른 jar와 함께 lib dir이있는 jar를 어떻게 빌드합니까?

nicepro 2020. 11. 24. 19:53
반응형

gradle-다른 jar와 함께 lib dir이있는 jar를 어떻게 빌드합니까?


Gradle에서-lib 디렉토리 (특히 lib / enttoolkit.jar 및 lib / mail.jar)의 빌드 출력 jar에 jar를 어떻게 포함시킬 수 있습니까?


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.compile을 configuration.runtime으로 바꾸십시오.

편집 : 필요한 항아리 만 선택

새로운 구성을 만드십시오. releaseJars는 아마도

configurations {
    releaseJars
}

해당 구성에 원하는 항아리를 추가하십시오.

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

그런 다음 위에서 설명한 jar 작업에서 해당 구성을 사용하십시오.


libs프로젝트 의 디렉토리 안에 모든 jar가있는 경우 (라고 부릅니다 ) 다음 만 필요합니다.

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

단순한:

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와 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 및 "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 유형입니다.

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에서 테스트 됨


I needed to the same thing you asked, and used this method. you may not need a custom configuration declaration, but i needed to separate the locally used jar files from those declared in a super-build 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)

I had the same problem. I solved it like this:

Copy the files into the lib folder with copyToLib and reference the dependency with the 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)

In my case I needed to include a contents of the root project Jar into subproject Jar. So, to make it work, one 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
}

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

참고URL : https://stackoverflow.com/questions/3445825/gradle-how-do-i-build-a-jar-with-a-lib-dir-with-other-jars-in-it

반응형