I was following a tutorial with Hibernate and one of the tools required for it is Ant. Most Java developers are aware (they should be) that most Java IDE they use ships with Ant including Eclipse and Netbeans. Ironically, as we are aware of this very few people care about really knowing these greate tools(ant, maven, etc.). And so, as every C/C++ developer should be aware or be knowledgeable of using make, I also took a stand that as a Java developer therefore I shold now Ant(or any similar tool for Java).
Along my way in the tutorial I mentioned, I dunno if it was the author's mistake but I was instructed to replace the batch files I built which looked like this BTW
1 2 3 4 | javac -classpath .\lib\hibernate3.jar -d bin src\wp\hibernate\test\*.java copy /Y src\hibernate.cfg.xml bin copy /Y src\wp\hibernate\test\*.xml bin\wp\hibernate\test copy /Y src\log4j.properties bin |
with an ant script. Here is an example but more things were added since I stopped using batch files
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | <project name="hibernate-test" default="compile">
<property name="sourcedir" value="${basedir}/src"/> <property name="targetdir" value="${basedir}/bin"/> <property name="libdir" value="${basedir}/lib"/> <property name="configdir" value="${basedir}/config"/> <property name="wardir" value="${basedir}/war"/> <property name="viewsdir" value="${basedir}/views"/> <property name="static-htmldir" value="${basedir}/static-web"/>
<target name="compile" depends="copy-resources">
<javac srcdir="${sourcedir}" destdir="${targetdir}" debug="on"> <classpath>
<fileset dir="${libdir}"> <include name="*.jar"/> </fileset>
</classpath> </javac>
</target>
<target name="copy-resources"> <copy todir="${targetdir}"> <fileset dir="${sourcedir}"> <exclude name="**/*.java"/> </fileset>
</copy>
</target>
<target name="setup-war-structure" depends="compile"> <mkdir dir="${wardir}"/> <mkdir dir="${wardir}/WEB-INF"/> <mkdir dir="${wardir}/WEB-INF/classes"/> <mkdir dir="${wardir}/WEB-INF/lib"/> <mkdir dir="${wardir}/WEB-INF/views"/>
<copy todir="${wardir}/WEB-INF/classes"> <fileset dir="${targetdir}"/> </copy>
<copy todir="${wardir}/WEB-INF/lib"> <fileset dir="${libdir}"/> </copy>
<copy todir="${wardir}/WEB-INF/views"> <fileset dir="${viewsdir}"/> </copy>
<copy todir="${wardir}/WEB-INF/"> <fileset dir="${configdir}"/> </copy>
<copy todir="${wardir}"> <fileset dir="${static-htmldir}"/> </copy>
</target>
</project> |
However, I had problems with the above script(I prefer to call it script). The setup-war-structure was not executing. I atleast had to sleep to figure out the problem.
As we can see, Ant executes a "chain of command". Atleast from what I understood. From the very first target you specified using the project tag's "default" attribute, it would then execute that specific target and all its dependencies.
From the above example, I instructed Ant to execute compile, but compile is dependent to the copy-resources target, thus it would execute copy-resources first before compile.
The fix I did was simple, I just changed the project's default attribute to "setup-war-structure". Therefore, setup-war-structure and all of its dependencies would be executed. In this case, it would execute in this order
copy-resources
compile (dependent to above)
setup-war-structure (dependent to above)
Here's the working build.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | <project name="hibernate-test" default="setup-war-structure">
<property name="sourcedir" value="${basedir}/src"/> <property name="targetdir" value="${basedir}/bin"/> <property name="libdir" value="${basedir}/lib"/> <property name="configdir" value="${basedir}/config"/> <property name="wardir" value="${basedir}/war"/> <property name="viewsdir" value="${basedir}/views"/> <property name="static-htmldir" value="${basedir}/static-web"/>
<target name="compile" depends="copy-resources">
<javac srcdir="${sourcedir}" destdir="${targetdir}" debug="on"> <classpath>
<fileset dir="${libdir}"> <include name="*.jar"/> </fileset>
</classpath> </javac>
</target>
<target name="copy-resources"> <copy todir="${targetdir}"> <fileset dir="${sourcedir}"> <exclude name="**/*.java"/> </fileset>
</copy>
</target>
<target name="setup-war-structure" depends="compile"> <mkdir dir="${wardir}"/> <mkdir dir="${wardir}/WEB-INF"/> <mkdir dir="${wardir}/WEB-INF/classes"/> <mkdir dir="${wardir}/WEB-INF/lib"/> <mkdir dir="${wardir}/WEB-INF/views"/>
<copy todir="${wardir}/WEB-INF/classes"> <fileset dir="${targetdir}"/> </copy>
<copy todir="${wardir}/WEB-INF/lib"> <fileset dir="${libdir}"/> </copy>
<copy todir="${wardir}/WEB-INF/views"> <fileset dir="${viewsdir}"/> </copy>
<copy todir="${wardir}/WEB-INF/"> <fileset dir="${configdir}"/> </copy>
<copy todir="${wardir}"> <fileset dir="${static-htmldir}"/> </copy>
</target>
</project> |
Hmmm... Now I go to finish my reading. :)