【问题标题】:Build once and deploy for many environments一次构建并为多个环境部署
【发布时间】:2015-04-10 02:39:20
【问题描述】:

我们有带有 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}

【问题讨论】:

    标签: java maven deployment web-deployment


    【解决方案1】:

    根据我的经验,我还必须构建一次基于 Spring 的 Web 应用程序并部署在许多不同的环境中,因此我试图向您展示适合我的解决方案。

    我找不到将这个任务交给 Maven 的方法,所以我决定将配置外部化,并发现利用 Spring 我只需执行两个步骤就可以实现:

    1. 通过读取包含环境名称(例如 DEV、TEST、PRODUCTION)的文件,使应用程序“了解”其部署到的环境
    2. 根据找到的环境值,设置系统属性,然后加载对应文件夹下的配置文件。

    有了Tomcat,我们在shared/classes下设置配置文件夹结构如下:

    我们将包含环境描述的application-config.properties文件放入application-config folder

    application-config.env=DEV
    

    以及每个子文件夹下的相同配置文件正确配置为所需的相关环境。 之后,我们必须向 Spring 应用程序上下文添加另一个带有一些 bean 定义的 application-config-context.xml,以便与 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>
    

    我必须编写一个类来覆盖接口 org.springframework.beans.factory.config.BeanFactoryPostProcessor 的 Spring 的 postProcessBeanFactory 方法,如下所示:

    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 加载。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-15
    • 1970-01-01
    • 2020-11-05
    • 2021-10-08
    • 1970-01-01
    • 2020-05-13
    • 1970-01-01
    相关资源
    最近更新 更多