一, 我們平時在項目開發過程中會根據不同的環境打包不同的文件。如數據庫連接文件,三方接口文件,日志文件等等,他們在開發,測試和線上環境的配置不一樣,部署測試環境或線上環境都要改相應文件,很繁瑣。可以利用maven maven-resources-plugin插件幫我們簡化這一過程。
二.配置pom文件:
<build> <plugins> <!-- 不同環境的配置文件選擇 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.6</version> <executions> <execution> <id>copy-resources</id> <phase>compile</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <!-- 覆蓋原有文件 --> <overwrite>true</overwrite> <outputDirectory>${project.build.outputDirectory}</outputDirectory> <!-- 待處理的資源定義 --> <resources> <resource> <!-- 指定resources插件處理哪個目錄下的資源文件 --> <directory>src/main/resources/${package.environment}</directory> <filtering>false</filtering> </resource> </resources> </configuration> <inherited></inherited> </execution> </executions> </plugin> </plugins> <resources> <resource> <directory>src/main/resources</directory> <excludes> <exclude>test/**</exclude> <exclude>product/**</exclude> </excludes> <filtering>true</filtering> </resource> </resources> </build> <profiles> <profile> <id>dev</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <package.environment>dev</package.environment> </properties> </profile> <profile> <id>test</id> <properties> <package.environment>test</package.environment> </properties> </profile> <profile> <id>product</id> <properties> <package.environment>product</package.environment> </properties> </profile> </profiles>
三.src/main/resources目錄下創建不同環境下的文件
1.本地開發環境直接在此目錄下添加;
2.測試環境在此目錄下創建名為”test”的文件夾,下面放的是測試環境部署文件;
3.線上環境在此目錄下創建名為”product”的文件夾,下面放的是線上環境部署文件。
四.打包
1.本地打包,默認maven打包

2.測試環境打包,添加clean install -Ptest maven 參數

3.生產環境打包,添加clean install -Pproduct

