根据环境激活配置文件 [英] Activate a profile based on environment

查看:156
本文介绍了根据环境激活配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的情况:

  • Maven 2.0.9是我们的构建系统
  • 我们将代码安装到多个环境
  • 我们所有特定于环境的属性都包含在属性文件中,每个文件对应一个环境
    • 我们目前使用 properties-maven-plugin 将这些属性读入Maven. ;这个子项目不是必需的,只是我们当前的解决方案
    • Maven 2.0.9 is our build system
    • We install code to multiple environments
    • All of our environment-specific properties are contained in property files, one for each environment
      • We currently read these properties into maven using the properties-maven-plugin; this sub-bullet is not a requirement, just our current solution

      目标:

      • 仅在特定环境下执行构建的某些部分(即插件执行)
      • 通过在特定于环境的属性文件中设置值来控制要运行的零件

      到目前为止我已经尝试过的:

      What I've tried so far:

      • Maven允许将插件执行放在pom配置文件中,可以通过属性激活该配置文件;不幸的是,这些必须是 system 属性-即.从settings.xml或命令行中获取,而不是从properties-maven-plugin
      • 加载的属性中获取
      • Maven allows plugins executions to be put inside pom profiles, which can be activated by properties; unfortunately these must be system properties - ie. from settings.xml or the command-line, not from properties loaded by the properties-maven-plugin

      如果可能的话,我们希望将所有内容封装在构建工作区中,如下所示:

      If possible, we'd like to keep everything encapsulated within the build workspace, which looks something like this:

      project
          pom.xml
          src
             ...
          conf
             dev.properties
             test.properties
             prod.properties
          build-scripts
             build.groovy <-- the script that wraps maven to do the build
             install.groovy <-- ... wraps maven to do the install
      

      运行构建的过程如下:

      cd build-scripts
      ./build.groovy
      ./install.groovy -e prod
      

      使用我们使用的Maven版本是否可以实现这些目标?如果没有,那么使用较新版本的maven是否可能?

      Is there any possible way to accomplish these goals with the version of maven we are using? If not, is it possible with a newer version of maven?

      推荐答案

      仅使用Maven不可能做到这一点. (另请参见如何通过Maven属性激活配置文件?)原因是,首先要评估配置文件,然后再进行其他评估,以确定有效的POM .

      This isn't possible using just Maven. (See also How to activate profile by means of maven property?) The reason is that profiles are the first thing evaluated before anything else to determine the effective POM.

      我的建议是编写一些预处理器,以在启动Maven之前解析您的环境特定的属性文件并将其转换为所需的系统属性.该脚本可以包含在您的~/.mavenrc中,以便在启动Maven之前自动运行.这是一个示例脚本,该脚本假定属性文件位于固定位置:

      My suggestion is to write some preprocessor that parses your environment specific property files and converts them to the required system properties before launching Maven. This script can be included in your ~/.mavenrc so that it runs automatically before Maven is launched. Here is an example script that that assumes the properties file is in a fixed location:

      properties=`cat /etc/build-env.properties`
      while read line; do
         MAVEN_OPTS="$MAVEN_OPTS -D$line"
      done <<< "$properties"
      

      如果属性文件不是固定的,则只需向脚本中添加一些内容即可发现位置(假设它是可发现的).

      If the properties file is not fixed, you'll just need to add something to the script to discover the location (assuming it is discoverable).

      这篇关于根据环境激活配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆