Is it possible to create an "uber" jar containing the project classes and the project dependencies as jars with a custom manifest file?

Viewed 30963

I'm trying to create a executable jar(using maven) that contains the project classes and it's dependencies with a manifest file that has the entry for the main class and the class path entry that points to the dependencies packed in the root of the jar;something like this :

Manifest File:

.....
Main-Class : com.acme.MainClass
Class-Path : dependecy1.jar dependecy2.jar
.....

Jar:

jar-root
|-- ....
|-- com/acme/../*.class
|-- dependecy1.jar
`-- dependecy2.jar

I'm using the maven-jar-plugin to create the manifest file and the maven-shade-plugin to create the "uber" jar but the dependencies are unpacked and added as classes to my jar.

4 Answers
task uberJar(type: Jar) {
    manifest {
        attributes 'Main-Class': 'main.RunTest'
    }
    dependsOn configurations.runtimeClasspath
    from sourceSets.main.output
    from sourceSets.test.output
    from sourceSets.main.resources
    from sourceSets.test.resources
    from {
        configurations.runtimeClasspath.findAll { it.name.endsWith('jar') }.collect { zipTree(it) }
    }
    from {
        project(':someothermoudule').sourceSets.main.resources // If current project depends on some other modules
        project(':someothermoudule').sourceSets.test.resources // If current project depends on some other modules
    }
    exclude 'META-INF/*.RSA'
    exclude 'META-INF/*.SF'
    exclude 'META-INF/*.DSA'
}

In case of gradle above can help. Note the from { project('')} lines, this is only required when the current project depends on some other sub projects or modules.

Related