使用gradle为弹簧靴生成多个震击器



我的项目是带gradle的spring-boot。我的目标是让spring-boot生成2个不同的boot jar。第一个罐子是今天创建的典型罐子,将用于生产系统。第二个jar将被其他系统用于集成测试。第二个jar将有一组不同的配置和依赖关系。其他人做过这个吗?我没有看到bootJar任务的任何简单配置,也没有成功地尝试基于bootJar创建自己的任务。

更新:以下是基于Francisco Mateo答案的解决方案

configurations {
integrationImplementation.extendsFrom implementation
integrationRuntimeOnly.extendsFrom runtimeOnly
//...
}

dependencies {
// ...
integrationRuntimeOnly 'com.h2database:h2'
// ...
}
sourceSets {
integration {
compileClasspath += sourceSets.main.output
runtimeClasspath += sourceSets.main.output
}
}
tasks.register("integrationBootJar", BootJar) {
description = "Assembles an executable JAR archive to be used for integration tests of other projects containing the main classes, their dependencies, and any other integrationImplementation or integrationRuntimeOnly dependencies."
group = 'build' 
classpath = sourceSets.main.runtimeClasspath.plus(sourceSets["integration"].runtimeClasspath)
mainClass.set("${jarMainClass}") // TODO can pull from bootJarMainClassName or bootRunMainClassName like bootJar?
archiveClassifier.set("integration")
shouldRunAfter bootJar
}
assemble.dependsOn integrationBootJar

您需要基本上复制Spring Boot插件创建bootJar的方式,如源代码中所示。大部分逻辑都包含在任务本身中,所以您所要做的就是创建另一个BootJar任务类型,并进行一些轻微的修改,主要是添加不属于主JAR的附加依赖项。

完整示例(未经测试(:

import org.springframework.boot.gradle.plugin.SpringBootPlugin
import org.springframework.boot.gradle.tasks.bundling.BootJar
plugins {
id("org.springframework.boot") version "2.6.4"
id("java")
}
group = "io.mateo"
java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(17))
}
}
repositories {
mavenCentral()
}
sourceSets {
register("integrationTest") {
compileClasspath += sourceSets.main.get().output
runtimeClasspath += sourceSets.main.get().output
}
}
val integrationTestImplementation by configurations.getting {
extendsFrom(configurations.implementation.get())
}
dependencies {
implementation(platform(SpringBootPlugin.BOM_COORDINATES))
implementation(platform("org.springframework.cloud:spring-cloud-dependencies:2021.0.1"))
implementation("org.springframework.boot:spring-boot-starter-web")
integrationTestImplementation("org.apache.commons:commons-lang3:3.12.0")
testImplementation("org.springframework.boot:spring-boot-starter-test")
}
tasks {
test {
useJUnitPlatform()
}
register("integrationTestBootJar", BootJar::class) {
description = "Assembles an integration test executable JAR archive containing the main classes and their dependencies."
group = BasePlugin.BUILD_GROUP
classpath = sourceSets.main.get().runtimeClasspath.plus(sourceSets["integrationTest"].runtimeClasspath)
mainClass.set("io.mateo.springdemo.SpringdemoApplication")
archiveClassifier.set("integration-test")
}
}

上面的集成测试配置实际上是取自Gradle文档的一个示例:https://docs.gradle.org/current/userguide/java_testing.html#sec:configuring_java_integration_tests

tasks { }中,您可以看到集成测试特定的Boot JAR的创建方式与Spring Boot插件创建bootJar的方式相同。然而,这里的关键区别在于添加了集成测试运行时类路径。

我用一个较短的表单成功了,我的任务是只重新定义mainClass,硬性要求是提供targetJavaVersion&classpath:

tasks.named("bootJar", BootJar.class).configure {
mainClass = "com.evil.app.ServerApp"
}
tasks.register("bootJarH2", BootJar.class) {
group = bootJar.group
description = bootJar.description
targetJavaVersion = bootJar.targetJavaVersion
classpath = bootJar.classpath
mainClass = "com.evil.app.H2App"
}

最新更新