MAVEN开发,测试环境Properties的替换支持

参考

http://blog.csdn.net/stark_summer/article/details/42743007


1)在src/main/resources下面建立dev,product目录

      src/main/resources/dev

      src/main/resources/product

2)分别在这两个路径下放置适合各自环境的配置文件,如db.properties, log4j.properties等

3)配置pom,进行环境的选择。

   3.1)配制plugin

<plugin>

                <groupId>org.apache.maven.plugins</groupId>

                <artifactId>maven-war-plugin</artifactId>

                <configuration>

                    <archive>

                        <addMavenDescriptor>false</addMavenDescriptor>

                    </archive>

                    <webResources>

                        <resource>

                            <!-- this is relative to the pom.xml directory -->

                            <directory>src/main/resources/${package.environment}</directory>

                            <targetPath>WEB-INF/classes</targetPath>

                            <filtering>true</filtering>

                        </resource>

                    </webResources>

                    <packagingExcludes>

                        WEB-INF/classes/dev/,

                        WEB-INF/classes/product/

                    </packagingExcludes>

                </configuration>

            </plugin>

    3.2) 配置的Profiles

<profiles>

        <profile>

            <id>product</id>

            <properties>

                <package.environment>product</package.environment>

            </properties>

        </profile>

        <profile>

            <id>dev</id>

            <properties>

                <package.environment>dev</package.environment>

            </properties>

            <activation>

                <activeByDefault>true</activeByDefault>

            </activation>

        </profile>

    </profiles>

<properties>

        <package.environment>dev</package.environment>

</properties>

3.3) 执行mvn clean package -Pdev  完成对dev环境的应用生成。

注:其实可以在java/main/resources目录下直接放开发环境的配置,这样在开发时就不需要做任何修正。只有要发布其他环境的时候,才执行maven命令进行包的生成。


jar文件的替换策略

<plugin>

        <groupId>org.apache.maven.plugins</groupId>

        <artifactId>maven-antrun-plugin</artifactId>

        <executions>

          <execution>

            <id>compile</id>

            <phase>compile</phase>

            <configuration>

              <tasks>

                <echo message="********************** copy resources to classpath*************************"/>

                <echo message="********************** copy ${resources.dir} to classpath*************************"/>

                <copy todir="target/classes" overwrite="true">

                  <fileset dir="${resources.dir}">

                    <include name="*.*"/>

                    <include name="*/*.*"/>

                  </fileset>

                </copy>

              </tasks>

            </configuration>

            <goals>

              <goal>run</goal>

            </goals>

          </execution>

        </executions>

      </plugin>



你可能感兴趣的:(MAVEN开发,测试环境Properties的替换支持)