Article Directory
After the SpringBoot project is developed, it needs to be packaged into a jar package/war package, and deployed and run to complete the process of the entire project.
1. In the pom file
<build>
<finalName>DAG-1.0</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.yml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.yml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
1.1 maven-compiler-plugin
maven-compiler-plugin
You can specify the version of the JDK in the project and the version of the class file generated by the specified compilation. In my example, I specified that JDK11 was used in the source code during development, and the generated class file also corresponds to JDK11.
1.2 spring-boot-maven-plugin
spring-boot-maven-plugin
Plug-ins packaged using Maven. repackage
Repackage the application. By default, the two maven goals of repackage and run will include any dependencies defined in the project.
Some dependencies are required to be excluded from the executable jar package. There are three ways to exclude dependent modules while the package is running:
1.2.1 Exclude a specific maven module
Excluding a specific maven module is achieved through a unique combination of groupId and artifactId. (If necessary, you can add a classifier to uniquely confirm.)
<build>
<finalName>DAG-1.0</finalName>
<plugins>
......
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<excludes>
<excludes>
<groupId>com.foo</groupId>
<artifactId>bar</artifactId>
</excludes>
</excludes>
</configuration>
</plugin>
</plugins>
......
</build>
Exclude the bar module under com.foo.
1.2.2 Exclude all maven modules that match the "specified artifactId"
Exclude all maven modules that match the "specified artifactId".
<build>
<finalName>DAG-1.0</finalName>
<plugins>
......
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<excludes>
<excludeArtifactIds>my-lib,another-lib</excludeArtifactIds>
</excludes>
</configuration>
</plugin>
</plugins>
......
</build>
Excluded artifactId
is my-lib
, another-lib
all modules, i.e. all groupId
lower artifactId
for the my-lib
, another-lib
module.
1.2.3 Exclude all maven modules belonging to "specified groupId"
<build>
<finalName>DAG-1.0</finalName>
<plugins>
......
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<excludes>
<excludeGroupIds>com.foo</excludeGroupIds>
</excludes>
</configuration>
</plugin>
</plugins>
......
</build>
Excluded groupId
as com.foo
all the modules.
2. Jar package name
<finalName>Your jar Package Name</finalName>
The build fileName
specifies the name of the jar package generated by packaging.
3. Maven packaging
Execute the following commands in the root directory of the SpringBoot project:
mvn package spring-boot:repackage
You can generate the target folder in the root directory, which contains xxx.jar.
4. Run the jar package
4.1 Console running jar package
java -jar xxx.jar
4.2 Run the jar package in the background, closing the terminal does not affect the running of the jar package
nohup java -jar DAG-1.0.jar > msg.log 2>&1 &
The jar package runs in the background, the log information is entered into the msg.log file, and the error log and correct log are entered into the msg.log.
Adding an ampersand at the end of the command means that the process will be executed in the background, so that it will immediately return to the prompt state, and we can continue to do the following.
4.3 Run the jar package in the background, and input the correct log and the wrong log into different files
nohup java -jar DAG-1.0.jar >> msg1.log 2>>msg2.log &
The correct log is entered into msg1.log, and the wrong log is entered into msg2.log.
5. Pack and generate war package
The jar package is generated by default. Specify what form of package to generate in the pom file
<packaging>war</packaging>
<name>DAG-1.0</name>
<description>DAG project for Spring Boot</description>
Execute packaging commands
mvn package spring-boot:repackage
You can see that the war package is generated, and the war package is deployed to run the program.
6. Errors encountered and precautions
6.1 The corresponding page cannot be found when visiting the HTML page
In this case, we must first look at whether there is a corresponding templates folder under the generated target folder and the xxx.html file that should be contained in it. When the program is running, it only recognizes the things in the target folder. If there is no corresponding templates folder and xxx.html file under the target folder, it is definitely impossible to open the HTML page.
The common cause of this error is that xxx.html is not specified as the resource of the project to be packaged. This needs to be specified in the pom file.
<build>
......
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.yml</include>
<include>**/*.html</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.yml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
<build>
As above, specify the .properties file, .xml file, .yml file, .html file under the src/main/resources folder and the .properties file, .xml file, .yml under the src/main/java folder The file is used as the resource file of the project, so the html file can be found in the generated target file.
If you remove the indication of the .html file, the corresponding HTML file cannot be found in the generated target folder, and the HTML page cannot be opened.
<build>
......
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.yml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.yml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
<build>
After removing, there is no HTML file in the generated target folder.
Without the templates folder and the HTML files in it, the running program will not be able to access the HTML page.
6.2 The HTML file is generated in the target, but the HTML page is still not found
It is possible that thymeleaf
the configuration is wrong.
thymeleaf.prefix
You must write it correctly, and you cannot /
write one more or one less /
, because in this way, when you write the access page, there will be more or less one in the path, and you /
will not be able to find the HTML page.
6.3 The pom file maven-compiler-plugin
or spring-boot-maven-plugin
other plug-ins cannot be imported correctly (turns red)
You need to introduce these plug-ins in dependencies, and then introduce these plug-ins in plugins in the build.