If-Else in Ant

, Comments Off

When I need to write quick script to perform a simple task, I opt to use Ant instead of a shell script. Unfortunately, the Ant core tasks doesn’t provide any flow logic like the everyday if-else statements.

Ant-Contrib provides a collection of useful tasks missing in the Ant core.

Installing Ant-Contrib

There are 2 methods to install ant-contrib:

Copy ant-contrib-1.0b3.jar to the lib directory of your Ant installation. If you want to use one of the tasks in your own project, add the lines

<taskdef resource="net/sf/antcontrib/antcontrib.properties"/>

Keep ant-contrib-1.0b3.jar in a separate location. You now have to tell Ant explicitly where to find it e.g. a lib directory relative to the build.xml

<taskdef resource="net/sf/antcontrib/antcontrib.properties" classpath="lib/ant-contrib-1.0b3.jar"/>

Example Using If-Else Task

<if>
  <equals arg1="${status}" arg2="OFFLINE"/>
  <then>
    [...]
  </then>
  <else>
    [...]
  </else>
  </if>

Sending Email with Ant

, Comments Off

Sometimes it’s useful for an Ant script to send out an email. This can be easily done using the mail task which is part of the Core Tasks in Ant. Before using the mail task, the JavaBean Activiation Framework jar file (activation.jar), and JavaMail jar file (mail.jar) needs to be copied into the Ant lib directory. Here is a mail task example which sends an email using a GMail account.

<target name="sendmail">
  <mail
    tolist="nobody@gmail.com"
    from="nobody@gmail.com"
    subject="Email subject"
    mailhost="smtp.gmail.com"
    mailport="465"
    ssl="true"
    user="nobody@gmail.com"
    password="password">
    <message>Example email sent by Ant Mail task.</message>
  </mail>
</target>

Troubleshooting Ant mail task in Ubuntu

Failed to initialise MIME mail: org.apache.tools.ant.taskdefs.email.MimeMailer not found in java.net.URLClassLoader

If you downloaded Ant from the Ubuntu packages repository, not all the core Ant tasks are available. Download and extract Ant from the Apache Ant website.

$ wget http://www.apache.org/dist/ant/binaries/apache-ant-1.7.0-bin.tar.gz
$ tar -zxvf apache-ant-1.7.0-bin.tar.gz

Copy the ant-javamail.jar into the lib directory of the Ubuntu Ant installation

$ cp ./apache-ant-1.7.0/lib/ant-javamail.jar /usr/share/ant/lib

Failed to send email: could not instantiate ssl security provider, check that you have JSSE in your classpath

If the ssl attribute is set to true, then the Java Secure Socket Extension (jsse.jar) jar file is required, which is already part of J2SE 1.4.x and later. Ubuntu uses GNU Compiler for Java (gcj) as the default JVM which doesn’t have JSSE as the part of the distribution, therefore the jsse.jar file needs to be copied into the Ant lib directory. An alternative solution is to set the default JVM to use another Java distribution which already has JSSE bundled.

Some Useful Ant Tasks

, Comments Off

Copying a File to a Server Running SSH Daemon

  1. Download the jsch jar file from (http://www.jcraft.com/jsch/)
    > wget http://prdownloads.sourceforge.net/jsch/jsch-x.x.x.jar
  2. Copy the jar file into the lib directory of your ANT HOME
    > cp jsch-x.x.x.jar ${ANT_HOME}/lib
  3. Use the scp ant task in the build script
    <scp file="file.txt" todir="user:password@hostname:/remotedir"/>

Selecting Files by Size

The <size> tag in a FileSet will determine if the files should be included or not depending on the size limit specified.

<fileset dir="${jar.path}">
    <patternset>
        <include name="**/*.jar"/>
    </patternset>
    <size value="0" units="k" when="more"/>
</fileset>

value – is the size of the value to be tested
units – unit to be tested against: k, M, G
when – type of comparison to be used: less, equal, more

Execute Target Based on Conditional Logic

Ant doesn’t support true conditional logic, such as if/then/else. However, you can execute targets depending on the state of properties. For example the target executes if the dummy property is set:

<target name="run" if="dummy"/>

If the property is not set, the target is ignored. You can also specify that a target should execute unless a property is set:

<target name="run" unless="dummy"/>

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in