相信每个开发同胞都有过打线上包的时候,因为线上和本地的配置不一样而手动的修改配置文件的烦恼,这篇文章就教你怎么使用maven命令全自动打不通环境的war包
备注:默认springMVC+maven环境已经搭建好
1:resources目录下新建目录
- dev (开发环境配置文件存放处)
- test (测试环境配置文件存放处)
- pro (线上环境配置文件存放处)
2:pom.xml中加入以下配置
<profiles>
<profile>
<id>dev</id>
<properties>
<env>dev</env>
</properties>
<!--默认为dev-->
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>test</id>
<properties>
<env>test</env>
</properties>
</profile>
<profile>
<id>pro</id>
<properties>
<env>pro</env>
</properties>
</profile>
</profiles>
<!-- 以上配置标明了环境变量 -->
<build>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.7</version>
<configuration>
<connectors>
<connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
<port>8888</port>
<maxIdleTime>30000</maxIdleTime>
</connector>
</connectors>
<webAppSourceDirectory>${project.build.directory}/${pom.artifactId}-${pom.version}</webAppSourceDirectory>
<contextPath>/</contextPath>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<excludes>
<exclude>dev/*</exclude>
<exclude>test/*</exclude>
<exclude>pro/*</exclude>
</excludes>
<filtering>false</filtering>
</resource>
<!-- ${env}和上面的配置呼应,参数由maven命令传入 -->
<resource>
<directory>src/main/resources/${env}</directory>
</resource>
<resources>
</build>