spring-boot - 使用 gradle 为 spring boot 生成多个 jar

标签 spring-boot gradle jar

我的项目是带有gradle的spring boot。我的目标是让 Spring Boot 生成 2 个不同的启动 jar。第一个 jar 是今天创建的典型 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 的方式,如 source 中所示。 。大多数逻辑都包含在任务本身中,因此您所要做的就是创建另一个 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 的方式相同。然而,这里的主要区别是添加了集成测试运行时类路径。

关于spring-boot - 使用 gradle 为 spring boot 生成多个 jar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71387297/

相关文章:

java - 无法从 9.0.1 intellij (windows) 确定 java 版本

java - 如何在可执行 JAR 中加载 TestNG 配置?

java - 如何修复 : Error creating bean with name : Unsatisfied dependency expressed through field

java - 无法在 Android Studio 中使用 getFont

spring-boot - 无法获取 Jedis 连接,嵌套异常 : Could not get a resource from the pool

gradle - 大量静态资源重载问题

java - Runnable .jar 和 Eclipse run 之间的行为非常不同

java - Inno 安装程序 : Removing files installed by previous version

java - Spring Data 中的自定义方法实现失败并出现属性未找到错误

spring-boot - 在 @ComponentScan 中使用多个路径不起作用