Knowledge changes fate, rolling code makes me happy, and I will continue to swim in the open source industry in 2020 < br / > Take a look at what you like and get into the habit < br / > Give me a Star, Click to learn about the implementation solution of component-based interface service based on spring boot
After the application is written, an important stage is publishing. When we publish, we need to package the application. How to package the application written by spring boot?
Recommended reading
Packaging method
There are generally two forms of application publishing.
The traditional way is to package the application program into a xx.war file, which contains only the. class and configuration file compiled from the source code of the application program.
Spring boot also provides another efficient way of packaging. In pom.xml, MVN is executed by configuring maven plugin When the package command is used, all the files in the directories of src/main/java and src/main/resources will be packed, and finally an xx.jar file will be generated. Because the relevant dependencies of Tomcat will be put into xx.jar by default during the SpringBoot packing, it can be run directly through the java -jar xx.jar command line.
Packaging plug-ins
When we create a SpringBoot project through IDEA, we usually add packed maven plugin in 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>
**Note: * * if you are not building a spring boot application through the spring boot starter parent method, you need to manually configure < executions >. For plug-in usage documents, see Spring Boot Maven Plugin
Execute packaging
Using Maven to build the SpringBoot application package is very simple. We just need to execute mvn package in the root directory of the application through the command, as shown below:
➜ developing-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-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- //Omit some compilation logs [INFO] --- maven-jar-plugin:3.1.2:jar (default-jar) @ developing-first-application --- [INFO] Building jar: /Users/yuqiyu/study/article-source-code/spring-boot-chapter/developing-first-application/target/developing-first-application-0.0.1-SNAPSHOT.jar [INFO] [INFO] --- spring-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] ------------------------------------------------------------------------
When BUILD SUCCESS appears in the console, it proves that our package has been successful, and the executable Jar of the current application has been generated, which is located in the target directory.
Package file naming
The default format of the file name generated after the spring boot Maven plugin plug-in is: < artifactid > + < version >. Jar, such as: developing first application-0.0.1-snapshot.jar. If this is not the format you want, you can customize it as follows:
<build> <finalName>service-application-${version}</finalName> </build>
Note: < finalname > and < plugins > are peer-to-peer configurations. You can use the placeholder property ${property name} to format the named content. This is just the name of the file. The suffix. jar will be appended automatically after packaging.
Skip test
During the packaging process, the project will automatically run tests to check whether the project can pass the running tests and whether the execution of test scripts is effective. Generally, this process takes a certain time, and the more project content, the longer it will take. If you want to skip this testing process, you only need to add a very simple < Properties > property configuration, as shown below:
<properties> <maven.test.skip>true</maven.test.skip> </properties>
This way, when we run mvn package again, we will skip the test step.
Run Jar
To run the application, use the Java jar command as follows:
➜ developing-first-application git:(2.x) ✗ java -jar target/service-application-0.0.1-SNAPSHOT.jar . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.2.4.RELEASE) 2020-02-20 15:29:39.615 INFO 3208 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2020-02-20 15:29:39.626 INFO 3208 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2020-02-20 15:29:39.626 INFO 3208 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.30] 2020-02-20 15:29:39.953 INFO 3208 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2020-02-20 15:29:39.955 INFO 3208 --- [ main] o.m.c.d.f.a.DevelopingFirstApplication : Started DevelopingFirstApplication in 1.539 seconds (JVM running for 1.868)