Spring boot package separate jar deployment

Keywords: Maven Spring Java Apache

First of all, a question is raised. Why do we separate the jar package?

The reason is very simple. The jar files that we use the spring boot maven plugin plug-in to package are usually large, or tens of meters less. If the external network transmission of the upload cloud server is slow, why the files will be large after packaging? This is because there are many jars that our project depends on, so we can boldly think whether we can separate the jars or not, because the project is in the process of development When distributing, configuration files and java files are often modified. Few changes are made to dependent jars, so an idea emerges. If only the changed class and configuration files are released each time, can jars be separated? Happily, maven does provide this way of packaging. Use the maven jar plugin plug-in to package and then peel off the jar.

The problem has been thrown out, so how to solve it? Modify the pom.xml file to change the original spring boot Maven plugin package plug-in to Maven jar plugin

<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-jar-plugin</artifactId>
				<configuration>
					<archive>
						<manifest>
							<addClasspath>true</addClasspath>
							<classpathPrefix>lib/</classpathPrefix>
                            <!--Change here to the full path of your application entry class-->
							<mainClass>com.jetsen.edu.StatisticsApplication</mainClass>
						</manifest>
					</archive>
				</configuration>
			</plugin>
			<plugin>
                <!--This plug-in is based on jar The package is copied and put into the compiled target/lib Directory and exclude internal dependencies during packaging-->
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-dependency-plugin</artifactId>
				<executions>
					<execution>
						<id>copy-dependencies</id>
						<phase>prepare-package</phase>
						<goals>
							<goal>copy-dependencies</goal>
						</goals>
						<configuration>
							<outputDirectory>${project.build.directory}/lib</outputDirectory>
							<overWriteReleases>false</overWriteReleases>
							<overWriteSnapshots>false</overWriteSnapshots>
							<overWriteIfNewer>true</overWriteIfNewer>
						</configuration>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>

Next, execute the packing command in the project root directory: mvn clean package -Dmaven.test.skip=true

After packaging, I will find that there is a lib directory full of jar files that our project depends on in the target directory. However, we can upload the Lib directory and the packed jar files to the ECS at the time of publishing. In the future, if the project dependency does not change, just upload the xxxx.jar file to the ECS, execute the Java - jar xxxx.jar & command, if the dependency occurs The change requires replacing the Lib directory. The jar file packed in this way is usually only a few hundred kb in size, and the upload is also very fast.

Posted by rocram16 on Mon, 06 Jan 2020 04:05:12 -0800