Вопрос

I have Gradle JAR (Main) project. I have other commons-io jars (dependent) that I would like to package them with the Main JAR. I tried below, but when I decompile Main JAR, I see all the dependent JARS inside libs folder with .jar extension.

jar {
    from("$projectDir") {
        include 'libs/**'
    }
}

What I want is the class files from all the dependent JARs into Main JAR. I am doing this because the Main JAR is going to be used by multiple projects. So that way, I do not have to include all the dependent JARs.

Thanks

Это было полезно?

Решение

As far as I know You cannot pack other jars into single jar just like that. What You need to do is to extract all the jars and pack the content into single file.

Following piece of code prepares such jar for declared dependencies

apply plugin: 'java'

repositories {
    mavenCentral()
}

dependencies {
    compile 'com.google.guava:guava:16.0.1'
    compile 'com.google.inject:guice:3.0'
}

task fatJar(type: Jar, dependsOn: jar) {
  baseName = project.name + '-fat'
  deps = configurations.runtime.collect { it.isDirectory() ? it : zipTree(it) }
  from(deps) { 
    exclude 'META-INF/MANIFEST.MF'
  }
}

Другие советы

use this.

into(libForderName){
        from {
            configurations.compile.collect {
                it.isDirectory() ? it : it.getAbsoluteFile()
            }
        }
    }
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top