Proficient in Spring Boot -- Chapter 22: selection of packaging methods

Keywords: Programming Maven Spring xml Apache

1. Background

After the new Spring Boot is built, it will bring its own packaging method. Now it is usually packaged into jar package. Of course, you can also pack it into war package, I won't introduce it! This article mainly wants to talk about the self-contained packaging method and assembly packaging method. What are the differences, advantages and disadvantages between them?

2. Self packing method

After you create a Spring Boot project using IDEA's spring initializer or start.spring.io, you can see the maven packaging method in pom.xml file

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

You can use the command mvn clean package to package the project into a jar package, but in this way, you can package all configuration files and template files (if template exists) in the jar. If you change them, you must repack them.

Think about it.

This way of packing is really very simple and convenient, but when we modify the configuration, we need to repackage and release it. Another problem is that the online database address is in the configuration file, and the developers generally don't know it (of course, the operation and maintenance won't tell you, so as to avoid misoperation). So let the operation and maintenance pack it?? Obviously not! So we can take the following assembly packaging method!

3.assembly packaging method

Step one: Exclude packaged plug-ins that come with Spring Boot: comment or remove code from pom.xml

	<plugin>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-maven-plugin</artifactId>
		<configuration>
			<fork>true</fork>
			<mainClass>com.joyreach.base.JoyBaseServerApplication</mainClass>
		</configuration>
		<executions>
			<execution>
				<goals>
					<goal>repackage</goal>
				</goals>
			</execution>
		</executions>
	</plugin>

The second step: Add assembly packaging plug-in, and add it in pom.xml

<plugin>
	<artifactId>maven-assembly-plugin</artifactId>
	<configuration>
		<finalName>${project.artifactId}</finalName>
		<descriptors>
			<descriptor>src/main/assembly/assembly.xml</descriptor>
		</descriptors>
	</configuration>
	<executions>
		<execution>
			<id>make-assembly</id>
			<phase>package</phase>
			<goals>
				<goal>single</goal>
			</goals>
		</execution>
	</executions>
</plugin>

The third step To configure assembly: First, add the following code in pom.xml to separate the configuration file:

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-jar-plugin</artifactId>
	<version>2.3.2</version>
	<configuration>
		<excludes>
			<exclude>*.*</exclude>
		</excludes>
		<archive>
			<manifest>
				<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
				<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
				<addClasspath>true</addClasspath>
				<classpathPrefix>../lib/</classpathPrefix>
				<mainClass>com.guuidea.basesave.BaseSaveApplication</mainClass>
			</manifest>
			<manifestEntries>
				<Class-Path>../conf/</Class-Path>
			</manifestEntries>
		</archive>
	</configuration>
</plugin>

Second, configure in assembly.xml

<?xml version="1.0" encoding="UTF-8" ?>
<assembly
		xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
	<id>${project.version}</id>
	<formats>
		<format>zip</format>
	</formats>
	<dependencySets>
		<!-- Project dependencies -->
		<dependencySet>
			<!-- Exclude components from the current project -->
			<useProjectArtifact>true</useProjectArtifact>
			<outputDirectory>lib</outputDirectory>
		</dependencySet>
	</dependencySets>
		<fileSet>
			<directory>lib/</directory>
			<outputDirectory>${file.separator}lib</outputDirectory>
			<includes>
				<include>*.jar</include>
			</includes>
		</fileSet>

		<fileSet>
			<directory>${project.build.directory}</directory>
			<outputDirectory>${file.separator}bin</outputDirectory>
			<includes>
				<include>${project.artifactId}-${project.version}.jar</include>
			</includes>
		</fileSet>
		<fileSet>
			<directory>src/main/resources</directory>
			<outputDirectory>${file.separator}conf</outputDirectory>
		</fileSet>
	</fileSets>
</assembly>

Talk about the pit I met:

  • 1.include all configuration files.
    1. Exclude must exclude all configuration files under the jar package, otherwise, the configuration in the jar package will be used first by default, which is why the configuration files under the conf directory are not effective after modification!

4. Summary

The two methods have advantages and disadvantages. The default method is convenient and more suitable for development and testing. assembly packaging is de service and engineering, more suitable for the company's process and production. If most of the company's project deployment is completed by development, it is recommended to use the self-contained method. If there is an operation and maintenance dedicated to online maintenance, it is more standardized to use assembly.

Posted by bluestar on Mon, 11 Nov 2019 22:52:41 -0800