The Spring Boot Maven Plugin provides Spring Boot support in Maven, allowing you to package executable jar or war archives and run an application “in-place”. To use it you must be using Maven 3 (or better).
To use the Spring Boot Maven Plugin simply include the appropriate XML in the plugins
section of your pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <!-- ... --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>1.0.1.RELEASE</version> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
This configuration will repackage a jar or war that is built during the package
phase of
the Maven lifecycle. The following example shows both the repackaged jar, as well as the
original jar, in the target
directory:
$ mvn package $ ls target/*.jar target/myproject-1.0.0.jar target/myproject-1.0.0.jar.original
If you don’t include the <execution/>
configuration as above, you can run the plugin on
its own (but only if the package goal is used as well). For example:
$ mvn package spring-boot:repackage $ ls target/*.jar target/myproject-1.0.0.jar target/myproject-1.0.0.jar.original
If you are using a milestone or snapshot release you will also need to add appropriate
pluginRepository
elements:
<pluginRepositories> <pluginRepository> <id>spring-snapshots</id> <url>http://repo.spring.io/snapshot</url> </pluginRepository> <pluginRepository> <id>spring-milestones</id> <url>http://repo.spring.io/milestone</url> </pluginRepository> </pluginRepositories>
Once spring-boot-maven-plugin
has been included in your pom.xml
it will automatically
attempt to rewrite archives to make them executable using the spring-boot:repackage
goal. You should configure your project to build a jar or war (as appropriate) using the
usual packaging
element:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <!-- ... --> <packaging>jar</packaging> <!-- ... --> </project>
Your existing archive will be enhanced by Spring Boot during the package
phase. 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 in the usual way. 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:
$ mvn package $ java -jar target/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 “provided”, e.g:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <!-- ... --> <packaging>war</packaging> <!-- ... --> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <!-- ... --> </dependencies> </project>
The following configuration options are available for the spring-boot:repackage
goal:
Name | Description |
---|---|
| Directory containing the generated archive (defaults to |
| Name of the generated archive (defaults to |
Name | Description |
---|---|
| Classifier to add to the generated artifact. If given, the artifact will be attached. If
this is not given, it will merely be written to the output directory according to the
|
| The name of the main class. If not specified will search for a single compiled class
that contains a |
| The type of archive (which corresponds to how the dependencies are laid out inside it). Defaults to a guess based on the archive type. |
The plugin rewrites your manifest, and in particular it manages the Main-Class
and
Start-Class
entries, so if the defaults don’t work you have to configure those there
(not in the jar plugin). The Main-Class
in the manifest is actually controlled by the
layout
property of the boot plugin, e.g.
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>1.0.1.RELEASE</version> <configuration> <mainClass>${start-class}</mainClass> <layout>ZIP</layout> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin>
The layout property defaults to a guess based on the archive type (jar or war). For the
PropertiesLauncher
the layout is “ZIP” (even though the output might be a jar file).
Tip | |
---|---|
The executable jar format is described in the appendix. |
The Spring Boot Maven Plugin includes a run
goal which can be used to launch your
application from the command line. Type the following from the root of your Maven
project:
$ mvn spring-boot:run
By default, any src/main/resources
folder will be added to the application classpath
when you run via the maven plugin. This allows hot refreshing of resources which can be
very useful when developing web applications. For example, you can work on HTML, CSS or
JavaScipt files and see your changes immediately without recompiling your application. It
is also a helpful way of allowing your front end developers to work without needing to
download and install a Java IDE.
The following configuration options are available for the spring-boot:run
goal:
Name | Description |
---|---|
| Directory containing the classes and resource files that should be packaged into the
archive (defaults to |
Name | Description |
---|---|
| Arguments that should be passed to the application. |
| Add Maven resources to the classpath directly, this allows live in-place editing or
resources. Since resources will be added directly, and via the target/classes folder
they will appear twice if |
| The name of the main class. If not specified the first compiled class found that contains a main method will be used. |
| Folders that should be added to the classpath (defaults to
|