`

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

阅读更多

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/mydb
mvn.mysql.user.mydb=root
mvn.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>
0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics