简体   繁体   中英

How to include local jars into a fat jar using Gradle 6.3?

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?

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.

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'
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM