继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

maven profile filter 线上线下分开打包配置

喵喵一只汪
关注TA
已关注
手记 315
粉丝 86
获赞 467

maven自动选择不同的配置文件打包profile+filter

1. profile:
[要点:] activeByDefault默认激活,不用再mvn命令时指定额外参数;[注意:] 使用非默认的配置,要在编译时指定:如,要在生产环境打包[并跳过单元测试]:mvn clean package -Pproduct [-Dmaven.test.skip=ture]

如果mvn clean package:

a. 默认加载profile-dev.properties来打包
b. 可以替换的内容范围是:
src/main/resources/*.xml|*.properties...src/main/java/*.xml
<project>    <build>        <finalName>project-name</finalName>        <!-- 启用filter功能为resources中的变量赋值 -->          <resources>              <resource>                  <directory>src/main/resources</directory>                    <includes>                        <include>**/*</include>                    </includes>                    <filtering>true</filtering>              </resource>            <resource>                <directory>src/main/java</directory>                <includes>                    <include>**/*.xml</include>                </includes>            </resource>        </resources>      </build>    <profiles>        <!-- 1.开发环境 -->        <profile>            <id>dev</id>            <activation>                <activeByDefault>true</activeByDefault>            </activation>            <build>                <filters>                    <filter>profile-dev.properties</filter>                </filters>            </build>        </profile>        <!-- 2.测试环境 -->        <profile>            <id>test</id>            <build>                <filters>                    <filter>profile-test.properties</filter>                </filters>            </build>        </profile>        <!-- 3.UAT环境 -->        <profile>            <id>uat</id>            <build>                <filters>                    <filter>profile-uat.properties</filter>                </filters>            </build>        </profile>        <!-- 4.生产环境 -->        <profile>            <id>product</id>            <build>                <filters>                    <filter>profile-product.properties</filter>                </filters>            </build>        </profile>    </profiles></project>
2. 用法:

比如profile-dev.properties文件中定义了

mvn.mysql.url.mydb=jdbc:mysql://localhost:3306/mydbmvn.mysql.user.mydb=rootmvn.mysql.pass.mydb=1234

那么可以在其他公用配置文件中使用${}来引用profile-dev.properties中的属性,比如:

(1). db.properties中:
db.url=${mvn.mysql.url.mydb} db.user=${mvn.mysql.user.mydb}db.pass=${mvn.mysql.pass.mydb}
(2)   spring-ds.xml中:(用法1)
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">        <property name="url" value="${mvn.mysql.url.mydb}"/>        <property name="username" value="${mvn.mysql.user.mydb}"/>        <property name="password" value="${mvn.mysql.pass.mydb}"/>    </bean>

spring-ds.xml中,也可以使用这样:(用法2-引用再引用)

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">        <property name="url" value="${db.url}"/>        <property name="username" value="${db.user}"/>        <property name="password" value="${db.pass}"/></bean>

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP