spring-boot - 多个Gradle bootJar配置-不同的mainClass属性?

标签 spring-boot gradle kotlin

我有一个使用.kts( Kotlin 风格)Gradle构建文件的Gradle / Spring Boot构建。 (最后一部分可能没有关系。)

我的Spring Boot应用程序具有两种不同的类可用于启动它,一种用于“客户端”模式,另一种用于“服务器”模式。

我有一个bootJar任务,看起来像:

tasks.bootJar {
    mainClassName = "com.me.ClientApplication"
}

因此,默认情况下,当我运行./gradlew build bootJar时,我获得了JAR的客户端版本,当使用java -jar theJar.jar运行时,会执行ClientApplication类。

但是,我还要发布另一个具有不同mainClassName并运行com.me.ServerApplication第二个JAR

我应该如何处理?我对Gradle还是很陌生-我应该在mainClassName命令中提供./gradlew来覆盖它吗?还是可以定义单独的任务(例如clientBootJarserverBootJar)来生成单独的 Artifact ?

最佳答案

由于您的配置是互斥的(不能有两个主文件),所以我想说有两个任务是最佳的:

tasks.bootJarClient {
    mainClassName = "com.me.ClientApplication"
}

tasks.bootJarClient {
    mainClassName = "com.me.ServerApplication"
}

另一种方法是使用属性,但是如果您有更大的差异,则更有意义。

现在要执行自定义任务,请在build.gradle.kts中添加以下内容(未使用SpringBoot专门对其进行测试):
open class BootJarClient : BootJar() {
   override mainClassName = "YourClientClassName"
}

tasks.register<BootJarClient>("bootJarClient") {
    group = "Other"
}

// Server is basically the same

属性的命名来自GitHub sources

关于spring-boot - 多个Gradle bootJar配置-不同的mainClass属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57646289/

相关文章:

Grails 3.2.4 : custom authentication filter not called

java - gRpc,客户端收到 io.grpc.StatusRuntimeException : UNIMPLEMENTED: Method even when the message is received and deserialized by the server

gradle - 无法使用gradle将package.html导入javadoc

kotlin - Flow的collect会阻塞执行吗?

android - Kotlin - 高阶函数成本?

spring-boot - 如何避免使用 Zuul 重定向到另一个主机?

java - Spring 启动: how to use @PropertySource to point to a text file outside the jar?

android - Gradle:如何只为 android 生成 apk,而不包括其他平台,如 iOS、HTML 等

gradle - 在vscode中调试Gradle + Cucumber时不会触发断点

Kotlin 注释多个目标