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