spring boot uses the assembly plug-in to package dependent jar s into external standalone folders

Posted by westmich on Wed, 25 Dec 2019 22:29:45 +0100

spring boot packs all dependent jars into one jar by default when it packs and runs jars. This is inconvenient because the packaged jar packages are relatively large and need to be uploaded to the server for a long time. Here, we can use the assembly plug-in to package dependent jars into external independent folders. In the future, we only need to upload the packages that do not contain dependent jars OK, so the typed jar can also run

Add the following plug-ins to pom:

<build>
		<!-- <finalName>fileName</finalName>-->
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-jar-plugin</artifactId>
				<configuration>
					<archive>
						<manifest>
							<!-- Project startup class, which needs to be replaced by startup class -->
							<mainClass>com.bc.Application</mainClass>
							<!-- Dependent jar Directory prefix for -->
							<classpathPrefix>../lib</classpathPrefix>
							<addClasspath>true</addClasspath>
						</manifest>
					</archive>
					<excludes>
						<exclude>assembly.xml</exclude>
						<exclude>application.properties</exclude>
						<exclude>application.yml</exclude>
					</excludes>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-assembly-plugin</artifactId>
				<configuration>
					<appendAssemblyId>true</appendAssemblyId>
					<descriptors>
						<descriptor>src/main/resources/assembly.xml</descriptor>
					</descriptors>
				</configuration>
				<executions>
					<execution>
						<id>make-assembly</id>
						<phase>package</phase>
						<goals>
							<goal>single</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>

Corresponding assembly.xml configuration file:

<?xml version="1.0" encoding="UTF-8"?>
<assembly>
	<!-- Customizable-->
	<id>assembly-${project.version}</id>

	<!-- Type of packaging, if any N One, will fight N Packages of types -->
	<formats>
		<format>zip</format>
		<!-- <format>tar.gz</format> -->
	</formats>

	<includeBaseDirectory>true</includeBaseDirectory>
	<!-- Will rely on jar Pack to lib Folder -->
	<dependencySets>
		<dependencySet>
			<outputDirectory>lib</outputDirectory>
			<scope>runtime</scope>
			<excludes>
				<exclude>${groupId}:${artifactId}</exclude>
			</excludes>
		</dependencySet>
	</dependencySets>
	
	<fileSets>
		<!-- 0755->That is, the user has read/write/Execute permission, group user and other users have read and write permission; 0644->That is, users have read-write permission, group users and other users have read-only permission; -->
		<!-- take src/bin All files in the directory are exported to the packaged bin Directory -->
		<fileSet>
			<directory>${basedir}/bin</directory>
			<outputDirectory>bin</outputDirectory>
			<fileMode>0755</fileMode>
			<includes>
				<include>**.sh</include>
				<include>**.bat</include>
			</includes>
		</fileSet>

		<!-- Specified output target/classes Profiles in to config Directory -->
		<fileSet>
			<directory>${basedir}/target/classes</directory>
			<outputDirectory>bin</outputDirectory>
			<fileMode>0644</fileMode>
			<includes>
				<include>application.properties</include>
				<include>application.yml</include>
			</includes>
		</fileSet>

		<!-- Package third-party dependencies into lib Directory -->
		<fileSet>
			<directory>${basedir}/target/lib</directory>
			<outputDirectory>lib</outputDirectory>
			<fileMode>0755</fileMode>
		</fileSet>

		<!-- Start the project jar Pack to bin Directory -->
		<fileSet>
			<directory>${basedir}/target</directory>
			<outputDirectory>bin</outputDirectory>
			<fileMode>0755</fileMode>
			<includes>
				<include>${project.build.finalName}.jar</include>
			</includes>
		</fileSet>

		<!-- Include files in the root directory -->
		<fileSet>
			<directory>${basedir}</directory>
			<includes>
				<include>NOTICE</include>
				<include>LICENSE</include>
				<include>*.md</include>
			</includes>
		</fileSet>
	</fileSets>

</assembly>

The compressed file can be generated by executing mvn package. The running jar is in bin directory and can be directly run after being extracted

 

Another way to copy dependent jar s to an external directory is to use the Maven dependency plugin plug-in

<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-dependency-plugin</artifactId>
				<configuration>
					<outputDirectory>${project.build.directory}/lib</outputDirectory>
					<excludeTransitive>false</excludeTransitive>
					<stripVersion>false</stripVersion>
				</configuration>
				<executions>
					<execution>
						<phase>package</phase>
						<goals>
							<goal>copy-dependencies</goal>
						</goals>
					</execution>
				</executions>
			</plugin>

Comment out the dependencySets node in the assembly.xml file, so that you can copy the dependent jar s into the lib folder

Topics: Programming Maven xml Apache Spring