maven(2):resource,filter,profile

利用resouce,filter,profile达到不同环境不同配置文件

一、原理

利用filter实现对资源文件(resouces)过滤

maven filter可利用指定的xxx.properties中对应的key=value对资源文件中的${key}进行替换,最终把你的资源文件中的username=${key}替换成username=value

利用profile来切换环境

maven profile可使用操作系统信息,jdk信息,文件是否存在,属性值等作为依据,来激活相应的profile,也可在编译阶段,通过mvn命令加参数 -PprofileId 来手工激活使用对应的profile

结合filter和profile,我们就可以方便的在不同环境下使用不同的配制

二、实例

profile切换环境>filter获取此环境的配置文件>resource标签filter为true的,执行过滤替换

<build>
		<finalName>demo</finalName>
		<filters><!--配置文件,指向profile中的props标签内文件-->
			<filter>${props}</filter>
		</filters>

		<resources>
			<resource><!--配置文件application.properties-->
				<directory>src/main/resources</directory>
		        <!--执行过滤:属性文件内的${key}被锅炉文件内key=value替换-->		
                <filtering>true</filtering>
				<includes>
					<include>application.properties</include>
				</includes>
			</resource>
			<resource><!--非application.properties的配置文件文件-->
				<directory>src/main/resources</directory>
                <!--不过滤-->	
				<filtering>false</filtering>
				<excludes>
					<exclude>application.properties</exclude>
				</excludes>
			</resource>
		</resources>

		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.6.1</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>
			
			<plugin>
				<artifactId>maven-assembly-plugin</artifactId>
				<configuration>
					<appendAssemblyId>false</appendAssemblyId>
					<outputDirectory>./target</outputDirectory>
					<descriptors>
						<descriptor>src/main/resources/assembly.xml</descriptor>
					</descriptors>
				</configuration>
				<executions>
					<execution>
						<id>make-assembly</id>
						<phase>package</phase>
						<goals>
							<goal>single</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>

	<profiles>
		<profile><!--定义dev环境-->
			<id>dev</id>
			<properties><!--引用的属性文件-->
				<props>src/test/resources/application-test.properties</props>
			</properties>
		</profile>
		<profile><!--定义prod环境-->
			<id>prod</id>
			<properties><!--引用的属性文件-->
				<props>xxx.properties</props>
			</properties>
			<activation><!--设定默认激活状态-->
				<activeByDefault>true</activeByDefault>
			</activation>
		</profile>
	</profiles>

详情引用:https://segmentfault.com/a/1190000012193745

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值