简体   繁体   English

如何使用 Gradle 6.3 将本地罐子包含到胖罐子中?

[英]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 :在运行 Java 8 和 Gradle 6.3 的 Linux 机器上,我需要构建一个由混合库组成的胖 jar,一些来自 Maven Central,其他来自位于我的存储库根目录的本地libs目录,以及我的build.gradlegradlew

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.运行./gradlew clean buildcd build/libs ,如果我unzip myproject.jar我可以看到 jar 中没有包含任何依赖项。

My end goal is to make my project executable as java -jar myproject.jar .我的最终目标是使我的项目可执行为java -jar myproject.jar How can I solve this?我该如何解决这个问题?

After unzipping your jar file check here for all dependencies.解压 jar 文件后,请在此处检查所有依赖项。

Your-Project --> BOOT-INF --> libs

by default, if your Gradle build is successful the jar files come here.

you can run java -jar then.然后你可以运行 java -jar 。

This is a build.gradle that generates a fat jar including local dependencies:这是一个build.gradle ,它生成一个包含本地依赖项的胖 jar:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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