Knowledge changes fate, stroke code makes me happy, 2020 continue to walk in the open source community click “like” to see, form a habit to give me a Star, click to understand the componentialized interface services based on SpringBoot landing solution

After the application is written, there is an important stage is release, when we release the application needs to be packaged, so how to package the application written by SpringBoot?

Recommended reading

  • Springboot2.x Basics: Developing your first SpringBoot application

packaging

Application distribution generally takes two forms.

The more traditional approach is external Tomcat, which packages the application as a xx.war file containing only the compiled.class and configuration files of the application’s source code.

In addition, SpringBoot also provides another efficient packaging method. By configuring maven plugin in POm. XML, all files in SRC /main/ Java, SRC /main/resources directories will be packaged when executing MVN package command. Jar file is generated, because SpringBoot packages the Tomcat dependencies into xx.jar by default, so you can directly run the Java -jar xx.jar command line.

Packaging plug-in

When we create a SpringBoot project with IDEA, the packaged Maven plugin is typically added to the POM.xml file by default, as shown below:

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

Note: If you do not build SpringBoot applications using spring-boot-starter-parenter, you need to configure them manually. For details about how to use the Plugin, see the SpringBoot Maven Plugin

Perform packaging

The packaging of SpringBoot applications built using Maven is simple, we just need to execute the MVN package in the root directory of the application, as shown below:

➜ gear-first-application git:(2.x) MVN package [INFO] Scanning for projects... [INFO] [INFO] ----------< org.minbox.chapter:developing-first-application >----------- [INFO] Building Developing - first - application 0.0.1 - the SNAPSHOT [INFO] -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- [jar ] -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- to omit part of the compiler log... [INFO] -- Maven-jar-plugin :3.1.2:jar (default-jar) @ gear-first-application -- [INFO] Building JAR: maven-jar-plugin:3.1.2: JAR (default-jar) @ gear-first-application -- /Users/yuqiyu/study/article-source-code/spring-boot-chapter/developing-first-application/target/developing-first-applica Tion - 0.0.1 - the SNAPSHOT. Jar [INFO] [INFO] - spring - the boot - maven - plugin: 2.2.4. RELEASE: repackage (repackage) @ developing-first-application --- [INFO] Replacing main artifact with repackaged archive [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- [INFO] Total time: 4.711 s [INFO] Finished at: 2020-02-20T15:02:24+08:00 [INFO] ------------------------------------------------------------------------Copy the code

When BUILD SUCCESS appears on the console, our package has been successful and the executable Jar for the current application has been generated, located in the target directory.

Package file naming

The default file name generated after the spring-boot-Maven-plugin is packaged is in the format of +.jar, for example: If this is not the format you want, you can customize it as follows:

<build>
  <finalName>service-application-${version}</finalName>
</build>Copy the code

Note: In the same configuration, you can use the placeholder attribute ${attribute name} to format the named content, which is just the name of the file, and the suffix.jar will be appended automatically after the packaging is complete.

Skip the test

Program will automatically run the test in the process of packaging, to check whether the project is to run the test, and test the validity of the execution of the script, generally it takes time, this process is to project’s need more time will be longer, if you want to skip this test process, only need to add a simple attribute configuration, as shown below:

<properties>
  <maven.test.skip>true</maven.test.skip>
</properties>Copy the code

This will skip the test step when we run the MVN Package again.

Run the Jar

To run the application, use the java-jar command, as shown below:

➜ germ-first-application git:(2.x) those who qualify can go onto java-jar target/ service-application-0.1-snapshot.jar. ____ ____ _ /\\ / ___ '_ __ _ _) (_ _ __ __ _ \ \ \ \ (\ ___ () |' _ | '_ | |' _ \ / _ ` | \ \ \ \ \ \ / ___) | | _) | | | | | | | (_ | |)))) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.2.4. RELEASE) the 2020-02-20 15:29:39. 3208-615 the INFO [main] O.S.B.W.E mbedded. Tomcat. TomcatWebServer: Tomcat initialized with port(s): 8080 (HTTP) 15:29:39 2020-02-20. 3208-626 the INFO [main] o.a pache, catalina. Core. StandardService: Starting the service [Tomcat] 2020-02-20 15:29:39. 626 INFO 3208 - [the main] org. Apache. Catalina. Core. StandardEngine: Starting Servlet engine: Apache Tomcat / 9.0.30 15:29:39 2020-02-20. 3208-953 the INFO [main] O.S.B.W.E mbedded. Tomcat. TomcatWebServer: Tomcat started on port(s): 8080 (HTTP) with the context path '15:29:39 2020-02-20. 3208-955 the INFO [main] O.M.C.D.F.A.D evelopingFirstApplication: Started DevelopingFirstApplication in 1.539 seconds (JVM running for 1.868)Copy the code

To exit the application, press Ctrl + C.


Author’s Personal blog

Use the open source framework ApiBoot to help you become an Api service architect