springBoot项目中,如何根据不同的环境,打包不同的配置文件(maven)

1、创建springboot项目,然后创建不同的resource

2、application.properties加上一个标志,方便测试

3、在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>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.1.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.cn.dl</groupId>
	<artifactId>springboot-maven-package-demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>springboot-maven-package-demo</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>


	<!--定义全局的配置信息-->
	<profiles>
		<profile>
			<id>dev</id>  <!--开发环境-->
			<properties>
				<deploy.type>dev</deploy.type>
			</properties>
			<activation>
				<activeByDefault>true</activeByDefault>
			</activation>
		</profile>
		<profile>
			<id>pro</id> <!--生产环境-->
			<properties>
				<deploy.type>pro</deploy.type>
			</properties>
		</profile>
		<profile>
			<id>pre_pro</id> <!--预生产环境-->
			<properties>
				<deploy.type>pre_pro</deploy.type>
			</properties>
		</profile>
	</profiles>


	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<resources>
			<resource>
				<!--deploy.type 指定部署类型:dev、pre_pro、pro-->
				<directory>src/main/resources.${deploy.type}</directory>
			</resource>
		</resources>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

4、打包测试

1、使用命令:mvn clean package -P参数

p:就是前面定义的profiles文件,-p后面的参数就是profile中配置的id值

例如执行命令:

F:\ideaWorkSpace\practice\springboot-maven-package-demo>mvn clean package -Ppro

 

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论
使用Spring Boot Maven插件可以很方便地根据不同环境使用不同配置文件打包WAR文件。 首先,在项目的资源目录下创建多个不同环境配置文件,如application-dev.properties、application-prod.properties、application-test.properties等。 然后,在pom.xml文件,为每个环境配置一个profile,指定相应的配置文件。 ``` <profiles> <profile> <id>dev</id> <activation> <activeByDefault>true</activeByDefault> </activation> <build> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>application-dev.properties</include> </includes> </resource> </resources> </build> </profile> <profile> <id>prod</id> <build> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>application-prod.properties</include> </includes> </resource> </resources> </build> </profile> <profile> <id>test</id> <build> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>application-test.properties</include> </includes> </resource> </resources> </build> </profile> </profiles> ``` 接下来,在插件配置,使用resource元素来指定每个环境配置文件。 ``` <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <executable>true</executable> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> <configuration> <finalName>my-project-${env}</finalName> <appendAssemblyId>false</appendAssemblyId> <descriptors> <descriptor>src/main/assembly/assembly.xml</descriptor> </descriptors> </configuration> </execution> </executions> </plugin> </plugins> </build> ``` 在上面的配置,使用了一个单独的assembly.xml文件来定义如何打包WAR文件。在assembly.xml,可以根据不同环境配置不同配置文件。 ``` <assembly xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/ASSEMBLY/2.0.0" xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd"> <id>war</id> <formats> <format>war</format> </formats> <fileSets> <fileSet> <directory>${project.basedir}/src/main/resources</directory> <outputDirectory>/</outputDirectory> <includes> <include>application*.properties</include> </includes> </fileSet> </fileSets> </assembly> ``` 最后,使用命令mvn -P dev package可以选择不同的profile进行打包,生成的WAR文件会根据不同环境使用相应的配置文件。例如,使用mvn -P dev package会使用application-dev.properties配置文件进行打包,生成的WAR文件名为my-project-dev.war。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

燕少༒江湖

给我一份鼓励!谢谢!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值