java sw,java-项目环境隔离实现
实现为参考开源项目https://github.com/shuzheng/zheng.git,很不错的项目
1.demo结构
2.pom中添加web项目各种包,重点在下面,profiles节点用来配置需要的环境,build --> filters读取profile中的env加载指定的配置文件
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4.0.0
com.test.group
mvnprofile
war
0.0.1-SNAPSHOT
mvnprofile Maven Webapp
http://maven.apache.org
1.7
4.2.1.RELEASE
1.7.12
UTF-8
taglibs
standard
1.1.2
dev
dev
true
test
test
prod
prod
zheng-upms-server
src/main/resources/profiles/${env}.properties
src/main/resources
true
org.eclipse.jetty
jetty-maven-plugin
9.2.7.v20150116
3
/
1111
automatic
org.apache.maven.plugins
maven-surefire-plugin
2.18.1
true
true
3.新建springMVC-servlet.xml测试配置是否生效
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">
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文件中的占位符会被替换为指定环境的参数