0

On a Linux machine running Java 8 and Gradle 6.3 I need to build a fat jar made of mix of libraries, some sourced from Maven Central, others from a local libs directory located at the root of my repository, together with my build.gradle and the gradlew:

apply plugin: 'java' repositories { mavenCentral() flatDir { dirs 'libs' } } sourceCompatibility = 1.8 targetCompatibility = 1.8 task copyLibs(type: Copy) { from configurations.compile into 'libs' } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) testImplementation "junit:junit:4.12" } 

After running ./gradlew clean build and cd build/libs, if I unzip myproject.jar I can see that no dependencies have been included in the jar.

My end goal is to make my project executable as java -jar myproject.jar. How can I solve this?

2 Answers 2

2

This is a build.gradle that generates a fat jar including local dependencies:

plugins { id 'java' } sourceCompatibility = 1.8 targetCompatibility = 1.8 group 'com.mycompany.foo' version '1.0' jar { archiveBaseName = 'myjarname' archiveVersion = '0.1.0' manifest { attributes( 'Main-Class': 'com.mycompany.foo.Main' ) } from { configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } } } repositories { mavenCentral() } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) testImplementation'org.junit.jupiter:junit-jupiter-api:5.7.2' testImplementation 'org.assertj:assertj-core:3.20.2' } 
Sign up to request clarification or add additional context in comments.

Comments

1

After unzipping your jar file check here for all dependencies.

Your-Project --> BOOT-INF --> libs by default, if your Gradle build is successful the jar files come here. 

you can run java -jar then.

1 Comment

I have a META-INF/ containing only a MANIFEST.FM.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.