spring-boot/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/docs/asciidoc/using.adoc

98 lines
4.6 KiB
Plaintext

[[using]]
= Using the Plugin
Maven users can inherit from the `spring-boot-starter-parent` project to obtain sensible defaults.
The parent project provides the following features:
* Java 1.8 as the default compiler level.
* UTF-8 source encoding.
* Compilation with `-parameters`.
* A dependency management section, inherited from the `spring-boot-dependencies` POM, that manages the versions of common dependencies.
This dependency management lets you omit `<version>` tags for those dependencies when used in your own POM.
* An execution of the <<goals.adoc#goals-repackage, `repackage` goal>> with a `repackage` execution id.
* Sensible https://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html[resource filtering].
* Sensible plugin configuration (https://github.com/ktoso/maven-git-commit-id-plugin[Git commit ID], and https://maven.apache.org/plugins/maven-shade-plugin/[shade]).
* Sensible resource filtering for `application.properties` and `application.yml` including profile-specific files (for example, `application-dev.properties` and `application-dev.yml`)
NOTE: Since the `application.properties` and `application.yml` files accept Spring style placeholders (`${...}`), the Maven filtering is changed to use `@..@` placeholders.
(You can override that by setting a Maven property called `resource.delimiter`.)
[[using.parent-pom]]
== Inheriting the Starter Parent POM
To configure your project to inherit from the `spring-boot-starter-parent`, set the `parent` as follows:
[source,xml,indent=0,subs="verbatim,quotes,attributes"]
----
<!-- Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>{gradle-project-version}</version>
</parent>
----
NOTE: You should need to specify only the Spring Boot version number on this dependency.
If you import additional starters, you can safely omit the version number.
With that setup, you can also override individual dependencies by overriding a property in your own project.
For instance, to use a different version of the SLF4J library and the Spring Data release train, you would add the following to your `pom.xml`:
[source,xml,indent=0,subs="verbatim,attributes",tabsize=4]
----
include::../maven/using/different-versions-pom.xml[tags=different-versions]
----
Browse the {version-properties-appendix}[`Dependency versions Appendix`] in the Spring Boot reference for a complete list of dependency version properties.
[[using.import]]
== Using Spring Boot without the Parent POM
There may be reasons for you not to inherit from the `spring-boot-starter-parent` POM.
You may have your own corporate standard parent that you need to use or you may prefer to explicitly declare all your Maven configuration.
If you do not want to use the `spring-boot-starter-parent`, you can still keep the benefit of the dependency management (but not the plugin management) by using an `import` scoped dependency, as follows:
[source,xml,indent=0,subs="verbatim,attributes",tabsize=4]
----
include::../maven/using/no-starter-parent-pom.xml[tags=no-starter-parent]
----
The preceding sample setup does not let you override individual dependencies by using properties, as explained above.
To achieve the same result, you need to add entries in the `dependencyManagement` section of your project **before** the `spring-boot-dependencies` entry.
For instance, to use a different version of the SLF4J library and the Spring Data release train, you could add the following elements to your `pom.xml`:
[source,xml,indent=0,subs="verbatim,attributes",tabsize=4]
----
include::../maven/using/no-starter-parent-override-dependencies-pom.xml[tags=no-starter-parent-override-dependencies]
----
[[using.overriding-command-line]]
== Overriding settings on the command-line
The plugin offers a number of user properties, starting with `spring-boot`, to let you customize the configuration from the command-line.
For instance, you could tune the profiles to enable when running the application as follows:
[indent=0]
----
$ mvn spring-boot:run -Dspring-boot.run.profiles=dev,local
----
If you want to both have a default while allowing it to be overridden on the command-line, you should use a combination of a user-provided project property and MOJO configuration.
[source,xml,indent=0,subs="verbatim,attributes",tabsize=4]
----
include::../maven/using/default-and-override-pom.xml[tags=default-and-override]
----
The above makes sure that `local` and `dev` are enabled by default.
Now a dedicated property has been exposed, this can be overridden on the command-line as well:
[indent=0]
----
$ mvn spring-boot:run -Dapp.profiles=test
----