原文出處:
https://faceghost.com/article/623591
先介紹分離后的命令:
啟動命令:
tomcat7:run -Pdev //開發環境 tomcat7:run -Ppro //生產環境
打war包命令
install -Pdev //開發環境 install -Ppro //生產環境
項目新增dev,pro目錄
在 /src/main/resources 新增文件夾 “dev” ,“pro”
如圖所示:

其中 dev 存放開發環境配置
其中 pro 存放生產環境配置
打開pom.xml ,修改參考如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
...
<dependencies>
...
</dependencies>
<!-- 配置文件 -->
<profiles>
<profile>
<!--開發環境 -->
<id>dev</id>
<properties>
<profiles.active>dev</profiles.active>
</properties>
</profile>
<profile>
<!--生產環境 -->
<id>pro</id>
<properties>
<profiles.active>pro</profiles.active>
</properties>
</profile>
</profiles>
<build>
<!--資源根目錄排除各環境的配置,使用單獨的資源目錄來指定 -->
<resources>
<resource>
<directory>src/main/resource</directory>
<excludes>
<exclude>/dev/*</exclude>
<exclude>/pro/*</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources/${profiles.active}</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<warName>xxx</warName>
<packagingExcludes>
WEB-INF/classes/dev/**,
WEB-INF/classes/pro/**</packagingExcludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>8080</port>
<path>/xxx</path>
<contextFile>\${tomcatContextXml}</contextFile>
<protocol>org.apache.coyote.http11.Http11NioProtocol</protocol>
<uriEncoding>utf-8</uriEncoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
轉載請注明:
原出處:見鬼網
原作者:xingfu2017
