how to use Jenkins parameter in Ant's build.xml file - ant

I want Ant to replace a token in a file with the build number from Jenkins.
I have a project that contains build.xml file with several tasks including:
<replace file="${web.dir}\index.html" token="#build#" value="${build_num}"/>
the "value" should somehow be resolved to the build number in Jenkins.
I
In Jenkins, in build configureation -> ant -> advanced i set the following under properties:
first: build_num=$build , second: build_num=${build}
Neither worked , "$build" or "${build}" just appeared as the value instead of the token.
Anyone knows how to do it?
Thanks,
N

${BUILD_NUMBER} instead of ${BUILD} solved it

Related

Jenkins - How to access BUILD_NUMBER environment variable

Are Jenkins parameters case-sensitive? I have a parametrized build which needs an ant parameter named "build_parameter" to be set before the build. When I try to access the ${BUILD_NUMBER} set by Jenkins, I get the value set for the ant parameter. If the build parameters are not case sensitive, can anyone suggest me a work around to this issue? I cannot change the build parameter name as I will have to change my build scripts (which is not an option). Thanks!
To Answer your first question, Jenkins variables are case sensitive. However, if you are writing a windows batch script, they are case insensitive, because Windows doesn't care about the case.
Since you are not very clear about your setup, let's make the assumption that you are using an ant build step to fire up your ant task. Have a look at the Jenkins documentation (same page that Adarsh gave you, but different chapter) for an example on how to make Jenkins variables available to your ant task.
EDIT:
Hence, I will need to access the environmental variable ${BUILD_NUMBER} to construct the URL.
Why don't you use $BUILD_URL then? Isn't it available in the extended email plugin?
Assuming I am understanding your question and setup correctly,
If you're trying to use the build number in your script, you have two options:
1) When calling ant, use: ant -Dbuild_parameter=${BUILD_NUMBER}
2) Change your script so that:
<property environment="env" />
<property name="build_parameter" value="${env.BUILD_NUMBER}"/>
For Groovy script in the Jenkinsfile using the $BUILD_NUMBER it works.
Use $env:BUILD_NUMBER in PowerShell script

Gradle Ant Cannot add task ':myproject:test' as a task with that name already exists

I'm trying to Gradle-ize our build by using Gradle to execute the Ant build. I'm using the java plugin so I can set source/target and I'm using ant.importBuild 'build.xml'. When I execute Gradle, I get the error above. I understand that both Ant and Gradle have these targets/tasks in common: clean, jar, javadoc, test. One option is to change the Ant target names in build.xml, but I'm hoping there's an easier way as I have a lot of projects and build files. I found this "wrapper" solution (http://issues.gradle.org/browse/GRADLE-771), but this did not work for me. How can I solve this?
Your options are:
Do not apply the plugin to the same project that imports the Ant build.
Rename the conflicting targets in the Ant build script.
You can rename all the ant targets:
ant.importBuild('build.xml') { String oldTargetName ->
return 'ant_' + oldTargetName
}

How do i pass parameters from Jenkins to Ant scripts?

For some GUI testing I'm creating a Jenkins task for each GUI module to be tested.
Once created I'm using Ant to build these tests, but I'm not aware of how to actually pass parameters from Jenkins to Ant build file? Basically how do I do variable substitution in Ant?
I'm using the Sahi framework to test GUI components, so the flow goes like this...
Jenkins → Ant build script → Sahi file to execute
Can anyone please take a look at it?
"Using ant -Dname=value lets you define values for properties on the Ant command line." http://ant.apache.org/faq.html#passing-cli-args
To use a jenkins parameter as a variable when you call any use ${variablename}
https://wiki.jenkins-ci.org/display/JENKINS/Parameterized+Build
Click Advance under configure section of your job in jenkins and use "Properties" section to pass parameter value to Ant script.
e.g
jenkins.param=10
ant.prop=$jenkins.param where jenkins.param is the parameter defined in the jenkins job .
Now in your ant build script ,you can get the value by using ${ant.prop}.
From Jenkins to SAHI Pro via ANT.
In the ant target that you call from Jenkins, give the following within sahi tag.
<customfield key="variable_name" value=" variable _value"/>
Now, such values from Jenkins will be available in SAHI Pro through the ant target. To retrieve them in SAHI, you should set them in “CUSTOM_FIELDS” of testrunner file.
For example:
SET CUSTOM_FIELDS= -variable_name jenkinsToSahiVariable
Where -variable_name should be same key that you set in ant target. And second string will contain the value you set from Jenkins. To get this in a sahi file, use sahiSuite API like following.
$jenkinsValues = _suiteInfo();
$sahiVariable = $ jenkinsValues ["jenkinsToSahiVariable"];

Dynamically finding build files with <ant> task

I am trying to build subprojects from my main Ant build script..
The build files are located in
plugings/<pluginName>/build.xml
I want to do something effectively like
<ant antfile="plugins/*/build.xml" ...>
It should dynamically find build files in the plugin directory. Haven't been able to get it to work yet with filesets.. any tips?
Thanks in advance.
Solution: <subant> was the task I was looking for
These links show you a way of building sub-projects:
Sample Ant build file for multiple projects
Ant Tip 1: Write a master build file
Check this answer:
Generate Ant build file

Pass a command line argument to Ant in Hudson

I'm trying to pass a -lib argument to ant as part of an automated build using Hudson but can't see a way to do this. I could add the relevant libraries to the ant/lib folder but that would then mean the same version of the library necessarily being shared by all builds on that machine.
Any help much appreciated.
In your Hudson job configuration you can specify ant arguments such as -lib in the Targets field. See the help message that opens when you click the ? next to the Targets field.

Resources