Ant Tips

From EggeWiki

Forked Java VM exited abnormally

This is a favorite error of mine. The log files will show you nothing usually. The best thing to do is to temporarily not fork the JVM. This will usually give you a stack trace which is fixable. Digging into my issue a little further, I found I was using JUnit 3.8 when forked, and 3.8.2 inside Eclipse.

<geshi lang="DOS"> C:\dev>ant junit Buildfile: build.xml

junit:

   [junit] Tests run: 4, Failures: 0, Errors: 0, Time elapsed: 0.078 sec
   [junit] Testsuite: ioc.SingletonFactoryUnitTest
   [junit] Tests run: 4, Failures: 0, Errors: 0, Time elapsed: 0.078 sec
   [junit]
   [junit] java.io.FileNotFoundException: C:\dev\junitvmwatcher1409925816.pr

operties (The system cannot find the file specified)

   [junit]     at java.io.FileInputStream.open(Native Method)
   [junit]     at java.io.FileInputStream.<init>(FileInputStream.java:106)
   [junit]     at java.io.FileReader.<init>(FileReader.java:55)
   [junit]     at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.executeAsForked(JUnitTask.java:1028)
   [junit]     at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.execute(JUnitTask.java:817)
   [junit]     at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.execute(JUnitTask.java:780)
   [junit]     at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:288)
   [junit]     at sun.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
   [junit]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   [junit]     at java.lang.reflect.Method.invoke(Method.java:585)
   [junit]     at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:105)
   [junit]     at org.apache.tools.ant.Task.perform(Task.java:348)
   [junit]     at org.apache.tools.ant.Target.execute(Target.java:357)
   [junit]     at org.apache.tools.ant.Target.performTasks(Target.java:385)
   [junit]     at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1329)
   [junit]     at org.apache.tools.ant.Project.executeTarget(Project.java:1298)
   [junit]     at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
   [junit]     at org.apache.tools.ant.Project.executeTargets(Project.java:1181)
   [junit]     at org.apache.tools.ant.Main.runBuild(Main.java:698)
   [junit]     at org.apache.tools.ant.Main.startAnt(Main.java:199)
   [junit]     at org.apache.tools.ant.launch.Launcher.run(Launcher.java:257)
   [junit]     at org.apache.tools.ant.launch.Launcher.main(Launcher.java:104)
   [junit] ioc.SingletonFactoryUnitTest
   [junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec

BUILD FAILED </geshi>

Launch ant tasks in multiple threads

I've tried to use the Ant parallel task to launch ant tasks in multiple threads, but never with successful results. It always seems to mostly work, but then I'll find half written files, or other strangeness. For example, it should be a no brainer to run four or more JUnit tasks concurrently. Maybe this is fixed in Ant 1.7, as I haven't tried it lately.

Use ant to create a properties java file which can be compiled

<geshi lang="xml">

   <target name="build-properties">
      <copy todir="src">
           <fileset dir="src">
               <include name="**/*.hava"/>
           </fileset>
          <globmapper from="*.hava" to="*.java"/>
          <filterset>
              <filtersfile file="src/resources/messages_en_AU_company.properties" />
          </filterset>
       </copy>
   </target>

</geshi>


Ant is great, but the XML gets to suck after a while.

  • If I knew then what I knew now, I would have tried using a real scripting language, such as ~JavaScript via the Rhino component or Python via JPython, with bindings to Java objects which implemented the functionality expressed in todays tasks. Then, there would be a first class way to express logic and we wouldn't be stuck with XML as a format that is too bulky for the way that people really want to use the tool.

Ant is giving you this error when using a fileset:

java.lang.NoClassDefFoundError: org/apache/oro/text/regex/MalformedPatternException

You need to download the Jararta ORO libarary. Install the jar in you ant/lib directory.

! You get some stack trace like this:

 java.lang.NumberFormatException: multiple points
  at java.lang.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1067)
  at java.lang.Double.parseDouble(Double.java:220)
  at java.text.DigitList.getDouble(DigitList.java:127)
  at java.text.DecimalFormat.parse(DecimalFormat.java:1070)
  at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:1705)
  at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1156)
  at java.text.DateFormat.parse(DateFormat.java:333)

This might be caused by your ~SimpleDateFormat object being used concurrently. The spec says you can't:

  Date formats are not synchronized. It is recommended to create separate format instances for
  each thread. If multiple threads access a format concurrently, it must be synchronized externally.

How to require an environment variable in an Ant file

<property environment="env"/><BR>
<fail message="Provide MY_VAR" unless="env.MY_VAR"/>

Remove Ant Configuration Dialog from Eclipse

In Eclipse 3.0 you can launch Ant tasks. This is a great feature. If you don't want to run with the default settings, you can open the configuration settings and add environment variables and add flags. Sometimes I like to add a "-verbose" flag. The problem is after this you are stuck with the configuration dialog popping up each time you launch a target. To stop this, you must delete files in your Eclipse workspace settings. My files are in:

H:\eclipseworkspace\.metadata\.plugins\org.eclipse.debug.core\.launches

Then restart Eclipse, and you can now run Ant tasks simply by double clicking on them.