Java code compilation

Author: Kasper B. Graversen and Jakob Jenkov, 04/12/2007
Keywords: ANT, Build script, Compile
Abstract: This guide shows you how to compile Java code in ANT and introduces the most commonly used settings for compilations of real-life sized projects.
subscribe to my RSS feed


Bookmark and Share


Java code compilation

ANT provides a straight forward way to compile Java code. The below task assumes the properties dir.src and dir.dst
<target name="compile">
    <delete dir="${dir.dst}" />
    <mkdir dir="${dir.dst}" />
    <javac
        srcdir="${dir.src}"
        destdir="${dir.dst}"
        source="5"
        target="5"
        optimize="true"
        debug="off" />
</target>
Often, however, you need a bit more fine-grained control over class paths and which files should be compiled. It is also a good idea to fork the javac process, to circumvent the memory settings the build script was executed with. Below shows you how
<target name="compile">
    <delete dir="${dir.dst}" />
    <mkdir dir="${dir.dst}" />
    <javac destdir="${webapp.path}/WEB-INF/classes"
           optimize="on"
           debug="on"
           verbose="${flag.java.verbose}"
           deprecation="true"
           failonerror="true"
           encoding="utf-8"

           fork="true"
           memoryInitialSize="100m"
           memoryMaximumSize="512m"

           srcdir="${webapp.path}/WEB-INF/src"
           source="1.5" >
      <classpath>
        <pathelement location="${webapp.path}/WEB-INF/classes"/>
        <fileset dir="${webapp.path}/WEB-INF/lib">
          <include name="*.jar"/>
        </fileset>
        <pathelement location="${tomcat.home}/common/classes"/>
        <fileset dir="${tomcat.home}/common/lib">
          <include name="*.jar"/>
        </fileset>
        <pathelement location="${tomcat.home}/shared/classes"/>
        <fileset dir="${tomcat.home}/shared/lib">
          <include name="*.jar"/>
        </fileset>

        <fileset dir="${webapp.path}/WEB-INF/classes">
          <include name="**/*.jar"/>
        </fileset>

     </classpath>
     <include name="**" />
     <exclude name="tags/**" />

    </javac>
</target>
alternatively, you may assemble all the references to jar files the project depends on under a name such as path.jars. You can have as many fileset definitions you want nested inside the path.
<path id="path.jars">
    <fileset dir="${lib.dir}">
       <include name="**/*.jar" />
    </fileset>
</path>

<javac fork="true" destdir="${workdir.dir}">
   <classpath refid="path.jars" />
</javac>
You should note that the javactasks supports a significant number of compiler flags (see compiler flags for a full list).

The most commonly used flags are to enable the compilation of deprecated classes, and to maximize the memory allocated to the compiler. You set the compiler flags using a combination of value and line to compilerarg.
<javac
    ...>
    <compilerarg value="-Xlint:-unchecked" />
    <compilerarg value="-Xlint:-deprecation" />
    <compilerarg line="-Xmaxerrs 30000" />
    <compilerarg line="-Xmaxwarns 30000" />
</javac>
Finally, you should be careful with using the target flag. If you set the target to 5 (that is Java 5 compatible) but compiling using a JDK 6 or later, you will be unable to use the class files. Although there are all sorts of extra flags to support this "legacy compatible compilation", I found it easiest to execute the ANT script within Eclipse and setting the JDK in there to a freshly installed JDK 5.



Comments

If you have any comments to this article, please drop me a mail at firstclassthoughts at gmail dot com please indicate if I can't publish whole or parts of your comment on the site.


If you like this site consider subscribing to my RSS feed or how about subscribing by Email.


Help spread the word

Share this post on your favorite social bookmarking sites:
If you enjoyed this article, found it thought provoking, educative or otherwise good, please link to this page from your page or social bookmarking page. If you have any texts you think are worth publishing on First Class Thoughts, don't hesitate to send me a mail! Quality always welcome.


Bookmark and Share


The most recent contributions
28/07/09 Magic in mathematics II Fun with the number cyclic numbers, and specifically with 142857 as it is the smallest of such numbers.
13/07/09 My top 8 time-saving Firefox shortcuts This article presents my favorite top 8 time-saving shortcuts in Firefox 3.0 and Firefox 3.5. Get to know these and you'll be saving a lot of time. They have been ordered by "the element of most surprise"
20/05/09 Board Game Jungle speed / Arriba Review of the cool game "Jungle Speed" aka. "Arriba".
16/05/09 Danish Twin words "Twin words" are words that not only have multiple meanings, they must be composed next to each other in meaningful sentences. This article explores the concept of twin words.
Nothing of interest? Try browsing the entire article archive...