4
votes

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.

2

2 Answers

1
votes

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

0
votes

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.