Maven根据不同环境打包不同配置文件
程序员文章站
2022-05-04 09:55:17
...
开发过程中经常会用到dev、test、prod等不同环境的配置切换,maven就提供了这种解决方案,废话不说,原理不说(因为不懂),踩过很多坑,遇到很多坎,终于找到一种简洁可用的mavn配置方法。
经过检验下面的配置文件是正确的:
项目路径:
pom文件:
<profiles>
<profile>
<!-- 本地开发环境 -->
<id>dev</id>
<properties>
<env>dev</env>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<!-- 测试环境 -->
<id>test</id>
<properties>
<env>test</env>
</properties>
</profile>
<profile>
<!-- 生产环境 -->
<id>prod</id>
<properties>
<env>prod</env>
</properties>
</profile>
</profiles>
<build>
<sourceDirectory>src/main/java </sourceDirectory>
<testSourceDirectory>src/test/java</testSourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<webXml>src\main\webapp\WEB-INF\web.xml</webXml>
<warSourceDirectory>webapp</warSourceDirectory>
<archive>
<addMavenDescriptor>false</addMavenDescriptor>
</archive>
<warName>smartsimple</warName>
<webResources>
<resource>
<directory>src/main/resources/${env}</directory>
<targetPath>WEB-INF/classes</targetPath>
<filtering>true</filtering>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<excludes>
<exclude>dev/*</exclude>
<exclude>test/*</exclude>
<exclude>prod/*</exclude>
</excludes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources/${env}</directory>
</resource>
</resources>
</build>
打包命令:mvn -Pdev -Dmaven.test.skip=true install