The Spring Boot Gradle Plugin provides Spring Boot support in Gradle, allowing you to
package executable jar or war archives, run Spring Boot applications and use the
dependency management provided by
spring-boot-dependencies
.
The
spring-boot
plugin automatically applies the
Dependency Management Plugin
and configures it to import
the
spring-boot-starter-parent
bom. This provides a similar dependency management
experience to the one that is enjoyed by Maven users. For example, it allows you to omit
version numbers when declaring dependencies that are managed in the bom. To make use of
this functionality, simply declare dependencies in the usual way, but leave the version
number empty:
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.thymeleaf:thymeleaf-spring4")
compile("nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect")
}
|
Note
|
The version of the
spring-boot
gradle plugin that you declare determines the
version of the
spring-boot-starter-parent
bom that is imported (this ensures that builds
are always repeatable). You should always set the version of the
spring-boot
gradle
plugin to the actual Spring Boot version that you wish to use. Details of the versions
that are provided can be found in the
appendix
.
|
To learn more about the capabilities of the Dependency Management Plugin, please refer to
its
documentation
.
Once the
spring-boot
plugin has been applied to your project it will automatically
attempt to rewrite archives to make them executable using the
bootRepackage
task
. You
should configure your project to build a jar or war (as appropriate) in the usual way.
The main class that you want to launch can either be specified using a configuration
option, or by adding a
Main-Class
attribute to the manifest. If you don’t specify a
main class the plugin will search for a class with a
public static void main(String[] args)
method.
To build and run a project artifact, you can type the following:
$ gradle build
$ java -jar build/libs/mymodule-0.0.1-SNAPSHOT.jar
To build a war file that is both executable and deployable into an external container,
you need to mark the embedded container dependencies as belonging to the war plugin’s
providedRuntime
configuration, e.g.:
...
apply plugin: 'war'
war {
baseName = 'myapp'
version = '0.5.0'
repositories {
jcenter()
maven { url "http://repo.spring.io/libs-snapshot" }
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
}
To run a project in place without building a jar first you can use the “bootRun” task:
$ gradle bootRun
If
devtools
has been added to your project
it will automatically monitor your application for changes. Alternatively, you can also
run the application so that your static classpath resources (i.e. in
src/main/resources
by default) are reloadable in the live application, which can be helpful at development
time.
bootRun {
addResources = true
}
Making static classpath resources reloadable means that
bootRun
does not use the output
of the
processResources
task, i.e., when invoked using
bootRun
, your application will
use the resources in their unprocessed form.
When
spring-boot
is applied to your Gradle project a default task named
bootRepackage
is created automatically. The
bootRepackage
task depends on Gradle
assemble
task, and
when executed, it tries to find all jar artifacts whose qualifier is empty (i.e. tests and
sources jars are automatically skipped).
Due to the fact that
bootRepackage
finds 'all' created jar artifacts, the order of
Gradle task execution is important. Most projects only create a single jar file, so
usually this is not an issue; however, if you are planning to create a more complex
project setup, with custom
Jar
and
BootRepackage
tasks, there are few tweaks to
consider.
If you are 'just' creating custom jar files from your project you can simply disable
default
jar
and
bootRepackage
tasks:
jar.enabled = false
bootRepackage.enabled = false
Another option is to instruct the default
bootRepackage
task to only work with a
default
jar
task.
bootRepackage.withJarTask = jar
If you have a default project setup where the main jar file is created and repackaged,
'and' you still want to create additional custom jars, you can combine your custom
repackage tasks together and use
dependsOn
so that the
bootJars
task will run after
the default
bootRepackage
task is executed:
task bootJars
bootJars.dependsOn = [clientBoot1,clientBoot2,clientBoot3]
build.dependsOn(bootJars)
All the above tweaks are usually used to avoid situations where an already created boot
jar is repackaged again. Repackaging an existing boot jar will not break anything, but
you may find that it includes unnecessary dependencies.
If you are
declaring
dependencies without versions
and you want to publish artifacts to a Maven repository
you will need to configure the Maven publication with details of Spring Boot’s
dependency management. This can be achieved by configuring it to publish poms that
inherit from
spring-boot-starter-parent
or that import dependency management from
spring-boot-dependencies
. The exact details of this configuration depend on how you’re
using Gradle and how you’re trying to publish the artifacts.