Kirill
Kirill

Reputation: 8088

How should i set main class for Spring Boot application in Gradle?

I read spring boot documentation and i see there are at least 2 ways to set main class:

bootRepackage {
    mainClass = 'demo.Application'
}

and

springBoot {
    mainClass = "demo.Application"
}

Which one should i use or they are both required for different tasks? I do not want to repeat myself.

Upvotes: 1

Views: 5112

Answers (2)

Andy Wilkinson
Andy Wilkinson

Reputation: 116301

In Gradle terms, springBoot is an extension. When you use it to configure the main class, you're configuring it for every repackaging task in the project. On the other hand bootRepackage is referencing a single repackaging task so you're just configuring the mainClass for that one task.

Which one should i use or they are both required for different tasks?

If you only have a single repackaging task (as is the default) this is a matter of personal preference.

If you have configured additional repackaging tasks, you are probably better configuring it on each individual task rather than using the springBoot extension. If you use a mixture of the two, the setting on an individual repackaging task will take precedence over whatever you have configured using springBoot.

Upvotes: 2

river
river

Reputation: 814

This is from documentation for springBoot plugin:

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):

And below that there is an example of configuration of mainClass element of bootRepackage plugin:

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.

In other words those two are identical.

Upvotes: 0

Related Questions