Arjun Patel
Arjun Patel

Reputation: 353

How to include jars from libs folder to another jar?

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

Upvotes: 1

Views: 2928

Answers (2)

SJY
SJY

Reputation: 1

use this.

into(libForderName){
        from {
            configurations.compile.collect {
                it.isDirectory() ? it : it.getAbsoluteFile()
            }
        }
    }

Upvotes: -1

Opal
Opal

Reputation: 84756

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

Upvotes: 2

Related Questions