【问题标题】:Gradle dependencies not being exported to runnable jar by eclipseEclipse 未将 Gradle 依赖项导出到可运行的 jar
【发布时间】:2019-07-13 12:46:22
【问题描述】:

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

I 文件 -> 导出 -> 可运行的 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 编辑器中运行一样运行。

【问题讨论】:

标签: java eclipse gradle dependencies


【解决方案1】:

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

gradle clean
gradle taskName 

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

$project/build/libs/ folder. 

【讨论】:

  • 我不知道为什么这个答案被接受。在问题的构建脚本上调用 gradle fatJar 应该会失败,因为没有定义名为 fatJar 的任务。
  • 此评论解决了用户问题,因此用户接受了此答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-08
  • 1970-01-01
  • 2017-05-13
  • 2021-10-17
  • 1970-01-01
  • 2017-02-01
相关资源
最近更新 更多