theAnonymous
theAnonymous

Reputation: 1804

How to uberjar SPECIFIC dependencies?

The typical answer for uberjar is the following code:

build.gradle for project

manifest {
    attributes("Main-Class": "main.Main" )
}
jar{

    //<editor-fold defaultstate="collapsed" desc="uberjar">
//uberjar start
    from(configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }) {
        exclude "META-INF/*.SF"
        exclude "META-INF/*.DSA"
        exclude "META-INF/*.RSA"
    }
//uberjar end
    //</editor-fold>
}

From what I observe, this works by putting in all the jars in maven local.

How do I make it uberjar ONLY the dependencies, or only the dependencies that I choose?

Upvotes: 0

Views: 146

Answers (1)

miskender
miskender

Reputation: 7968

You can use include or exclude configuration(or both if it suits your needs) to specify which packages you want in your uberjar.

Example:

task customJar(type: Jar) {
   .....
   exclude('com/somePack/excludePack1/**') 
   exclude('com/somePack/excludePack2/**') 

   include('com/somePack/**')

    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
    with jar
}
// this example will include only packages with com/somePack/
// since exclude configuration specifies com/somePack/excludePack1/ and 
// com/somePack/excludePack2/ packages to be excluded. 
// any other packages under com/somePack/ will be included in the jar.

If you use include it will only contain the packages that matches include definitions.

If you use exclude, it wont containt any packages that mathces exlude defitions.

You can choose to exclude your own source codes like this. I suggest creating a custom jar task and executing this task for this kind of things. (Ex: gradle yourJarTaskName)

Upvotes: 1

Related Questions