【发布时间】:2017-08-20 03:28:32
【问题描述】:
我有一个 Spring MVC 项目,在 Spring Tool Suite 中使用 Spring Boot 1.5.2 和 Gradle Buildship。
- 如何仅从我的源创建一个 JAR 文件,该文件将在另一台服务器上运行并在那里下载所需的依赖项?
- 如何创建包含所有源文件和依赖项的胖 JAR?
我的gradle.build 文件是:
buildscript {
ext {
springBootVersion = '1.5.2.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'application'
mainClassName = "com.jtv.elastic.mvc.ElasticSpringApplication"
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
【问题讨论】:
-
Spring Boot 已经创建了一个包含所有必要依赖项的胖 JAR,它是可执行的。那么你到底有什么问题呢?
-
@dunni 在哪个文件夹中可以找到胖 JAR?另请回答问题的第一部分。
-
您可以在 Gradle 的输出文件夹中找到 JAR(我不确定它是称为 build 还是 target,但如果您查看项目文件夹,您应该会找到它)。关于第一个问题:我不完全了解您想要实现的目标。 Gradle 已经下载了你所有的依赖项。如果你想在你的应用程序中这样做,你必须自己构建它(并且搞乱类加载器等,所以我不知道为什么有人想要这样做)。
-
然后从你的 build.gradle 中删除 spring-boot-gradle-plugin,Gradle 只会用你的类构建一个 JAR。
-
不,当然不是。 Gradle 是一个构建系统,不打算在运行时环境中使用。这就是为什么 Spring Boot 默认将所有必要的依赖项打包在 JAR 中的原因。如果你想在你的服务器上使用 Gradle,你必须自己编写脚本。
标签: spring gradle spring-boot