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 omit version
information from your build.gradle
file for “blessed” dependencies.
To use the Spring Boot Gradle Plugin simply include a buildscript
dependency and apply
the spring-boot
plugin:
buildscript { dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:1.0.2.RELEASE") } } apply plugin: 'spring-boot'
If you are using a milestone or snapshot release you will also need to add appropriate
repositories
reference:
buildscript { repositories { maven.url "http://repo.spring.io/snapshot" maven.url "http://repo.spring.io/milestone" } // ... }
The spring-boot
plugin will register a custom Gradle ResolutionStrategy
with your
build that allows you to omit version numbers when declaring dependencies to “blessed”
artifacts. All artifacts with a org.springframework.boot
group ID, and any of the
artifacts declared in the managementDependencies
section of the
spring-dependencies
POM can have their version number resolved automatically.
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 |
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 a configuration named "providedRuntime", e.g:
... apply plugin: 'war' war { baseName = 'myapp' version = '0.5.0' } repositories { mavenCentral() maven { url "http://repo.spring.io/libs-snapshot" } } configurations { providedRuntime } 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
Running this way makes your static classpath resources (i.e. in src/main/resources
by
default) reloadable in the live application, which can be helpful at development time.
The gradle plugin automatically extends your build script DSL with a springBoot
element
for configuration. Simply set the appropriate properties as you would with any other Gradle
extension (see below for a list of configuration options):
springBoot { backupSource = false }
Sometimes it may be more appropriate to not package default dependencies resolved from
compile
, runtime
and provided
scopes. If the created executable jar file
is intended to be run as it is, you need to have all dependencies nested inside it;
however, if the plan is to explode a jar file and run the main class manually, you may already
have some of the libraries available via CLASSPATH
. This is a situation where
you can repackage your jar with a different set of dependencies.
Using a custom
configuration will automatically disable dependency resolving from
compile
, runtime
and provided
scopes. Custom configuration can be either
defined globally (inside the springBoot
section) or per task.
task clientJar(type: Jar) { appendix = 'client' from sourceSets.main.output exclude('**/*Something*') } task clientBoot(type: BootRepackage, dependsOn: clientJar) { withJarTask = clientJar customConfiguration = "mycustomconfiguration" }
In above example, we created a new clientJar
Jar task to package a customized
file set from your compiled sources. Then we created a new clientBoot
BootRepackage task and instructed it to work with only clientJar
task and
mycustomconfiguration
.
configurations {
mycustomconfiguration.exclude group: 'log4j'
}
dependencies {
mycustomconfiguration configurations.runtime
}
The configuration that we are referring to in BootRepackage
is a normal
Gradle
configuration. In the above example we created a new configuration named
mycustomconfiguration
instructing it to derive from a runtime
and exclude the log4j
group. If the clientBoot
task is executed, the repackaged boot jar will have all
dependencies from runtime
but no log4j
jars.
The following configuration options are available:
Name | Description |
---|---|
| The main class that should be run. If not specified the value from the manifest will be used, or if no manifest entry is the archive will be searched for a suitable class. |
| The name of the provided configuration (defaults to |
| If the original source archive should be backed-up before being repackaged (defaults
to |
| The name of the custom configuration. |
| The type of archive, corresponding to how the dependencies are laid out inside (defaults to a guess based on the archive type). |
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 disables
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.