springboot jar、资源文件分离打包配置

原文地址:https://www.jianshu.com/p/a97501bb5460

pom.xml

	<build>
		<plugins>
			<!--打包jar -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-jar-plugin</artifactId>
				<configuration>
					<archive>
						<manifest>
							<addClasspath>true</addClasspath>
							<!--MANIFEST.MF 中 Class-Path 加入前缀 -->
							<classpathPrefix>lib/</classpathPrefix>
							<!--jar包不包含唯一版本标识 -->
							<useUniqueVersions>false</useUniqueVersions>
							<!--指定入口类 -->
							<mainClass>xxx.HgZhwgApplication</mainClass>
						</manifest>
						<manifestEntries>
							<!--MANIFEST.MF 中 Class-Path 加入资源文件目录 -->
							<Class-Path>/resources</Class-Path>
						</manifestEntries>
					</archive> 
					<outputDirectory>${project.build.directory}/dis</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}/dis/lib/
							</outputDirectory>
						</configuration>
					</execution>
				</executions>
			</plugin>

			<!--拷贝资源文件 copy-resources, config固定 -->
			<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}/dis/config</outputDirectory>
						</configuration>
					</execution>
				</executions>
			</plugin>

			<!--spring boot repackage,依赖 maven-jar-plugin 打包的jar包 重新打包成 spring boot 
				的jar包 -->
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<layout>ZIP</layout>
					<!--使用外部配置文件,jar包里没有资源文件 -->
					<addResources>true</addResources>

				</configuration>
				<executions>
					<execution>
						<goals>
							<goal>repackage</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
		<!-- springboot jar中没有资源文件 -->
		<resources>
			<resource>
				<filtering>true</filtering>
				<directory>src/main/resources</directory>
				<excludes>
					<exclude>**/**</exclude>
				</excludes>
			</resource>
		</resources>
	</build>

启动:

进入target/dis

java -Dloader.path=.,lib -jar xxxx-1.0.0.jar

猜你喜欢

转载自blog.csdn.net/m0_37598340/article/details/86575098