1. 程式人生 > >maven編譯打包切換test,dev,prod環境

maven編譯打包切換test,dev,prod環境

1.pom.xml加上profile資訊:(profiles與build是同級的)

 <profiles>
        <profile>
            <id>dev</id>
            <properties>
                <profiles.activation>dev</profiles.activation>
            </properties>
            <activation>
                <!--預設為dev環境打包方式-->
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <profiles.activation>test</profiles.activation>
            </properties>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <profiles.activation>prod</profiles.activation>
            </properties>
        </profile>
    </profiles>

2.在src/rescource加3個資料夾

        test/xx.properties

        dev/xx.properties

        prod/xx.properties

3.在pom的build配置項中編譯打包過濾 3個環境的資料夾 並定義profile的變數

<resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <!-- 排除dev、test目錄下的檔案 -->
                <excludes>
                    <exclude>dev/*</exclude>
                    <exclude>test/*</exclude>
                    <exclude>prod/*</exclude>
                </excludes>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <!-- 包含,若沒有指定則預設為 activeByDefault 標籤定義的profile -->
                <includes>
                    <include>${profiles.activation}/*</include>
                </includes>
            </resource>
        </resources>

4.在application配置檔案上進行寫上對應配置檔案的引用

<bean id="propertyConfig"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:${profiles.activation}/xxx.properties</value>
            </list>
        </property>
    </bean>

5.在logback.xml引入日誌寫入的路徑

<configuration>
    <property resource="${profiles.activation}/xxx.properties" />
	<!--<property name="LOG_HOME" value="${TAS_HOME:-..}/logs"/>-->
	<property name="LOG_HOME" value="${lockback.log.path}"/>

6.自動化編譯打包的指令為:(由引數 -P區分開)

mvn clean install -Dmaven.test.skip=true -Ptest
mvn clean install -Dmaven.test.skip=true -Pdev
mvn clean install -Dmaven.test.skip=true -Pprod