DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Are you still fond of your ANT?

Are you still fond of your ANT?

Alex Staveley user avatar by
Alex Staveley
·
Dec. 24, 11 · Java Zone · Interview
Like (0)
Save
Tweet
5.24K Views

Join the DZone community and get the full member experience.

Join For Free

Ok you haven't caught the Maven bug and you're still using ANT. Here's two very simple Ant tips for you...

Tip 1: echoproperties

You're trying to debug things, you'd like to know details of the Ant and the JVM you're using. You'd also like to know details of the properties you have defined. You want this all quickly. Well look no further than Ant's built in target: echoproperties.

Wrap this around your own target so you can call it from the command line and that's it. I usually wrap it around a target called debug.

So I'd have something like this:

<target name="debug">
 <echoproperties/>
</target>


A sample output would be:
C:\Users\Alex\workspaces\staveley\sudoku>ant debug
Buildfile: C:\Users\Alex\workspaces\staveley\sudoku\build.xml
 
debug:
[echoproperties] #Ant properties
[echoproperties] #Fri Jun 17 20:18:35 BST 2011
[echoproperties] ant.core.lib=C\:\\tools\\buildtools\\ant\\apache-ant-1.8.0\\lib\\ant.jar
[echoproperties] ant.file=C\:\\Users\\Alex\\workspaces\\staveley\\sudoku\\build.xml
[echoproperties] ant.file.sudoku=C\:\\Users\\Alex\\workspaces\\staveley\\sudoku\\build.xml
[echoproperties] ant.file.type=file
[echoproperties] ant.file.type.sudoku=file
[echoproperties] ant.home=C\:\\tools\\buildtools\\ant\\apache-ant-1.8.0
[echoproperties] ant.java.version=1.6
[echoproperties] ant.library.dir=C\:\\tools\\buildtools\\ant\\apache-ant-1.8.0\\lib
[echoproperties] ant.project.default-target=help
[echoproperties] ant.project.invoked-targets=debug
[echoproperties] ant.project.name=sudoku
[echoproperties] ant.version=Apache Ant version 1.8.0 compiled on February 1 2010
[echoproperties] awt.toolkit=sun.awt.windows.WToolkit
[echoproperties] basedir=C\:\\Users\\Alex\\workspaces\\staveley\\sudoku
[echoproperties] build.classes=C\:\\Users\\Alex\\workspaces\\staveley\\sudoku\\build\\classes
[echoproperties] build.dir=C\:\\Users\\Alex\\workspaces\\staveley\\sudoku\\build
[echoproperties] docs.dir=C\:\\Users\\Alex\\workspaces\\staveley\\sudoku\\docs
[echoproperties] file.encoding=Cp1252
[echoproperties] file.encoding.pkg=sun.io
...




Tip 2: Those friggin' classpaths.

You're not a java developer unless you've spent some hours in your life banging your head of the wall because your classpath was incorrect. This isn't just a beginner's problem; it can even catch season pro's out. Especially when a class or set of classes is available from multiple jars. For example, you can easily use whatever
StAX implementation you want but if you forget to put the one you want on your classpath, your runtime will use the reference implementation from the JDK. So double check your classpath before you go around boasting of the performance improvements you're getting with Woodstox!

Right, so you want a target that will echo your classpaths so you can see them how ANT sees them. Well, in ANT you can't just echo paths. But, you can make property representations of paths and then just echo them.

So let's say you set up your properties, directories and classpaths as something like
<project name="alex" basedir="." default="help">
 
 <property name="build.dir" location="build"/>
 <property name="build.classes" location="${build.dir}/classes"/>
 <property name="src.dir"   location="src"/>
 <property name="tests.dir" location="test"/>
 <property name="lib.dir"   location="lib"/>
 <property name="docs.dir"   location="docs"/>
 <property name="junit.jar" location="${lib.dir}/junit-4.8.2.jar"/>
 <property name="test.class" value="com.staveley.sudoku.AllTests"/>
  
 <path id="compile.classpath">
  <pathelement location="${build.classes}"/>
 </path>
  
 <path id="test.classpath">
  <path refid="compile.classpath"/>
  <pathelement location="${junit.jar}"/>
 </path>
  
 <path id="junit.task.classpath">
  <fileset dir="${lib.dir}">
   <include name="ant-junit.jar"/>
  </fileset>
 </path>


All you do is create a target, which defines properties that represent these paths and
then you echo these properties.
<!--  Outputs classpaths to console. Useful for debugging etc. -->
<target name="echo.classpaths">
 <property name="compile.classpath.string" refid="compile.classpath"/>
 <echo message="compile.classpath= ${compile.classpath.string}"/>
  
 <property name="test.classpath.string" refid="test.classpath"/>
 <echo message="test.classpath= ${test.classpath.string}"/>
  
 <property name="junit.task.classpath.string" refid="junit.task.classpath"/>
 <echo message="junit.classpath= ${junit.task.classpath.string}"/>
</target>


Sample output:
C:\Users\Alex\workspaces\staveley\sudoku>ant echo.classpaths
 
echo.classpaths:
     [echo] compile.classpath= C:\Users\Alex\workspaces\staveley\sudoku\build\classes
     [echo] test.classpath= C:\Users\Alex\workspaces\staveley\sudoku\build\classes;C:\Users\Alex\workspaces\staveley\sudoku\lib\junit-
 
4.8.2.jar
     [echo] junit.classpath= C:\Users\Alex\workspaces\staveley\sudoku\lib\ant-junit.jar
 
BUILD SUCCESSFUL


Notice the way I have defined the properties locally to the target. This is because they are only relevant to the target echo.classpath. There is no need to make the global. Always try to encapsulate - even in Ant.

References:

1. http://ant.apache.org/manual/Tasks/echoproperties.html

 

From http://dublintech.blogspot.com/2011/06/are-you-still-fond-of-your-ant.html

Property (programming) Echo (command) Reference implementation Classpath (Java) Java (programming language) Debug (command) Implementation Java Development Kit Directory Command (computing)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Exhaustive JUNIT5 Testing with Combinations, Permutations, and Products
  • What Developers Need to Know About Table Partition Pruning
  • NativeScript vs. Flutter: A Comparison for Tech Businesses and Entrepreneurs
  • How Does the Internet Speed Test Work?

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo