top of page

How do I pass parameters from Jenkins to Ant scripts?

Well the integration of any CI tool with ANT is fairly common in the industry. It's a part of DevOps. Here we are working on two things Jenkins and ANT. The Requirement of this technique is very important because it will set a particular value in your ANT build file without doing hardcoded you can pass whatever value on the run time and your ANT build file will stay remain as it is.

Jenkins offers a simple way to set up a continuous integration or continuous delivery (CI/CD) environment for almost any combination of languages and source code repositories using pipelines, as well as automating other routine development tasks. While Jenkins doesn’t eliminate the need to create scripts for individual steps, it does give you a faster and more robust way to integrate your entire chain of build, test, and deployment tools than you can easily build yourself.


Apache Ant (“Another Neat Tool”) is a Java library used for automating build processes for Java applications. Additionally, Ant can be used for building non-Java applications. It was initially part of the Apache Tomcat codebase and was released as a standalone project in 2000.

In many aspects, Ant is very similar to Make, and it's simple enough so anyone can start using it without any particular prerequisites. Ant build files are written in XML, and by convention, they're called build.xml.

Different phases of a build process are called “targets”.

Here is the sample of the ant build file.

<project>
    <target name="clean">
        <delete dir="classes" />
     </target>
     
     <target name="compile"depends="clean">
         <mkdir dir="classes" />
         <javac srcdir="src"destdir="classes" />
      </target>
      
      <target name="jar"depends="compile">
          <mkdir dir="jar" /> 
          <jar destfile="jar/HelloWorld.jar"basedir="classes">                                                                                                                                            
              <manifest>
                  <attribute name="Main- 
                    Class"value="antExample.HelloWorld" />        
               </manifest>
           </jar>
       </target>
       
       <target name="run"depends="jar">
           <java jar="jar/HelloWorld.jar"fork="true" />                            
       </target>
</project>


Now let's say you in the above ant build you want this fork value to be false or true according to your scenario and you don't want to hardcode so for this we can pass this value on our run time from Jenkins.

<javajar="jar/HelloWorld.jar"fork="true" />


To do this we have to add a property tag in our ANT build file. Now a property tag will look like this:-

<project name="MyProject">
    <property name="abc" value="src"/>
</project>

The <property> tag should directly comes under the <project>. It won't go inside under the target or any other tags. Also, the property value is up to you, if you want you can give a value for a default run so when you will not anything on runtime then that default value works. I have passed "src" here.


You can give whatever property name you want I have given "abc". Now in the fork value, we need to pass this "abc" as a variable so it will look like this:-

<javajar="jar/HelloWorld.jar"fork="${abc}" />


Now that's all customization you needed in ant build file of yours.


Let's go to Jenkins and add a build step in a freestyle


project.

  1. Give your build file path

  2. If you have multiple targets then give your target name

  3. In properties just give abc="your value" in my case I have given true.


Now in the case of pipeline script, you have to write some line to invoke your ANT script.

which is this:-

 ant -f build.xml

Now you have to modify it a little bit like this:-

ant -Dabc="true" -f build.xml 

In Jenkins, that abc=true is also passed like this but that formatting is done by Jenkins only.


Thank You...


Hope you learn something from it, Please share it.....

And comment down below or contact me If you have any issues.

Recent Posts

See All
bottom of page