Maven的profile使用详解

一,原理:

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

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

  • 利用profile来切换环境

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

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

二,配制

在工程根目录下添加3个配制文件:

  • config-dev.properties   -- 开发时用
  • config-test.properties   -- 测试时用
  • config-product.properties  -- 生产时用

工程根目录下的pom文件中添加下面的设置:

<build>
	<sourceDirectory>src/main/java</sourceDirectory>
    <testSourceDirectory>src/test/java</testSourceDirectory>
    <!-- directory下的文件、文件夹为资源文件,对其进行过虑,即这些文件、文件夹中的${key}会被替换掉为真正的值 -->
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.xml</include>
            </includes>
            <excludes>
                <exclude>**/*.java</exclude>
            </excludes>
            <filtering>true</filtering>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
    <testResources>
        <testResource>
            <directory>src/test/resources</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <excludes>
                <exclude>**/*.java</exclude>
                <exclude>**/*.class</exclude>
            </excludes>
            <filtering>true</filtering>
        </testResource>
        <testResource>
            <directory>src/test/resources</directory>
            <filtering>true</filtering>
        </testResource>
    </testResources>
</build>
<profiles>
	<profile>
		<id>dev</id>
		<!-- 默认激活开发配制,使用config-dev.properties来替换设置过虑的资源文件中的${key} -->
		<activation>
			<activeByDefault>true</activeByDefault>
		</activation>
		<build>
			<filters>
				<filter>config-dev.properties</filter>
			</filters>
		</build>
        <properties>
            <!-- 也可以使用properties标签,会覆盖掉配置文件的值 -->
            <redis.host>192.168.14.200</redis.host>
            <redis.port>6379</redis.port>
            <redis.auth></redis.auth>
            <redis.select></redis.select>
        </properties>
	</profile>
	<profile>
		<id>test</id>
		<build>
			<filters>
				<filter>config-dev.properties</filter>
			</filters>
		</build>
	</profile>
	<profile>
		<id>product</id>
		<build>
			<filters>
				<filter>config-product.properties</filter>
			</filters>
		</build>
	</profile>
</profiles>

三,使用

filter是在maven的compile阶段执行过虑替换的,所以只要触发了编译动作即可。

  • 开发环境:

手工编译,打包:maven clean install,因为pom.xml中设置了dev为默认激活的,所以默认会把config-dev拿来进行替换${key}

  • 测试环境

手工编译,打包:maven clean install -Ptest -- 激活id="test"的profile

  • 生产环境

手工编译,打包:maven clean install -Pproduct -- 激活id="product"的profile

 

常用案例

项目开发需要有多个环境,一般为开发,测试,预发,正式4个环境,通过maven可以实现按不同环境进行打包部署,命令为: mvn package -P dev,其中“dev“为环境的变量id, 可以自己定义, 我定义的名称为:dev,qa,pre,prod , 具体在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/maven-v4_0_0.xsd">  
    ......
    <profiles>  
        <profile>  
            <id>dev</id>  
            <properties>  
                <env>dev</env>  
            </properties>  
            <activation>  
                <activeByDefault>true</activeByDefault>  
            </activation>  
        </profile>  
        <profile>  
            <id>qa</id>  
            <properties>  
                <env>qa</env>  
            </properties>  
        </profile>  
        <profile>  
            <id>pre</id>  
            <properties>  
                <env>pre</env>  
            </properties>  
        </profile>  
        <profile>  
            <id>prod</id>  
            <properties>  
                <env>prod</env>  
            </properties>  
        </profile>  
    </profiles>
    ......
    <build>  
        <filters>  
            <filter>config/${env}.properties</filter>  
        </filters>  
        <resources>  
            <resource>  
                <directory>src/main/resources</directory>  
                <filtering>true</filtering>  
            </resource>  
        </resources>
        ......
    </build>  
</project>  

1.profiles定义了各个环境的变量id

2.filters中定义了变量配置文件的地址,其中地址中的环境变量就是上面profile中定义的值

3.resources中是定义哪些目录下的文件会被配置文件中定义的变量替换,一般我们会把项目的配置文件放在src/main/resources下,像db,bean等,里面用到的变量在打包时就会根据filter中的变量配置替换成固定值

 

 

转载于:https://my.oschina.net/mywiki/blog/1494762

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值