【sprinb-boot】配置文件分离打包

前言

  • springboot 2.0.0.RELEASE
  • maven 3.5.0

使用maven命令mvn package打包spring boot项目时,将配置文件分离出来。

pom.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<build>
		...
		<plugins>
			<!-- 配置文件分离打包步骤 : 1,jar -> 2,copy-resources -> 3,repackage -->
			<!-- 配置文件分离打包1/3  jar : 配置文件文件不打包到jar中 -->
			<plugin>
				<artifactId>maven-jar-plugin</artifactId>
				<configuration>
					<excludes>
						<exclude>application*.properties</exclude>
						<exclude>messages*.properties</exclude>
						<exclude>*.xml</exclude>
					</excludes>
				</configuration>
			</plugin>
			<!-- 配置文件分离打包2/3 copy-resources : 拷贝配置文件 -->
			<plugin>
				<artifactId>maven-resources-plugin</artifactId>
				<configuration>
				</configuration>
				<executions>
					<execution>
						<id>copy-resources</id>
						<phase>package</phase>
						<goals>
							<goal>copy-resources</goal>
						</goals>
						<configuration>
							<resources>
								<resource>
									<directory>src/main/resources</directory>
									<includes>
										<include>application.properties</include>
										<include>logback-spring.xml</include>
										<include>messages*.properties</include>
									</includes>
								</resource>
							</resources>
							<outputDirectory>${project.build.directory}/final-package</outputDirectory>
						</configuration>
					</execution>
				</executions>
			</plugin>
			<!-- 配置文件分离打包3/3 repackage : spring boot 打包设置 -->
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<executions>
					<execution>
						<goals>
							<goal>repackage</goal>
						</goals>
						<configuration>
							<layout>ZIP</layout>
							<!--设置spring boot的jar/war的存放路径 -->
							<outputDirectory>${project.build.directory}/final-package</outputDirectory>
						</configuration>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
	...
</project>

打包命令

mvn clean package

启动命令

java -jar xxx-springboot-app.jar 
发布了284 篇原创文章 · 获赞 54 · 访问量 42万+

猜你喜欢

转载自blog.csdn.net/sayyy/article/details/103599127
今日推荐