如何阻止Maven扩展环境.有詹金斯变量吗? [英] How to stop Maven from expanding env. variable w/Jenkins?

查看:98
本文介绍了如何阻止Maven扩展环境.有詹金斯变量吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我熟悉Jenkins,但对Maven还是陌生的,并且我试图弄清楚如何阻止Jenkins中的Maven作业扩展ApplicationContext.xml文件内部的环境变量.

I'm familiar with Jenkins, but new to Maven, and I'm trying to figure out how to stop our Maven jobs in Jenkins from expanding an environment variable inside of the ApplicationContext.xml file.

在我们的ApplicationContext.xml文件中,我们引用了一个${DeployMode}环境变量(由我们创建),Tomcat在加载/运行时对其进行了扩展:

Inside our ApplicationContext.xml file we reference a ${DeployMode} environment variable (that we created) which Tomcat expands at load/run time:

<!-- SPRING CONTEXT static accessor -->
<beans:bean id="contextApplicationContextProvider"
            class="com.dartneuroscience.compserv.rest.appcontext.AppContextProvider">
  <beans:constructor-arg>
    <beans:value>${DeployMode}</beans:value>
  </beans:constructor-arg>
</beans:bean>

<beans:bean id="placeholderConfig"
            class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <beans:property name="location"
                  value="/WEB-INF/App_${DeployMode}.properties" />
</beans:bean>

问题是,在Jenkins运行的Maven构建中,如果在构建机器上设置了环境变量,则构建的WEB-INF/ApplicationContext.xml看起来像这样(在本示例中为'Prod'):

The problem is that in a Maven build run by Jenkins, the built WEB-INF/ApplicationContext.xml looks like this if there's an environment variable set on the build machine (let's say to 'Prod' in this example):

<!-- SPRING CONTEXT static accessor -->
<beans:bean id="contextApplicationContextProvider"
            class="com.dartneuroscience.compserv.rest.appcontext.AppContextProvider">
  <beans:constructor-arg>
    <beans:value>Prod</beans:value> <!-- Expanded env. var -->
  </beans:constructor-arg>
</beans:bean>

<beans:bean id="placeholderConfig"
            class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <beans:property name="location"
                  value="/WEB-INF/App_Prod.properties" /> <!-- Expanded env. var -->
</beans:bean>

因此,该值现在被硬编码"到WAR中,并且即使目标Web服务器将其DeployMode环境变量设置为其他变量(如暂存"),也将始终像"Prod"一样工作.

And so the value is now "hard coded" into the WAR, and will always act like "Prod" even if the target web server has its DeployMode environment variable set to something else, like 'Staging'.

当我在同一台构建服务器上手动运行Maven时,不会发生这种情况-仅在Jenkins生成作业时发生.

This does not happen when I run Maven manually on the same build server--it only happens when Jenkins builds the job.

我可以传递给Jenkins阻止该行为的设置吗?

Is there a setting I can pass to Jenkins to stop this behavior?

我已经查看过诸如EnvInject插件之类的选项,以在运行作业时对所有环境变量进行UNSET的操作,但我对此行为感到非常困惑,并想深入了解它.

I've looked at options like the EnvInject Plugin to UNSET all environment variables as a job is run, but I am really puzzled by this behavior and would like to get to the bottom of it.

谢谢.

我在顶级POM中找到了以下资源过滤块,并添加了<excludes/>块以跳过我们的AppContext.xml文件:

I found the following resource filtering block in the top-level POM and added the <excludes/> block to skip our AppContext.xml file:

  <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.1.1</version>
        <configuration>
          <webResources>
            <webResource>
              <directory>${basedir}/src/main/webapp/WEB-INF</directory>
              <includes>
                <include>*.xml</include>
              </includes>
              <excludes>
                <exclude>*AppContext.xml</exclude>
              </excludes>
              <targetPath>WEB-INF</targetPath>
              <filtering>true</filtering>
            </webResource>
          </webResources>

但是,这仍然无法回答为什么在由詹金斯(Jenkins)运行时将环境变量添加到属性列表,而在从命令行运行时将其忽略的原因;尽管我几年前确实发现了这个哈德逊问题:从哈德逊运行时资源过滤失败

But this still doesn't answer why the environment variables are added to the properties list when run by Jenkins, and ignored when run from the command line; though I did find this Hudson issue from a couple of years ago: Resource filtering fails when run from Hudson

此外,EnvInjectPlugin会执行其广告,但是在删除至少PATH变量时,由于maven找不到ls命令,因此构建中断了.

Also, EnvInjectPlugin does what it advertises, but in removing at least the PATH var, the build broke because maven could not find the ls command.

将Jenkins中的工作从"Maven 2/3项目"更改为自由风格的软件项目",并使用Invoke top-level Maven targets构建步骤可产生所需的结果,而无需修改POM.

Changing the job in Jenkins from a "Maven 2/3 project" to a "Free-style software project" and using the Invoke top-level Maven targets build step produces the desired result without having to modify the POM.

推荐答案

Jenkins可能以某种方式启用了资源过滤.您可以选择多种配置过滤.您可以完全禁用它,将过滤限制为具有某些扩展名的文件,告诉它不要在过滤器中包含构建属性,或者选择要完全过滤的其他分隔符.请参见 resources:resources文档资源过滤概念的描述.

Jenkins may be enabling resource filtering somehow. You have lots of choices for configuring filtering. You may disable it entirely, limit filtering to files with certain extensions, tell it not to include build properties in the filters, or choose different delimiters to be filtered entirely. See the resources:resources docs or the description of resource filtering concepts.

这篇关于如何阻止Maven扩展环境.有詹金斯变量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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