使用 gradle 为 Spring Boot 生成多个 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
My project is spring boot with gradle. My goal is to have spring boot generate 2 different boot jars. The first jar is the typical jar created today which would be used in production systems. The second jar would be used by other systems for integration testing. The second jar would have a different set of configuration and dependencies. Has anyone else done this? I did not see any simple configuration for the bootJar
task nor was i successful trying to create my own task based on bootJar
.
UPDATE: Below is the solution which worked based upon Francisco Mateo's answer.
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要基本上复制 Spring Boot 插件创建
bootJar
的方式,如 源。大多数逻辑都包含在任务本身中,因此您所要做的就是创建另一个BootJar
任务类型并进行一些细微的修改,主要是添加不属于主 JAR 的其他依赖项。完整示例(未经测试):
上面的集成测试配置实际上是取自 Gradle 文档的示例: https://docs.gradle.org/current/userguide/java_testing.html#sec:configuring_java_integration_tests
在
任务中{ }
您可以看到集成测试特定 Boot JAR 的创建位置,与 Spring Boot 插件创建bootJar
的方式相同。然而,这里的主要区别是添加了集成测试运行时类路径。You would need to essentially duplicate the way the Spring Boot plugin creates
bootJar
as shown here in the source. Most of the logic is contained within the task itself, so all you have to do is create anotherBootJar
task type with some slight modifications, mainly adding additional dependencies that are not part of the main JAR.Complete example (untested):
The above integration test configuration is actually an example taken from the Gradle documentation: https://docs.gradle.org/current/userguide/java_testing.html#sec:configuring_java_integration_tests
In the
tasks { }
you can see where the integration test specific Boot JAR is created the same way the Spring Boot plugin createsbootJar
. However, the key difference here is addition of the integration test runtime classpath.我成功地使用了更简短的形式,我的任务是仅重新定义
mainClass
,硬性要求是提供targetJavaVersion
和类路径:I succeeded with a shorter form, my task was to redefine only
mainClass
, hard requirements were to supplytargetJavaVersion
&classpath
: