SpringBoot打包分离依赖jar和资源文件

本文详细介绍了如何使用Maven进行有效的项目打包配置,包括解决依赖jar拷贝问题、避免Eclipse警告、设置Java版本和编码、排除资源文件、定义入口类、复制资源文件和依赖包等关键步骤。

参考资料 maven-dependency-plugin 尝试了网上的很多配置,在我本机试了都有很多问题,要么依赖jar没有拷贝出来 要么就是eclipse报警告,尝试很多次最终解决了

mvn -package 打包后拷贝target下的 jar文件和 resources 及 lib

执行方式:java -jar a.jar

配置如下:


	<build>
		<finalName>sample</finalName>
		<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>
					<compilerArguments>
						<verbose />
						<bootclasspath>${java.home}\lib\rt.jar;${java.home}\lib\jce.jar</bootclasspath>
					</compilerArguments>
				</configuration>
			</plugin>
			<!--**********分离资源文件及依赖包打包配置************* -->
			<!--打包jar -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-jar-plugin</artifactId>
				<configuration>
					<!--不打包资源文件 -->
					<excludes>
						<exclude>*.**</exclude>
						<exclude>static/**</exclude>
						<exclude>templates/**</exclude>
						<exclude>config/**</exclude>
					</excludes>
					<archive>
						<manifest>
							<addClasspath>true</addClasspath>
							<!--MANIFEST.MF 中 Class-Path 加入前缀 -->
							<classpathPrefix>lib/</classpathPrefix>
							<!--jar包不包含唯一版本标识 -->
							<useUniqueVersions>false</useUniqueVersions>
							<!--指定入口类 -->
							<mainClass>com.beone.AppAdminApplication</mainClass>
						</manifest>
						<manifestEntries>
							<!--MANIFEST.MF 中 Class-Path 加入资源文件目录 -->
							<Class-Path>./resources/</Class-Path>
						</manifestEntries>
					</archive>
					<outputDirectory>${project.build.directory}</outputDirectory>
				</configuration>
			</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>

			<!-- 先在pluginManagement中声明再使用 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-dependency-plugin</artifactId>
			</plugin>
		</plugins>
		<pluginManagement>
			<plugins>
				<!--拷贝依赖 copy-dependencies -->
				<plugin>
					<groupId>org.apache.maven.plugins</groupId>
					<artifactId>maven-dependency-plugin</artifactId>
					<executions>
						<execution>
							<phase>package</phase>
							<goals>
								<goal>copy-dependencies</goal>
							</goals>
						</execution>
					</executions>
					<configuration>
						<includeScope>compile</includeScope>
						<outputDirectory>${project.build.directory}/lib</outputDirectory>
					</configuration>
				</plugin>
			</plugins>
		</pluginManagement>
	</build>

可做参考的其他资料1

转载于:https://my.oschina.net/isharecn/blog/2994892

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值