Useful Ant build tags

Questions:

How to do following tasks in ant file?

  1. Make zip file.
  2. Run command.
  3. Copy files to remote machine.
  4. Run commands on Remote Linux machine.
  5. Open an input box and respond to input value.
  6. Make an ant call.

Answers:

1. Make zip file:

Following is the xml for making zip file in ant:

<zip destfile='${destination.folder}/zipName.zip'>
     <fileset dir= '${Source.folder}' />
</zip>

In here “destfile” is the name and location of the created zip file. Inside fileset tag the dir attricute is used to specify source folder form where all files will be zipped.

2. Run commands:

Here I will show you how to start tomcat in ant file to demonstrate how to run commands in ant. Following is the xml for this:

<exec dir='${tomcat.home}/bin' executable='cmd' os='Windows XP'>
     <arg line='/c startup.bat'/>
</exec>

Here “${tomcat.home}” is the path of the tomcat folder. The command is given in “<arg>” tag in “line” attribute.

Note: To run following commands you will need JSCH jar.

3. Copy files to remote machine:

If remote machine is in your LAN and you can access it directly as \\remote_machine\path then you can use simple copy command as follows:

<copy file='${source.folder.file} ' todir='\\remote\path'>
</copy>

To copy files on remote machine that supports SCP use following tag:

<scp file='${source.folder.file} ' todir='${remote.user}@${remote.host}:${remote.path.where.to.do.copy}'
       password='${remote.password}' trust='true'>
</scp>

In above both commands “file” is the source file which you want to copy with its path. And “todir” is the remote machine folder path where you want to copy the file.

4. Run commands on remote machine:

You can use following tag to execute commands on remote Linux machine.

<sshexec host='${remote.host}' username='${remote.username}' password='${remote.password}'
     command='${command.to.run}' trust='true' />

For example to start tomcat use following tag:

<sshexec host='${remote.host}' username='${remote.user}' password='${remote.password}'
         command='sh ${tomcat.home}/startup.sh' trust='true' />

5. Open an input box and respond to input value:

To open an input dialog use following tag:

<input message='Enter id: ' addproperty='my.id'>
</input>

Here “my.id” in “addproperty” is the variable name which holds input value. Now to check if user has denied to enter value in input:

<condition property='do.abort'>
     <equals arg1='n' arg2='${my.id}'/>
</condition>
<fail if='do.abort'>Build aborted by user.</fail>

And if user enters value and press OK then after you can refer to entered value as “${my.id}”.

6. Make an ant call:

<antcall target='targetName'>
</antcall>

Here “target” is the name of the target that will be executed.

Note: In above examples all values starting with “${”and ending with “}’’ are variables and you may have to put appropriate values in them to successfully run them.

Reference: Useful Ant build tags from our JCG partner Harsh Raval at the harryjoy blog.

Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You’ll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You’ll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now!  

Leave a Reply


six × 7 =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books