使用 gradle 为 Spring Boot 创建一个库,而无需执行 bootJar
Create a librairie for Springboot using gradle without doing bootJar
我开发了一个 controllerAdvice,我想把它放在一个将被多个微服务使用的库中;
由于我的库正在使用 spring 注释,我正在为 springboot 添加插件和依赖项。但是当我添加
apply plugin: 'org.springframework.boot'
这在构建步骤中添加了一个 bootJar 步骤。 bootJar 要求我应该有一个主要的 class 用于 spring 引导,但我也不想要,因为我的 jar 只是一个库而不是 spring 引导应用程序。
这是我的 gradle.build 脚本的一部分:
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
group = 'com.test'
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
repositories {
mavenCentral()
maven {
url = artifactoryRepoUrl
}
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-actuator')
compile('org.springframework.boot:spring-boot-starter-web')
compile("org.springframework.boot:spring-boot-configuration-processor")
compile("org.springframework.boot:spring-boot-starter-hateoas")
testCompile('org.springframework.boot:spring-boot-starter-test')
compileOnly('org.projectlombok:lombok:1.18.6')
annotationProcessor('org.projectlombok:lombok:1.18.6')
}
我该如何解决这个问题?
听起来您会受益于单独使用 Spring Boot 的依赖管理。这将负责管理您的依赖项版本,同时让您的项目构建一个普通的 jar 而不是 Spring Boot fat jar。 Gradle 插件的参考文档 describes how to do this。第一步是依赖插件而不应用它。如果您使用 plugins
块,它看起来像这样:
plugins {
id 'org.springframework.boot' version '2.1.4.RELEASE' apply false
}
在您的情况下,删除 apply plugin: 'org.springframework.boot'
就足够了。
您已经在应用依赖管理插件,所以剩下的步骤是配置 Boot 的依赖管理:
dependencyManagement {
imports {
mavenBom org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES
}
}
这将允许您继续声明没有版本的依赖项。然后,您可以 运行 jar
任务来构建一个适合用作依赖项的普通 jar。
您需要做的就是禁用 bootJar
任务并启用 jar
任务(它创建一个普通的 jar 而不是可执行的 jar)。在您的 build.gradle 文件中添加以下代码段:
bootJar {
enabled = false
}
jar {
enabled = true
}
我开发了一个 controllerAdvice,我想把它放在一个将被多个微服务使用的库中; 由于我的库正在使用 spring 注释,我正在为 springboot 添加插件和依赖项。但是当我添加
apply plugin: 'org.springframework.boot'
这在构建步骤中添加了一个 bootJar 步骤。 bootJar 要求我应该有一个主要的 class 用于 spring 引导,但我也不想要,因为我的 jar 只是一个库而不是 spring 引导应用程序。
这是我的 gradle.build 脚本的一部分:
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
group = 'com.test'
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
repositories {
mavenCentral()
maven {
url = artifactoryRepoUrl
}
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-actuator')
compile('org.springframework.boot:spring-boot-starter-web')
compile("org.springframework.boot:spring-boot-configuration-processor")
compile("org.springframework.boot:spring-boot-starter-hateoas")
testCompile('org.springframework.boot:spring-boot-starter-test')
compileOnly('org.projectlombok:lombok:1.18.6')
annotationProcessor('org.projectlombok:lombok:1.18.6')
}
我该如何解决这个问题?
听起来您会受益于单独使用 Spring Boot 的依赖管理。这将负责管理您的依赖项版本,同时让您的项目构建一个普通的 jar 而不是 Spring Boot fat jar。 Gradle 插件的参考文档 describes how to do this。第一步是依赖插件而不应用它。如果您使用 plugins
块,它看起来像这样:
plugins {
id 'org.springframework.boot' version '2.1.4.RELEASE' apply false
}
在您的情况下,删除 apply plugin: 'org.springframework.boot'
就足够了。
您已经在应用依赖管理插件,所以剩下的步骤是配置 Boot 的依赖管理:
dependencyManagement {
imports {
mavenBom org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES
}
}
这将允许您继续声明没有版本的依赖项。然后,您可以 运行 jar
任务来构建一个适合用作依赖项的普通 jar。
您需要做的就是禁用 bootJar
任务并启用 jar
任务(它创建一个普通的 jar 而不是可执行的 jar)。在您的 build.gradle 文件中添加以下代码段:
bootJar {
enabled = false
}
jar {
enabled = true
}