版权声明:本文为Niz原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_39403734/article/details/89134072
因网上没有找到自己需要的参考案例,所以在此自己整理了一下;
1. 在pom.xml配置不同的环境ID
<profiles>
<profile>
<!-- 本地开发环境 -->
<id>dev</id>
<properties>
<package.environment>dev</package.environment>
</properties>
<activation>
<activeByDefault>true</activeByDefault> <!-- 默认使用 -->
</activation>
</profile>
<profile>
<!-- 测试环境 -->
<id>test</id>
<properties>
<package.environment>test</package.environment>
</properties>
</profile>
<profile>
<!-- 生产环境 -->
<id>main</id>
<properties>
<package.environment>main</package.environment>
</properties>
</profile>
</profiles>
2. 项目资源的放置,根据个人喜好,这里给大家看一下我的放置
这里的三个文件夹分配对应以上三个环境ID,properties根目录放准备被替换的文件( 另外的作用,给Maven的tomcat插件使用; )
3. 配置资源目录
<resources>
<resource>
<!-- 资源目录 -->
<directory>src/main/resources/</directory>
<excludes><!-- 清除 -->
<exclude>properties/dev/*</exclude>
<exclude>properties/test/*</exclude>
<exclude>properties/main/*</exclude>
</excludes>
</resource>
</resources>
为什么清除了dev,test和main文件及文件夹呢?因为避免war包里面出现多余的文件及文件夹,所以先清除;
资源目录里面有spring,mybatis和log4j等资源,所以不能清除;
4. 配置打包插件
<build>
<finalName>项目名</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<archive>
<addMavenDescriptor>false</addMavenDescriptor>
</archive>
<warName>项目名</warName>
<webResources>
<resource>
<directory>src/main/resources/properties/${package.environment}</directory>
<targetPath>WEB-INF/classes/properties</targetPath>
<filtering>true</filtering>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>
将获取src/main/resources/properties/${package.environment}里面的文件
加载到WEB-INF/classes/properties里面
5. 最终结果
原本properties根目录的文件会被替换掉
总结:该插件大大提升了面对不同环境打不同war包的效率,且清除了没用的资源文件;
小生初步使用该插件的总结,不喜勿喷,请各路大神多多指点一二;