Question

I can get Eclipse to export a standalone JAR file which works fine. But when I try to use my build.xml file with Ant I get about 100 errors because a bunch of references can not be resolved.

Here is my build.xml

  <!-- Deletes the existing build, docs and dist directory-->
  <target name="clean">
    <delete dir="${build.dir}" />
    <delete dir="${docs.dir}" />
    <delete dir="${dist.dir}" />
  </target>

  <!-- Creates the  build, docs and dist directory-->
  <target name="makedir">
    <mkdir dir="${build.dir}" />
    <mkdir dir="${docs.dir}" />
    <mkdir dir="${dist.dir}" />
  </target>

      <!-- ================================= 
            target: resolve              
           ================================= -->
      <target name="resolve" description="--> retrieve dependencies with ivy">
          <ivy:retrieve />
      </target>

  <!-- Compiles the java code (including the usage of library for JUnit -->
  <target name="compile" depends="resolve,clean, makedir">
    <javac destdir="${build.dir}" includeantruntime="false">
        <src path="${src.dir}"/>
        <src path="${lib.dir}"/>
    </javac>

  </target>

  <!-- Creates Javadoc -->
  <target name="docs" depends="compile">
    <javadoc packagenames="src" sourcepath="${src.dir}" destdir="${docs.dir}">
      <!-- Define which files / directory should get included, we include all -->
       <fileset dir="${src.dir}">
                <include name="**" />
           </fileset>
    </javadoc>
  </target>

  <!--Creates the deployable jar file  -->
  <target name="jar" depends="compile">
    <jar destfile="${dist.dir}\hcm.jar" basedir="${build.dir}">
      <manifest>
        <attribute name="Main-Class" value="org.heromanager.MainWindow" />
      </manifest>
    </jar>
  </target>

  <target name="main" depends="compile, jar, docs">
    <description>Main target</description>
  </target>

</project> 

Here is the ivy.xml file

<ivy-module version="2.0">
    <info organisation="org.herocombatmanager" module="mainwindow"/>
    <dependencies>
        <dependency org="org.apache.commons" name="commons-lang3" rev="3.0"/>
        <dependency org="com.jgoodies" name="looks" rev="2.2.2"/>
        <dependency org="com.jgoodies" name="forms" rev="1.2.1"/>
    </dependencies>
</ivy-module>

And here are a few of the errors :-)

[javac] H:\Development\Workspace\HeroCombatManager\src\org\herocombatmanager\XMLParser.java:3: package org.apache.commons.lang3 does not exist
[javac] import org.apache.commons.lang3.StringUtils;
[javac]                                ^
[javac] H:\Development\Workspace\HeroCombatManager\src\org\herocombatmanager\AttackAdvantagesPanel.java:10: package com.jgoodies.forms.factories does not exist
[javac] import com.jgoodies.forms.factories.FormFactory;
[javac]                                    ^
[javac] H:\Development\Workspace\HeroCombatManager\src\org\herocombatmanager\AttackAdvantagesPanel.java:11: package com.jgoodies.forms.layout does not exist
[javac] import com.jgoodies.forms.layout.*;
[javac] ^
...
[javac] H:\Development\Workspace\HeroCombatManager\src\org\herocombatmanager\AttackAdvantagesPanel.java:40: cannot find symbol
[javac] symbol  : class FormLayout
[javac] location: class org.herocombatmanager.AttackAdvantagesPanel
[javac]         this.setLayout(new FormLayout(new ColumnSpec[] {
[javac]                            ^
[javac] H:\Development\Workspace\HeroCombatManager\src\org\herocombatmanager\AttackAdvantagesPanel.java:40: cannot find symbol
[javac] symbol  : class ColumnSpec
[javac] location: class org.herocombatmanager.AttackAdvantagesPanel
[javac]         this.setLayout(new FormLayout(new ColumnSpec[] {
[javac]                                           ^

Thank you all in advance..

Was it helpful?

Solution

As @oers has pointed out, you cannot add the jar files in the "lib" directory as source files, the javac task doesn't work that way.

What you need to add the jars resolved by ivy to an ANT classpath. The simplest way to do this is to use the ivy cachepath task as follows:

  <target name="resolve" description="--> retrieve dependencies with ivy">
     <ivy:resolve/>
     <ivy:cachepath pathid="compile.path"/>
  </target>

  <target name="compile" depends="resolve,clean, makedir">
    <javac destdir="${build.dir}" includeantruntime="false" classpathref="compile.path">
        <src path="${src.dir}"/>
    </javac>
  </target>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top