Stack length shared two previous articles on the Spring Boot package run:

  • Three ways to run a Spring Boot application
  • How can Spring Boot install an executable Jar package?

I haven’t had a chance to explain the Spring Boot Maven plugin in detail.

Spring Boot has always supported Maven friendly, and the stack length has always used Maven for dependency and project management.

With the Spring Boot Maven Plugin, we can make projects into executable jar packages (*.jar) and war packages (*.war), which can help us easily run Spring Boot applications.

Official Address:

Docs. Spring. IO/spring – the boot…

They mainly include the following goals

  • spring-boot:run

You can run the Spring Boot application without packaging.

  • spring-boot:repackage

Executable run package (.jar/\.war)

  • spring-boot:start/ spring-boot:stop

For managing the life cycle of Spring applications (for example, for integration testing).

  • spring-boot:build-info

Used to generate build information for Spring Boot Actuator.

How to use

The following is mainly about the use of the first two goals, the latter two are used less.

Spring Boot Maven Plugin:

<build> ... <plugins> ... <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> < version > 2.2.6. RELEASE < / version > < / plugin >... </plugins> ... </build>Copy the code

1. Run the Spring Boot application directly

On the Maven command line:

mvn spring-boot:runCopy the code

In IDE development tools, you can omit the MVN command:

Of course, you can run the Main class directly, but using the Maven plugin has many more features, such as switching between profiles and replacing resources with Maven resources.

By default, the plug-in runs in a new process, and JVM parameters set on the command line are not valid and need to be specified separately:

-Dspring-boot.run.jvmArguments="-Dspring.profiles.active=dev"Copy the code

You can also specify other parameters:

  • SystemPropertyVariables: systemPropertyVariables
  • EnvironmentVariables: environmentVariables

Other JVM parameters can be specified after the command.

2, into the executable package

Take a look at a complete example:

<build> ... <plugins> ... <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> < version > 2.2.6. RELEASE < / version > < configuration > < mainClass > ${start - class} < / mainClass > < layout > jar < / layout > </configuration> <executions> <goals> <goal>repackage</goal> </goals> <configuration> <classifier>exec</classifier> </configuration> </execution> </execution> </plugin>... </plugins> ... </build>Copy the code

The following parameters are optional.

Repackage:

The most important thing is to add the Repackage goal for repackaging.

Layout:

The default layout property is JAR/WAR, depending on the project type.

  • JAR: executable JAR package;
  • WAR: Executable WAR package;
  • ZIP (alias: DIR) : similar to the JAR package, uses the PropertiesLauncher;
  • NONE: Package all dependencies and project resources. Do not bind any boot loader;

Classifier:

By default, only one package is included, but if the module is dependent on other templates and needs to be included as an executable package, then you need to specify another alias package with this tag, such as:

  • xxx.jar
  • xxx-exec-jar

Specific reference:

Docs. Spring. IO/spring – the boot…

The Spring Boot package plugin is often used, so you should learn how to use it. Otherwise, when you go to an interview, the interviewer will ask you how to package the Spring Boot project, you will be faced with MB.

In addition, if you are not familiar with Maven, you can reply to: Maven in the background of Java Technology Stack. I have shared a series of core tutorials before.

Ok, today’s share is here, more methods we can use official learning, think this article is good don’t forget to point to see and forward oh!

Read more on my blog:

1.Java JVM, Collections, Multithreading, new features series tutorials

2.Spring MVC, Spring Boot, Spring Cloud series tutorials

3.Maven, Git, Eclipse, Intellij IDEA series tools tutorial

4.Java, backend, architecture, Alibaba and other big factory latest interview questions

Life is good. See you tomorrow