Package Spring boot via Maven build and extract the config configuration file outside the jar file

If it is packaged through different IDEs, it will really feel that the dependencies are too large, and it is prone to errors and complicated operations.

At the same time, the use of spring-boot-maven-plugin has too few related configurations, and it cannot meet the needs of convenient deployment and operation. need.


Here we use, Maven's following plug-in

maven-jar-plugin, responsible for packaging the application into an executable jar file
maven-assembly-plugin, responsible for packaging the entire project into a final compressed package according to a custom directory structure, which is convenient Actual deployment



Requirement 1, extract the dependent jars outside the runnable jar file, we use maven-jar-plugin to achieve

For example, the final packaging directory of my project is as follows The

code directory structure is as follows


The final runnable file jar file does not Contains dependent jar packages, all dependent jar packages are placed in the lib folder parallel to ps.jar, so that if you want to deploy quickly in the future, you don't need to upload the large lib package every time, unless Dependency packages have changed. Of course, these are the prerequisites if you want to do this in the future. I am here to make the deployed files more regular. The configuration files of maven-jar-plugin
here are as follows

<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-jar-plugin</artifactId>
				<version>2.6</version>
				<configuration>
					<archive>
					<!-- Adding index does not read classpath from mainfest, but from Index.list -->
<!-- 						<index>true</index> -->
						<manifest>
							<mainClass>com.vmpay.pay.App</mainClass>
							<!-- to create a class path to your dependecies you have to fill true
								in this field -->
							<addClasspath>true</addClasspath>
							<classpathPrefix>lib/</classpathPrefix>
							<!--<classpathLayoutType>custom</classpathLayoutType> <customClasspathLayout>
								lib/$${artifact.groupId}.$${artifact.artifactId}.$${artifact.extension} </customClasspathLayout> -->
						</manifest>
						<manifestEntries>
							<Class-Path>./</Class-Path>
						</manifestEntries>
					</archive>
					<excludes>
						<exclude>config/**</exclude>
					</excludes>
				</configuration>
			</plugin>

The manifest part is the core. In the executable jar file, after packaging, a MANIFEST.MF file will be generated under the META-INF folder in the jar file, which records some related configurations of the executable file, such as The content configured in the above code, which configures the relative directory location of the executable jar file to read the classpath in the future, and the imported jar files. The above configuration is that the classpath directory is ./ (slightly I will explain why later.)
The mainClass configuration indicates which class is used as the entry of the program to execute the
addClasspath configuration, whether to package the dependent classpath together. The
classpathPrefix configuration indicates that the prefix of the dependent classpath, that is, in the MANIFEST.MF file generated after packaging, The imported jar files will be prefixed with lib/, such as fastjson-1.2.7.jar, which will be lib/fastjson-1.2.7.jar in the mainfest file. The excludes
configuration indicates which folders are excluded from being packaged.

maven-jar-plugin mainly configures the MANIFEST.MF file, which is to let the executable file know how to execute it, load the description of which files to execute, and hand over the rest of the work to maven-assembly-plugin

in the pom file The configuration is similar to the following
<plugin>
				<artifactId>maven-assembly-plugin</artifactId>
				<configuration>
					<!-- not append assembly id in release file name -->
					<appendAssemblyId>false</appendAssemblyId>
					<descriptors>
						<descriptor>src/main/build/package.xml</descriptor>
					</descriptors>
				</configuration>
				<executions>
					<execution>
						<id>make-assembly</id>
						<phase>package</phase>
						<goals>
							<goal>single</goal>
						</goals>
					</execution>
				</executions>
			</plugin>


The key point is the path of package.xml. The relevant configuration using maven-assembly-plugin is actually in the
file content of package.xml in this file.
<?xml version="1.0" encoding="UTF-8"?>
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
	<id>package</id>
	<formats>
		<format>zip</format>
	</formats>
	<includeBaseDirectory>true</includeBaseDirectory>
	<fileSets>
		<fileSet>
			<directory>bin</directory>
			<outputDirectory>/</outputDirectory>
		</fileSet>
		<fileSet>
			<directory>src/main/resources</directory>
			<outputDirectory>/</outputDirectory>
		</fileSet>
		<fileSet>
			<directory>${project.build.directory}</directory>
			<outputDirectory>/</outputDirectory>
			<includes>
				<include>*.jar</include>
			</includes>
		</fileSet>
	</fileSets>
	<dependencySets>
		<dependencySet>
			<outputDirectory>lib</outputDirectory>
			<scope>runtime</scope>
<!-- 			<unpack>false</unpack> -->
			<excludes>
<!-- 				<exclude>${project.name}-${project.version}</exclude> -->
				<exclude>${groupId}:${artifactId}</exclude>
			</excludes>
		</dependencySet>
	</dependencySets>
</assembly>

For other related configurations, please refer to the official document
[url]
http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html#class_unpackOptions
[/url]

I have configured here, and the final compressed file format is zip, that is, the final package is a zip file, which is then published on the server for decompression and deployment. The relevant configurations I want are all in this compressed package, and the decompression can be used directly.

The following fileSets are configured. I need to configure those The files are packaged into my final compressed package.
My configuration file includes the startup script bin folder, which contains the shell startup script, and
the related configuration file src/main/resources, which contains the properties extracted by the entire program. The
final runnable jar file uses the ${project.build.directory} variable, that is, the jar file generated by maven-jar-plugin. In
dependencySets, the dependency library is configured and finally output to the lib folder, with The path of the manifest file generated by the maven-jar-plugin configuration above corresponds to the path of the manifest file, so that the executable jar will find the corresponding file to load


start.sh according to the path of the manifest
###start up

#!/bin/sh

moduleName="ps"
pidPath="/var/run/$moduleName-tpid"

rm -f $pidPath

nohup java -jar ./$moduleName.jar -server -Xms1024m -Xmx2048m -Xss256k > ./run.log 2>&1 &

echo $! > $pidPath


stop.sh
###stop

moduleName="ps"

tpid=`cat /var/run/$moduleName-tpid | awk '{print $1}'`
tpid = `ps -aef | grep $ tpid | awk '{print $ 2}' | grep $ tpid`
if [ ${tpid} ]; then
kill -9 $tpid
be



The above content is for reference only


After the above configuration, upload the zip archive to the online for decompression, and run start.sh directly.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326763357&siteId=291194637