參考java
http://blog.csdn.net/stark_summer/article/details/42743007web
1)在src/main/resources下面創建dev,product目錄
apache
src/main/resources/devmaven
src/main/resources/productthis
2)分別在這兩個路徑下放置適合各自環境的配置文件,如db.properties, log4j.properties等
spa
3)配置pom,進行環境的選擇。.net
3.1)配製pluginxml
<plugin>blog <groupId>org.apache.maven.plugins</groupId>ip <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>