首页 > 解决方案 > Gradle 依赖项没有被 Eclipse 导出到可运行的 jar

问题描述

我正在尝试将我的 gradle eclipse 项目导出到一个可运行的 jar,但是它的依赖项没有被捆绑到可运行的 jar 本身中。

我文件 -> 导出 -> 可运行的 jar -> 将所需的库提取到生成的 jar 中。

如果我从配置构建路径选项手动执行“添加外部 jar”,它们会正确导出,但是我想通过 gradle 执行此操作,并且如果没有手动添加,它们不会正确导出。

我试图做一个 gradlew clean 和 gradlew build。我刷新了我的 gradle 依赖项,并重建了项目,并通过 eclipse 项目 -> clean 选项对其进行了清理。

下面是我的 build.gradle 文件:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:2.1.4.RELEASE")
    }
}

plugins {
    id 'java'
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'


version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    compile("org.springframework.boot:spring-boot-starter-activemq")
    testCompile group: 'junit', name: 'junit', version: '4.12'
    compile 'org.json:json:20171018'
    compile 'org.mongodb:mongo-java-driver:3.6.4'
    compile group: 'com.github.jai-imageio', name: 'jai-imageio-core', version: '1.4.0'
    compile files ('../libs/CommonUtils.jar')
    compile files ('../libs/UMS.jar')
    compile files ('../libs/Cache.jar')
}

jar {
    from { configurations.runtime.collect { zipTree(it) } }
}

当我查看生成的 jar 时,我发现它不包含其中的依赖项,当我通过 java -jar jar.jar 运行 jar 时,我收到 NoClassDefFound 错误。我想将我所有的 gradle 依赖项捆绑到一个可运行的 jar 中,这样我就可以执行 java -jar jar.jar 并让我的 jar 运行,就像我在 eclipse 编辑器中点击运行一样。

标签: javaeclipsegradledependencies

解决方案


构建工具本身将创建可运行的 jar。

gradle clean
gradle taskName 

项目 Jar 将在文件夹下创建:

$project/build/libs/ folder. 

推荐阅读