Java 将环境变量从 Jenkins 传递给 Ant build.xml?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24820204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 15:01:03  来源:igfitidea点击:

Pass environment variables to Ant build.xml from Jenkins?

javajenkinscontinuous-integrationhudsonjenkins-cli

提问by user755806

I am using Jenkins as CI. I have an build.xml. Build.xmlhas code like below.

我使用 Jenkins 作为 CI。我有一个 build.xml。Build.xml有如下代码。

<property name="environment" value="$environment}" />

How can i pass value to build.xml from Jenkins ? Can i pass it through environment variables?

我如何将值从 Jenkins 传递给 build.xml?我可以通过环境变量传递它吗?

采纳答案by neomega

You can set properties with build parameters (ie: myParameter) and get them in an ant script with ${myParameter}.

您可以使用构建参数(即:myParameter)设置属性,并使用${myParameter}.

Just make sure you don't use dots in parameters names in jenkins because there might be problems getting them in the ant script.

只要确保你不在 jenkins 的参数名称中使用点,因为在 ant 脚本中获取它们可能会出现问题。

As for environment variables, i don't know, sorry.

至于环境变量,我不知道,抱歉。

回答by abhinav

Considering parametrized build as in the case above e.g you must have set variable called environmentin the job and getting its value from user.Now you can refer to its value in build.xml using property name="environment" value="$environment}

考虑上述情况中的参数化构建,例如,您必须environment在作业中调用设置变量并从用户那里获取其值。现在您可以使用 build.xml 引用其值property name="environment" value="$environment}

回答by taringamberini

If your ant build dependeds on the property environment:

如果您的蚂蚁构建依赖于属性environment

<property
  name="environment"
  value="a_value_I_edit_just_before_run_ant_default_target_mannually" />

you could configure your Jenkins' job to build with parameter: enter image description here

您可以将 Jenkins 的作业配置为使用参数构建: 在此处输入图片说明

so Jenkins will log (i.e. on Windows) something like:

所以詹金斯会记录(即在 Windows 上)类似的东西:

cmd.exe /C '"ant.bat ant -file build.xml -Djenkins_environment=myValue && exit %%ERRORLEVEL%%"'

Then you could modify your ant build to check for system property:

然后你可以修改你的 ant 构建来检查系统属性:

<property
  name="manually_edited_environment"
  value="a_value_I_edit_just_before_run_ant_default_target_manually" />

<condition property="environment"
           value="${jenkins_environment}"
           else="${manually_edited_environment}">
    <isset property="jenkins_environment" />
</condition>

if the jenkins_environmentproperty is set then environmentgets its value, else environmentgets manually_edited_environment's value; the remaining part of your build still depend on the property environment.

如果jenkins_environment设置了属性,则environment获取其值,否则environment获取manually_edited_environment的值;您构建的其余部分仍然取决于属性environment

If you have trouble in correctly matching the -DpropertyNameuse the echoproperties ant taskto make ant logging all system properties.

如果您在正确匹配时遇到问题,请-DpropertyName使用echoproperties ant 任务使 ant 记录所有系统属性。

回答by Straff

Your environment variables are available via the environment property. In the example below, the environment variable VIEWis printed from the simple hello world ant script via ${env.VIEW}. Change VIEW to the name of the environment variable of interest.

您的环境变量可通过环境属性获得。在下面的示例中,环境变量VIEW是通过${env.VIEW}从简单的 hello world ant 脚本打印出来的。将 VIEW 更改为感兴趣的环境变量的名称。

<?xml version="1.0" encoding="UTF-8"?>
<project name="Hello World" default="Hello" basedir=".">
    <property environment="env"/>
    <property name="HelloText" value="Hello"/>
    <target name="Hello">
        <echo>VIEW=${env.VIEW}</echo>
    </target>
</project> 

IMPORTANT!Note that this line is needed in order for env.VIEWto be understood by ant:

重要的!请注意,为了env.VIEW被蚂蚁理解,需要这一行:

<property environment="env"/>

回答by venkateshwar reddy

your ant script should look like this:-

你的蚂蚁脚本应该是这样的:-

<target name="test" >
    <property environment="env" />
    <echo>BUILD_NUMBER: ${env.BUILD_NUMBER}</echo>
</target>