我们拥有50 -60 maven模块的遗留系统,所有这些module都使用maven资源插件来过滤属性(在属性文件中的构建时替换标记)。在构建不同的环境时,这非常痛苦,因为每当我们需要为不同的环境进行部署时,我们必须构建应用程序。
我们有一个新要求,即一次构建应用程序并部署到许多环境中。什么是最好的解决方案?我想到了外部化过滤器属性,但最大的问题是替换下面的应用程序的现有属性文件的标记(请参阅application.properties)文件。我想保留现有的属性文件,并从外部配置文件中选择值。
任何帮助都会非常感激。
e.g 注入maven的Filter.properties。
generic.sharepoint.host=xxxxx
generic.deploy.apps.host=xxxxx
generic.deploy.apps.url=xxxx
generic.deploy.trusted.host=xxxx
generic.deploy.trusted.url=xxxx
generic.deploy.orderentry=xxxxx
application.properties
generic.sharepoint.host=${generic.sharepoint.host}
generic.deploy.apps.host=${generic.deploy.apps.host}
generic.deploy.apps.url=${generic.deploy.apps.url}
generic.deploy.trusted.host=${generic.deploy.trusted.host}
generic.deploy.trusted.url=${generic.deploy.trusted.url}
generic.deploy.orderentry=${generic.deploy.orderentry}
答案 0 :(得分:2)
根据我的经验,我还必须构建一个基于Spring的Web应用程序并在许多不同的环境中部署,所以我试图向您展示对我来说工作正常的解决方案。
我找不到将这个任务交给Maven的方法,所以我决定外化配置,发现利用Spring我可以设法实现只执行两个步骤:
使用Tomcat,我们在shared/classes
下设置配置文件夹结构,方式如下:
我们放入application-config folder
包含环境描述的application-config.properties
文件:
application-config.env=DEV
并将每个子文件夹下的相同配置文件正确配置为所需的相关环境。
之后我们不得不向Spring应用程序上下文添加另一个application-config-context.xml
和一些bean定义,以便与org.springframework.beans.factory.config.PropertiesFactoryBean
进行交互:
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="environmentDetector" class="com.mycompany.myapp.spring.util.PropertiesBasedEnvironmentDetector" init-method="init" >
<property name="properties" ref="environmentDefinitionProperties" />
<property name="environmentDefinitionPropertyName" value="application-config.env"/>
</bean>
<bean id="environmentDefinitionProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="ignoreResourceNotFound" value="true" />
<property name="locations">
<list>
<value>classpath:application-config/application-config.properties</value>
</list>
</property>
</bean>
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="ignoreResourceNotFound" value="false" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="properties" ref="applicationExternalProperties" />
</bean>
<bean id="applicationExternalProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean" >
<property name="location" value="${application-config.prefix}application-config/${application-config.env}/application.properties" />
</bean>
</beans>
我必须编写一个类来覆盖接口postProcessBeanFactory
的Spring org.springframework.beans.factory.config.BeanFactoryPostProcessor
方法,如下所示:
package com.mycompany.myapp.doc.spring.util;
import java.util.Properties;
import javax.annotation.PostConstruct;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.core.PriorityOrdered;
public class PropertiesBasedEnvironmentDetector implements BeanFactoryPostProcessor, PriorityOrdered{
private static final Log log = LogFactory.getLog(PropertiesBasedEnvironmentDetector.class);
private Properties properties;
private String environmentDefinitionPropertyName ="env";
private String defaultEnvironment="DEV";
private String environmentSystemPropertyName;
private int order = PriorityOrdered.HIGHEST_PRECEDENCE;
private String prefixSystemPropertyName = "application-config.prefix";
private String prefixDefault = "classpath:";
private String prefix;
public void setProperties(Properties properties) {
this.properties = properties;
}
@PostConstruct
public void init()
{
if (environmentSystemPropertyName == null) {
environmentSystemPropertyName = environmentDefinitionPropertyName;
}
String activeEnvironment = properties.getProperty(environmentDefinitionPropertyName, defaultEnvironment);
prefix = properties.getProperty(prefixSystemPropertyName);
if (prefix == null) {
prefix = prefixDefault;
properties.put(prefixSystemPropertyName, prefix);
}
System.setProperty(environmentSystemPropertyName , activeEnvironment);
System.setProperty(prefixSystemPropertyName , prefix);
log.warn("Initializing Environment: "+activeEnvironment);
}
public String getEnvironmentDefinitionPropertyName() {
return environmentDefinitionPropertyName;
}
public void setEnvironmentDefinitionPropertyName(
String environmentDefinitionPropertyName) {
this.environmentDefinitionPropertyName = environmentDefinitionPropertyName;
}
public String getDefaultEnvironment() {
return defaultEnvironment;
}
public void setDefaultEnvironment(String defaultEnvironment) {
this.defaultEnvironment = defaultEnvironment;
}
public String getEnvironmentSystemPropertyName() {
return environmentSystemPropertyName;
}
public void setEnvironmentSystemPropertyName(String environmentSystemPropertyName)
{
this.environmentSystemPropertyName = environmentSystemPropertyName;
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
throws BeansException
{
String activeEnvironment = System.getProperty(environmentSystemPropertyName);
log.warn("PostProcessing ApplicationContext for Environment: "+activeEnvironment+" from "+prefix);
}
@Override
public int getOrder() {
return order;
}
public void setOrder(int order) {
this.order = order;
}
}
当应用程序启动时,Spring会加载所有上下文并调用我们的自定义类。
在init()
方法中,首先通过注入的environmentDefinitionPropertyName
属性加载properties
,然后设置为一个系统属性,其中包含bean定义中设置的environmentDefinitionPropertyName
值作为键。
之后,PropertyPlaceholderConfigurer
可以加载属性文件位置,因为它解析了:
<property name="location" value="${application-config.prefix}application-config/${application-config.env}/application.properties" />
到
<property name="location" value="classpath:application-config/DEV/application.properties" />
这种方法具有以下主要优点:
key=value
格式,与application.properties文件类似。希望这有所帮助,也可以帮助其他人。