当试图制作一个Jenkins项目时,我观察到一个奇怪的行为,该项目的Ant步骤接受Ant使用的任何String参数。它看起来像Ant以某种方式收到一些格式不正确的内容作为参数。从Jenkins Ant Plugin将参数传递给Ant
首先我登录安装在詹金斯在同一系统上,并写了一个简单的Ant构建文件:
<?xml version="1.0"?>
<!--DOCTYPE project PUBLIC "-//ANT//DTD project//EN" "/usr/src/ant/project.dtd"-->
<project name="many-params" default="run">
<target name="run">
<echo message="first Param: ${firstparam}"/>
<echo message="second Param: ${secondparam}"/>
</target>
</project>
然后我跑了设置参数的蚂蚁,得到了预期的输出:
$ ant -Dfirstparam=A -Dsecondparam=B
Buildfile: /tmp/build-dir/build.xml
run:
[echo] first Param: A
[echo] second Param: B
BUILD SUCCESSFUL
Total time: 0 seconds
接下来,我开始在Jenkins设定一个项目。我詹金斯系统规格如下:
- OS: Linux的AMD64 2.6.32-504.1.3.el6.x86_64
- 詹金斯:版本。 1.618
- JRE詹金斯在其上运行:甲骨文1.7.0_55-b13中
- 蚂蚁插件版本: 1.2
- JRE用于项目:甲骨文45年1月6日32位
- 蚂蚁用于该项目: 1.8.2
我已经设置了8个连续步骤的作业,4个准备shell脚本块,重写b具有略微不同的参数名称(没有分隔符,破折号分隔符,点分隔符)的uild.xml以及4个调用ant的Ant构建步骤,尝试传递相应的参数。
从作业目录的准备config.xml文件看起来像:
<?xml version='1.0' encoding='UTF-8'?>
<project>
<actions/>
<description>A simple task to test many parameter setting for jenkins calling ant tasks</description>
<logRotator class="hudson.tasks.LogRotator">
<daysToKeep>-1</daysToKeep>
<numToKeep>10</numToKeep>
<artifactDaysToKeep>-1</artifactDaysToKeep>
<artifactNumToKeep>-1</artifactNumToKeep>
</logRotator>
<keepDependencies>false</keepDependencies>
<scm class="hudson.scm.NullSCM"/>
<canRoam>true</canRoam>
<disabled>false</disabled>
<blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
<blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
<jdk>Oracle Java 1.6.45 32-bit JRE</jdk>
<triggers/>
<concurrentBuild>false</concurrentBuild>
<builders>
<hudson.tasks.Shell>
<command>echo "STEP 1: Pass parameters with no separators in names"
cat > build.xml << EOF
<?xml version="1.0"?>
<!--DOCTYPE project PUBLIC "-//ANT//DTD project//EN" "/usr/src/ant/project.dtd"-->
<project name="many-params" default="run">
<target name="run">
<echo message="first Param: ${paramfirst}"/>
<echo message="second Param: ${paramsecond}"/>
</target>
</project>
EOF</command>
</hudson.tasks.Shell>
<hudson.tasks.Ant plugin="[email protected]">
<targets>run</targets>
<antName>Ant 1.8.2</antName>
<properties>paramfirst=A
paramsecond=B</properties>
</hudson.tasks.Ant>
<hudson.tasks.Shell>
<command>rm build.xml
echo
echo "STEP 2: Pass parameters with separated by dashes in names"
cat > build.xml << EOF
<?xml version="1.0"?>
<!--DOCTYPE project PUBLIC "-//ANT//DTD project//EN" "/usr/src/ant/project.dtd"-->
<project name="many-params" default="run">
<target name="run">
<echo message="first Param: ${param-first}"/>
<echo message="second Param: ${param-second}"/>
</target>
</project>
EOF</command>
</hudson.tasks.Shell>
<hudson.tasks.Ant plugin="[email protected]">
<targets>run</targets>
<antName>Ant 1.8.2</antName>
<properties>param-first=A
param-second=B</properties>
</hudson.tasks.Ant>
<hudson.tasks.Shell>
<command>echo
echo "STEP 3: Same, but with parameters on the same line in Ant Plugin step setup"</command>
</hudson.tasks.Shell>
<hudson.tasks.Ant plugin="[email protected]">
<targets>run</targets>
<antName>Ant 1.8.2</antName>
<properties>param-first=A param-second=B</properties>
</hudson.tasks.Ant>
<hudson.tasks.Shell>
<command>rm build.xml
echo
echo "STEP 4: Pass parameters with separated by dots in names"
cat > build.xml << EOF
<?xml version="1.0"?>
<!--DOCTYPE project PUBLIC "-//ANT//DTD project//EN" "/usr/src/ant/project.dtd"-->
<project name="many-params" default="run">
<target name="run">
<echo message="first Param: ${param.first}"/>
<echo message="second Param: ${param.second}"/>
</target>
</project>
EOF</command>
</hudson.tasks.Shell>
<hudson.tasks.Ant plugin="[email protected]">
<targets>run</targets>
<antName>Ant 1.8.2</antName>
<properties>param.first=A
param.second=B</properties>
</hudson.tasks.Ant>
</builders>
<publishers/>
<buildWrappers>
<hudson.plugins.ws__cleanup.PreBuildCleanup plugin="[email protected]">
<deleteDirs>false</deleteDirs>
<cleanupParameter></cleanupParameter>
<externalDelete></externalDelete>
</hudson.plugins.ws__cleanup.PreBuildCleanup>
</buildWrappers>
</project>
建设任务给了我下面的输出:
Started by user Alexander Krasnyansky
[EnvInject] - Loading node environment variables.
Building remotely on j03_uk1ps41 (buildslaves) in workspace /var/lib/jenkins03/workspace/akrasnyansky/ManyParams/ant-param-test
Deleting project workspace... done
[ant-param-test] $ /bin/sh -xe /tmp/hudson2203137136163442442.sh
+ echo 'STEP 1: Pass parameters with no separators in names'
STEP 1: Pass parameters with no separators in names
+ cat
[ant-param-test] $ /usr/local/apache-ant-1.8.2/bin/ant -Dparamfirst=A -Dparamsecond=B run
Buildfile: /var/lib/jenkins03/workspace/akrasnyansky/ManyParams/ant-param-test/build.xml
run:
[echo] first Param:
[echo] second Param:
BUILD SUCCESSFUL
Total time: 0 seconds
[ant-param-test] $ /bin/sh -xe /tmp/hudson5869719507232196626.sh
+ rm build.xml
+ echo
+ echo 'STEP 2: Pass parameters with separated by dashes in names'
STEP 2: Pass parameters with separated by dashes in names
+ cat
[ant-param-test] $ /usr/local/apache-ant-1.8.2/bin/ant -Dparam-first=A -Dparam-second=B run
Buildfile: /var/lib/jenkins03/workspace/akrasnyansky/ManyParams/ant-param-test/build.xml
run:
[echo] first Param: first
[echo] second Param: second
BUILD SUCCESSFUL
Total time: 0 seconds
[ant-param-test] $ /bin/sh -xe /tmp/hudson1052316660761572548.sh
+ echo
+ echo 'STEP 3: Same, but with parameters on the same line in Ant Plugin step setup'
STEP 3: Same, but with parameters on the same line in Ant Plugin step setup
[ant-param-test] $ /usr/local/apache-ant-1.8.2/bin/ant "-Dparam-first=A param-second=B" run
Buildfile: /var/lib/jenkins03/workspace/akrasnyansky/ManyParams/ant-param-test/build.xml
run:
[echo] first Param: first
[echo] second Param: second
BUILD SUCCESSFUL
Total time: 0 seconds
[ant-param-test] $ /bin/sh -xe /tmp/hudson4427994032931026249.sh
+ rm build.xml
+ echo
+ echo 'STEP 4: Pass parameters with separated by dots in names'
STEP 4: Pass parameters with separated by dots in names
+ cat
/tmp/hudson4427994032931026249.sh: line 5: <?xml version="1.0"?>
<!--DOCTYPE project PUBLIC "-//ANT//DTD project//EN" "/usr/src/ant/project.dtd"-->
<project name="many-params" default="run">
<target name="run">
<echo message="first Param: ${param.first}"/>
<echo message="second Param: ${param.second}"/>
</target>
</project>
: bad substitution
Build step 'Execute shell' marked build as failure
Started calculate disk usage of build
Finished Calculation of disk usage of build in 0 seconds
Started calculate disk usage of workspace
Finished Calculation of disk usage of workspace in 0 seconds
Finished: FAILURE
所以出现这种情况很多问题:
- 为什么STEP 1中根本没有打印任何值?
- 如何在第2步中使用字符串值“第一”和“第二”而不是“A”和“B”?
- 为什么在第3步格式不正确的行“-Dparam-first = A param-second = B”(通过将两个参数放在Jenkins步骤设置中的同一行上产生:param-first = a param-second = B)以及步骤2中的“-Dparam-first = A -Dparam-second = B”?我应该在这里失败,就像我在shell中运行一样。
- 步骤4中的点有什么问题?为什么它会在这里产生一个错误的替代错误?
非常感谢您对这些问题的任何建议。
谢谢!即使重写了build.xml,转义美元符号也彻底解决了我的问题。我觉得自己像个白痴一样没有想出来! –
不要那么想。直到我运行你的配置并查看了生成的'build.xml'文件,我才发现它自己 – Slav