首页 文章

我应该如何在Gradle中为Spring Boot应用程序设置主类?

提问于
浏览
1

我读了 Spring 季启动文档,我看到至少有两种方法来设置主类:

bootRepackage {
    mainClass = 'demo.Application'
}

springBoot {
    mainClass = "demo.Application"
}

我应该使用哪一个,或者它们都是不同任务所必需的?我不想重复自己 .

2 回答

  • 1

    在Gradle术语中, springBoot 是一个扩展名 . 当您使用它来配置主类时,您将为项目中的每个重新打包任务配置它 . 另一方面, bootRepackage 正在引用单个重新打包任务,因此您只需为该一个任务配置 mainClass .

    我应该使用哪一个,或者它们都是不同任务所必需的?

    如果您只有一个重新包装任务(默认情况下),这是个人偏好的问题 .

    如果您已配置其他重新打包任务,则可能更好地在每个单独的任务上配置它而不是使用 springBoot 扩展 . 如果使用两者的混合,则单个重新打包任务的设置将优先于使用 springBoot 配置的任何设置 .

  • 0

    这是来自springBoot插件的文档:

    The gradle plugin automatically extends your build script DSL with a 
    springBoot element for global configuration of the Boot plugin. Set 
    the appropriate properties as you would with any other Gradle 
    extension (see below for a list of configuration options):
    

    在下面有一个配置bootRepackage插件的mainClass元素的例子:

    The main class that should be run. If not specified, and you 
    have applied the application plugin, the mainClassName project 
    property will be used. If the application plugin has not been 
    applied or no mainClassName has been specified, the archive will 
    be searched for a suitable class. "Suitable" means a unique class 
    with a well-formed main() method (if more than one is found the 
    build will fail). If you have applied the application plugin, 
    the main class can also be specified via its "run" task 
    (main property) and/or its "startScripts" task 
    (mainClassName property) as an alternative to using the 
    "springBoot" configuration.
    

    换句话说,这两个是相同的 .

相关问题