Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a runnable jar with gradle that depends on three jars?

Tags:

gradle

I have a java program with a simple main class that depends on libraries a.jar, b.jar, c.jar. How do I make it such that I can create a runnable jar file with all those jars properly packaged?

I know in the jar task, you need to include:

apply plugin: 'java'
apply plugin:'application'

repositories {
    mavenCentral()
}
jar {
    manifest {
        attributes 'Main-Class': 'com.foo.bar.MainClass'
    }
}

But do not know what to do with the three external jars that my code uses.

like image 391
Rolando Avatar asked Jan 18 '26 10:01

Rolando


1 Answers

The easiest way is to merge the dependency Jars into the main Jar:

jar {
    from "path/to/jar1", "path/to/jar2"
}

Or, if the Jars are retrieved from a Maven/Ivy repository:

jar {
    from configurations.runtime
}

Alternatively, you can use a plugin such as gradle-onejar, which covers more use cases.

like image 115
Peter Niederwieser Avatar answered Jan 21 '26 03:01

Peter Niederwieser



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!