Spring Boot executable jar package production system service
In the Maven plugin of Spring Boot, the function of building a complete executable program is also provided. What does it mean?
In other words, we can java -jar
run the jar directly to execute the program without using it. So that we can easily create it as a system service running in the background.
The main steps are as follows:
1. Configure the pom file
Add Spring Boot plugin in pom.xml, and pay attention to set executable configuration
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
</plugins>
</build>
2. Generate executable jar
After completing the above configuration, use mvn install
to package and build an executable jar package
3. Create a soft connection
Create a soft link to the /etc/init.d/
directory
sudo ln -s /var/yourapp/yourapp.jar /etc/init.d/yourapp
4. Service-based startup project
After the soft connection is created, we can control the start, stop, and restart operations on yourapp.jar application with the following command
service yourapp start
service yourapp stop
service yourapp restart
service yourapp start
Explanation
Magic number in zip format
The generated jar / war is actually a zip file. Why can this zip file be executed directly under the shell?
Study the format of the next zip file.
The zip file is composed of entries, and each entry has a 4 byte at the beginning magic number
:
Local file header signature = 0x04034b50 (read as a little-endian number)
即 PK\003\004
Reference: https://en.wikipedia.org/wiki/Zip_(file_format)
The zip processing software reads the magic number before it starts processing. So under linux / unix, you can write a bash file directly at the beginning of a zip file, so that it will be considered a bash script. The zip processing software can still process it correctly when reading this file.
For example, generated by spring boot executable jar/war
, the beginning is:
#!/bin/bash
#
# . ____ _ __ _ _
# /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
# ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
# \\/ ___)| |_)| | | | | || (_| | ) ) ) )
# ' |____| .__|_| |_|_| |_\__, | / / / /
# =========|_|==============|___/=/_/_/_/
# :: Spring Boot Startup Script ::
#
At the end of the script content, you can see the magic number of the zip entry:
exit 0
PK^C^D
spring boot的launch.script
In fact spring boot maven plugin
, the following script is packaged into the front part of the fat jar.
This launch.script
supports many variable settings.
It can also automatically identify whether it is in different modes of auto or service.
The so-called auto mode
refers to direct operation jar/war
:
./demo.jar
And service mode
it is the case that the operating system starts the service:
service demo start/stop/restart/status
So fat jar can be executed directly on the ordinary command line, ./xxx.jar
or link to the /etc/init.d/
next, and become a service.
to sum up
jar/war
It is actually in zip format.spring-boot-maven-plugin
Bring the startup script toexecutable jar/war
the front.- The last line of the
exit 0
script is that the script only executes its own content, not the content in the jar / war. - The zip file is composed of multiple entries, and there is an entry at the beginning
magic number
, so the zip processing software can skip the previous script and find it accuratelyzip entry