1
votes

I have a question, I have an build.properties where I have a property test=true; and the ant target should be called only in case is test is true. I want to have that value as a default one. Is it possible somehow to change the value in Jenkins? I tried to set test=false, but seems that there was no effect. Some suggestions?

3
Have you tried simply putting test=true in the properties section of the "invoke Ant" build step in your Jenkins job? Properties set there are treated the same way as properties passed to Ant via the command line, and thus should override anything set in your build script or properties files.CAustin

3 Answers

1
votes

In this scenario you have to modify Ant script similarly like below, then it will work as you expected. If not try similar kind of logic to set default and dynamic values for ant. Then if you pass value from Jenkins if it is -Dtest=true otherwise by default it will assign value to false

<condition property="test" value="${test}" else="false">
    <isset property="${test}" />
</condition>
1
votes

To have an Ant target execute only on certain condition

In your Ant build script, you want a target that only executes when a certain condition is met. It is easy to do using if="property" attribute of <target> tag, however that checks if property is set, not its value. And you are already setting the property to default test=true. So for your case, a little different method can be used.

<condition property="test.execute" value="${test}">
    <matches pattern="true" string="${test}"/>
</condition>

<target name="runtest" if="test.execute">
    <echo message="running tests"/>
</target>

This script checks the value of ${test}, and if that matches to text "true", it will set a value of ${test.execute} to value of ${test}. If not (i.e. anything other than "true"), then the property ${test.execute} remains unset.

Finally, the target runtest will only execute if property ${test.execute} is set.

Note: this will only work when that property remains unset. Even having <property name="test.execute" value=""/> in the build file or properties will break this.

To pass a variable through Jenkins to Ant:

  • Select Invoke Ant build step
  • Click Advanced...
  • Under Targets, enter your usual targets (or leave empty for default)
  • Under Build File, specify the build file (or leave empty for default build.xml in root of $WORKSPACE)
  • Under Properties, enter your parameters in the format param=value. This is the same as using -Dparam=value on command line. Note that you do not need -D when specifying properties in this field in Jenkins.
  • So, enter test=false
1
votes

With Ant 1.9.2 and up (It suppose to work with Ant 1.9.1, but I have problems with it), you can now use the if:true parameter in most tasks:

<project default="test" xmlns:if="ant:if"> <!-- Note xmlns in the entity "project" -->
    <property name="run.this" value="true"/>
    <target name="test">
        <echo if:true="${run.this}">Run test target</echo>
    </target>
</project>

If I run:

$ ant
Buildfile: /Users/david/build.xml

test:
      [echo] Run test target

BUILD SUCCESSFUL
Total time: 0 seconds

If I run:

ant -Drun.this=false  # Sets property run.this to "false"
test:

BUILD SUCCESSFUL
Total time: 0 seconds

Note that the if:true="${run.this} prevents echo from executing when run.this is false.