MAVEN+eclipse打不通的包获取不同的properties文件
首先我们有三个文件
根据pom.xml来打测试,生成包及本地使用
<!-- 本地环境 -->
<id>local</id>
<properties>
<env>local</env>
<propertiesPath>classpath:env/</propertiesPath>
<javaPath>env/</javaPath>
</properties>
<!-- test环境 -->
<id>test</id>
<properties>
<env>test</env>
<propertiesPath>file:/ccicall/addrs/addrs_config/</propertiesPath>
<javaPath>/ccicall/addrs/addrs_config/</javaPath>
</properties>
<id>test</id>
<properties>
<env>test</env>
<propertiesPath>file:/ccicall/addrs/addrs_config/</propertiesPath>
<javaPath>/ccicall/addrs/addrs_config/</javaPath>
</properties>
<!-- 生产环境 -->
<id>product</id>
<properties>
<env>product</env>
<propertiesPath>file:/ccicall/addrs/addrs_config/</propertiesPath>
<javaPath>/ccicall/addrs/addrs_config/</javaPath>
</properties>
<id>product</id>
<properties>
<env>product</env>
<propertiesPath>file:/ccicall/addrs/addrs_config/</propertiesPath>
<javaPath>/ccicall/addrs/addrs_config/</javaPath>
</properties>
新建一个固定的resources.properties而文件里的内容
#标记
env=${env}
javaPath=${javaPath}
而我们的java代码是在这样的:
prop.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("resources.properties"));
env= prop.getProperty("env");
if("local".equals(env)){
javaPath="env/"+env+".properties";
prop.load(Thread.currentThread().getContextClassLoader().getResourceAsStream(javaPath));
}else {
javaPath= prop.getProperty("javaPath")+env+".properties";
prop.load(new BufferedInputStream(new FileInputStream(javaPath)));
}
这样就可以动态的获取不同的文件了 。在本地tomcat和weblogic环境获取的方法是不一样的。