分类
Java

Spring Boot打包分离依赖JAR和配置文件

开发Spring Boot应用,用Spring Boot的Maven插件打包部署,会得到一个很大的JAR文件。该JAR文件包含了所有依赖JAR和配置文件。有时升级只需更新某个模块或修改某项配置,所以需要把它们分离出来。

在pom.xml添加如下插件,配置。

<plugins>
	<plugin>
		<groupId>org.apache.maven.plugins</groupId>
		<artifactId>maven-compiler-plugin</artifactId>
		<configuration>
			<source>1.8</source>
			<target>1.8</target>
			<encoding>UTF-8</encoding>
		</configuration>
	</plugin>
	<plugin>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-maven-plugin</artifactId>
		<configuration>
			<includes>
				<include>
					<groupId>null</groupId>
					<artifactId>null</artifactId>
				</include>
			</includes>
			<layout>JAR</layout>
			<addResources>true</addResources>
		</configuration>
		<executions>
			<execution>
				<goals>
					<goal>repackage</goal>
				</goals>
				<configuration>
					<!--配置jar包特殊标识 配置后,保留原文件,生成新文件 *-run.jar -->
					<!--配置jar包特殊标识 不配置,原文件命名为 *.jar.original,生成新文件 *.jar -->
					<!--<classifier>run</classifier>-->
				</configuration>
			</execution>
		</executions>
	</plugin>

	<!--打包jar-->
	<plugin>
		<groupId>org.apache.maven.plugins</groupId>
		<artifactId>maven-jar-plugin</artifactId>
		<configuration>
			<!--不打包资源文件-->
			<excludes>
				<exclude>*.**</exclude>
				<exclude>*/*.xml</exclude>
			</excludes>
			<archive>
				<manifest>
					<addClasspath>true</addClasspath>
					<!--MANIFEST.MF 中 Class-Path 加入前缀-->
					<classpathPrefix>lib/</classpathPrefix>
					<!--jar包不包含唯一版本标识-->
					<useUniqueVersions>false</useUniqueVersions>
					<!--指定入口类-->
					<mainClass>cn.payadd.admin.AdminApplication</mainClass>
				</manifest>
				<manifestEntries>
					<!--MANIFEST.MF 中 Class-Path 加入资源文件目录-->
					<Class-Path>./resources/</Class-Path>
				</manifestEntries>
			</archive>
			<outputDirectory>${project.build.directory}</outputDirectory>
		</configuration>
	</plugin>

	<!--拷贝依赖 copy-dependencies-->
	<plugin>
		<groupId>org.apache.maven.plugins</groupId>
		<artifactId>maven-dependency-plugin</artifactId>
		<executions>
			<execution>
				<id>copy-dependencies</id>
				<phase>package</phase>
				<goals>
					<goal>copy-dependencies</goal>
				</goals>
				<configuration>
					<outputDirectory>
						${project.build.directory}/lib/
					</outputDirectory>
				</configuration>
			</execution>
		</executions>
	</plugin>

	<!--拷贝资源文件 copy-resources-->
	<plugin>
		<artifactId>maven-resources-plugin</artifactId>
		<executions>
			<execution>
				<id>copy-resources</id>
				<phase>package</phase>
				<goals>
					<goal>copy-resources</goal>
				</goals>
				<configuration>
					<resources>
						<resource>
							<directory>src/main/resources</directory>
						</resource>
					</resources>
					<outputDirectory>${project.build.directory}/resources</outputDirectory>
				</configuration>
			</execution>
		</executions>
	</plugin>
</plugins>

执行maven,编译结果如图:

lib目录是依赖jar,resources是配置文件目录。

参考:
https://my.oschina.net/u/2329318/blog/1933393

发表评论

电子邮件地址不会被公开。