简体   繁体   中英

How to generate a jar with internal dependencies bundled and external dependencies as external jars?

I have a Gradle project which depends on sub-projects. I would like to create a "fat jar" containing all my sub-projects, and externel dependencies as external jars.

build.gradle:

dependencies {
     compile project(':MyDep1')
     compile project(':MyDep2')
     compile 'com.google.guava:guava:18.0'
}

I would like to be able to generate the following output:

MyProject.jar -> Includes MyDep1 & MyDep2

libs/guavaXXX.jar -> Guava as external lib

I don't know how I could do this.

Use different configurations to hold your internal and external dependencies and package only one of those configurations into your project artifact.

configurations{
    internalCompile
    externalCompile
}

//add both int and ext to compile
configurations.compile.extendsFrom(internalCompile)
configurations.compile.extendsFrom(externalCompile)

dependencies{
    internalCompile project(':MyDep1')
    internalCompile project(':MyDep2')

    externalCompile 'com.google.guava:guava:18.0'
}

in your fat jar task, include only from internalCompile

I finally made it work with this solution:

jar {
    subprojects.each {
        from files(it.sourceSets.main.output)
    }
}

distributions {
    main {
        contents {
            exclude subprojects.jar.archivePath.name
        }
    }
}

In my project's jar, I include the content of all subprojects ouputs. In the distribution, I exclude the jar from subprojects (so it only contains dependencies). This is probably not the best way, but it's simple and it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM