一次构建,多环境部署。

3
我们有一个遗留系统,包含50-60个Maven模块。所有这些模块都使用了Maven资源插件来过滤属性(在属性文件中替换标记)。当需要为不同的环境构建应用程序时,这非常麻烦,因为我们必须每次都构建应用程序。
我们有一个新的要求,即只需构建一次应用程序并部署到多个环境中。最好的解决方案是什么?我想将过滤属性外部化,但最大的问题是如何替换现有应用程序的属性文件中的标记(请参见下面的application.properties文件)。我希望保持现有的属性文件不变,并从外部配置文件中获取值。
任何帮助都将不胜感激。
例如: 将Filter.properties注入到Maven中。
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}
1个回答

2
根据我的经验,我曾经需要构建一个基于Spring的Web应用并在许多不同的环境中部署,因此我试图向您展示对我有效的解决方案。
我找不到将此任务移交给Maven的方法,因此我决定外部化配置,并发现利用Spring可以仅执行两个步骤即可实现:
1. 通过读取包含环境名称(例如DEV、TEST、PRODUCTION)的文件,使应用程序“知道”它被部署到的环境。 2. 根据找到的环境值,设置系统属性,然后加载放置在相应文件夹中的配置文件。
有了Tomcat,我们将配置文件夹结构设置在shared/classes下,像这样:
config structure 然后我们将application-config.properties文件放置在application-config文件夹中,其中包含环境的描述。
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>

我不得不编写一个类来覆盖Spring接口org.springframework.beans.factory.config.BeanFactoryPostProcessorpostProcessBeanFactory方法,代码如下:

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()方法中,首先通过注入的properties属性加载environmentDefinitionPropertyName,然后将其设置为系统属性,其键为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文件。
希望这对您有所帮助,并且也能帮助其他人。

这是一个适用于基于Spring的应用程序的很好解决方案,不幸的是我们的应用程序并不使用Spring。属性直接从Java中加载。 - Asela Senanayake

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接