实现为参考开源项目https://github.com/shuzheng/zheng.git,很不错的项目
1.demo结构
2.pom中添加web项目各种包,重点在下面,profiles节点用来配置需要的环境,build --> filters读取profile中的env加载指定的配置文件
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test.group</groupId>
<artifactId>mvnprofile</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>mvnprofile Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<java-version>1.7</java-version>
<org.springframework-version>4.2.1.RELEASE</org.springframework-version>
<org.slf4j-version>1.7.12</org.slf4j-version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- standard -->
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
</dependencies>
<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>
<finalName>zheng-upms-server</finalName>
<filters>
<filter>src/main/resources/profiles/${env}.properties</filter>
</filters>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<!-- jetty插件 -->
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<!--<version>9.0.0.v20130308</version>-->
<version>9.2.7.v20150116</version>
<configuration>
<scanIntervalSeconds>3</scanIntervalSeconds>
<webApp>
<contextPath>/</contextPath>
</webApp>
<httpConnector>
<port>1111</port>
</httpConnector>
<reload>automatic</reload>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<skipTests>true</skipTests>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
</plugins>
</build>
</project>
3.新建springMVC-servlet.xml测试配置是否生效
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 根目录'/'对应页面 -->
<mvc:view-controller path="/" view-name="/index.jsp"/>
<context:property-placeholder location="classpath:config.properties"/>
<!-- thymeleaf视图 -->
<bean id="templateResolver" class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver">
<property name="order" value="0"/>
<property name="prefix" value="${zheng.ui.path}"/>
<property name="suffix" value=""/>
<property name="templateMode" value="HTML"/>
<property name="cacheable" value="false"/>
<property name="characterEncoding" value="UTF-8"/>
<!--<property name="cacheable" value="true"/>-->
<!--<property name="cacheTTLMs" value="10000"/>-->
</bean>
<bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver"/>
<property name="enableSpringELCompiler" value="true"/>
</bean>
<bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine"/>
<property name="characterEncoding" value="UTF-8"/>
<property name="viewNames" value="/${app.name}/*"/>
</bean>
</beans>
4.confi.properties文件内容
app.name=${app.name}
env=${profile.env}
5.dev.properties文件中的内容
profile.env=dev
app.name=xxxx-web-dev
zheng.ui.path=http://xxxxx:1000/
6.执行maven打包命令,默认会使用dev模式,指定打包的环境信息使用,mvn clean package -P test即可打包指定环境配置的包
7.打包后解压查看xml文件中的占位符会被替换为指定环境的参数