【发布时间】: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 编辑器中运行一样运行。
【问题讨论】:
-
如果要构建应用程序/库,每次都需要启动 IDE 并执行手动任务,那么使用构建工具有什么意义?使用 gradle 构建您的应用程序,而不是使用 Eclipse。 docs.gradle.org/current/userguide/application_plugin.html
标签: java eclipse gradle dependencies