0
votes

I'm new to ANT. I have the below project structure,

      Project
        module1
          build.xml
        module2
          build.xml
        lib

What happens is, I have configured build.xml in module 2 to build a JAR of module2 classes and put it in lib folder. Now, I'm calling the build.xml fo module2 from module1's build.xml. module1 requires the module2's JAR to compile. I can see the JAR in lib folder but still module 1 is not compiling. Can some one please help me? Here my build.xml files, module1 build.xml,

<?xml version="1.0" encoding="UTF-8"?>

<property name="app.name" value="module1" />
<property name="src.home" value="${basedir}/src/main/java" />
<property name="build.home" value="${basedir}/build" />
<property name="ear.home" value="${basedir}/ear" />
<property name="module2.home" value="../module2" />
<property name="external.lib.dir" value="../lib" />

    <path id="lib.path">
    <fileset dir="${external.lib.dir}">
        <include name="*.jar" />
    </fileset>
</path>

<path id="module2.path">
    <filelist dir="${module2.home}" />
</path>


<target name="clean">
    <delete dir="${build.home}" />
    <delete dir="${ear.home}" />

</target>
<target name="prepare" depends ="clean"
    description="Create build dirs and copy static files to work dir">
    <mkdir dir="${build.home}" />
    <mkdir dir="${build.home}/classes" />
</target>


<target name="compile" depends="prepare"
    description="Compile Java sources and copy to build/classes dir">

    <javac includeantruntime="false" srcdir="${src.home}" destdir="${build.home}/classes">
        <classpath refid="lib.path" />
    </javac>
    <copy todir="${build.home}/classes">
        <fileset dir="${src.home}" excludes="**/*.java" />
    </copy>
</target>
<target name="includeJar">
    <subant target="buildJar">
        <fileset dir="../module2/" includes="build.xml" />
    </subant>
</target>

<target name="buildEar" depends="includeJar,compile">
            <jar destfile="${basedir}/ear/${app.name}.ear" basedir="${build.home}" />
</target>

And here is the module2 build.xml,

<?xml version="1.0" encoding="UTF-8"?>
<project name="module2">    
<property name="app.name"      value="module2"/>
<property name="src.home"      value="${basedir}/src/main/java"/>
<property name="build.home" value="${basedir}/build"/>
<echo>
    module2 build.xml
</echo>
<target name="clean">
    <delete dir="${build.home}"/>
</target>
<target name="init" depends="clean"
          description="Create build dirs and copy static files to work dir">

    <mkdir  dir="${build.home}"/>
    <mkdir  dir="${build.home}/classes"/>      
  </target>


<target name="compile" depends="init"
          description="Compile Java sources and copy to build/classes dir">
    <javac includeantruntime="false" srcdir="${src.home}"
          destdir="${build.home}/classes">

    </javac>
    <copy  todir="${build.home}/classes">
      <fileset dir="${src.home}" excludes="**/*.java"/>
    </copy>
  </target>

<target name="buildJar" depends="compile">
    <echo>Inside buildJar of module2
            </echo>
    <jar destfile="../lib/${app.name}.jar"
         basedir="${build.home}"
         />
  </target>

2
Please post build.xml files to have a better view of what could be happenningCristian Meneses
Edited my post to contain the build.xml filespaary
@Mark - Its working now. Posted the changes below. Thank you :)paary

2 Answers

0
votes

It would appear module1 is not compiling because the jar created by module2 is not being built.

You're calling the subant task, without specifying target:

<subant target="buildJar">
    <fileset dir="../module2/" includes="build.xml" />
</subant>

The module1 build file does not have a default target specified:

<?xml version="1.0" encoding="UTF-8"?>
<project name="module2">    
..
..

My recommended solution is to specify a default target as follows:

<project name="module2" default="buildJar">    

Aside

Multi-module build support and dependency management are features baked into more modern Java build tools. If you're trying to structure a large project in ANT, consider learning how the ivy plugin works.

0
votes

Solved the problem. It was a silly mistake that I overlooked. Here is the changes that I made, module2's build.xml, 1. I made the following changes, Changed the basedir of jar from basedir="${build.home}" to basedir="${build.home}/classes" module1's build.xml. 2. Made similar change as mentined above to buildEar target. I'm able to generate an EAR successfully :)

Thank you Mark for your time:)