0
votes

Hi i'm new to apache ant and ivy.i recently known ant does not support dependency management.so i hear about IVY ,a dependency manager for ant.Now the problem is , i have added ivy dependency to ivy.xml file

<ivy-module version="2.0">
    <info organisation="com.mlen" module="testApp"/>
    <dependencies>    
    <dependency org="net.sourceforge.jdatepicker" name="jdatepicker" rev="1.3.2"/>
    </dependencies>
</ivy-module>

Which is a jdatepicker for swing application. Now the problem is when i try to access the dependency class, it does not import classes. ivy downloaded the dependency to lib folder under project dir.

My build.xml file

<project name="HelloWorld" 
      basedir="." 
      default="run">
<!--      xmlns:ivy="antlib:org.apache.ivy.ant">-->

    <property name="src.dir"     value="src"/>
    <property name="lib.dir"     value="lib"/>
    <property name="build.dir"   value="build"/>
    <property name="classes.dir" value="${build.dir}/classes"/>
    <property name="jar.dir"     value="${build.dir}/jar"/>
    <property name="main-class"  value="com.mlen.testApp.HelloWorld"/>

    <path id="classpath">
        <fileset dir="${lib.dir}" includes="**/*.jar"/>
    </path>

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

    <target name="resolve" xmlns:ivy="antlib:org.apache.ivy.ant">
        <ivy:retrieve/>
    </target>

    <target name="compile" depends="resolve">
        <mkdir dir="${classes.dir}"/>
        <javac includeantruntime="false" srcdir="${src.dir}" destdir="${classes.dir}" classpathref="classpath"/>
        <copy todir="${classes.dir}">
            <fileset dir="${src.dir}" excludes="**/*.java"/>
        </copy>
    </target>

    <target name="jar" depends="compile">
        <mkdir dir="${jar.dir}"/>
        <jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
            <manifest>
                <attribute name="Main-Class" value="${main-class}"/>
                <!--how can i get ivy to note what the class path should be?-->
                <attribute name="Class-Path" value="log4j-1.2.17.jar"/>
            </manifest>
        </jar>
    </target>

    <target name="run" depends="jar">
        <java fork="true" classname="${main-class}">
              <classpath>
                <path refid="classpath"/>
                <path location="${jar.dir}/${ant.project.name}.jar"/>
            </classpath>
        </java> 
    </target>

    <target name="clean-build" depends="clean,jar"/>
    <target name="main" depends="clean,run"/>
</project>

And finally my main class.

public class HelloWorld extends JFrame {

    public HelloWorld(){
        UtilDateModel model = new UtilDateModel();
        JDatePanelImpl datePanel = new JDatePanelImpl(model);
        JDatePickerImpl datePicker = new JDatePickerImpl(datePanel);

        add(datePicker);
        setSize(300, 200);
        setVisible(true);
    }




    public static void main(String[] args) {
     new HelloWorld();

    }

}

Why is not importing to class. am i doing it right????

2
Not directly related to your question, but there is a newer version of JDatePicker available which contains a number of bug fixes: search.maven.org/#browse%7C-1191578160juanheyns

2 Answers

0
votes

I created the same files structure as yours and there're no mistakes in ivy.xml and build.xml and ant run task worked fine. Ivy resolved jar file as it supposed to, and ant target included right directory in the classpath. I think you just forget to import classes into HelloWorld.java

import net.sourceforge.jdatepicker.impl.UtilDateModel;
//TODO import more

To avoid those mistakes you can use IDE (Eclipse, Netbeans, Idea w/e) all of them support Ant and Ivy.

If you didn't forget to import classes. You need to be more specific and find the stage where your ant target fails.

  • If it's compile try to echo classpath and run the same ant command with javac src/HelloWorld.java -cp ./lib/jdatepicker-1.3.2.jar with your classpath.
  • If it's run try it the same way with java -jar ...
0
votes

Ivy is a dependency management for ANT not eclipse. If you want ivy to resolve Eclipse classpaths you need to install the ivy plugin (See ivyDE)

And your example works for me as well. As @deathangel908 pointed out you need to set the package declaration properly and import the classes from the 3rd party jars.

├── build.xml
├── ivy.xml
└── src
    └── com
        └── mlen
            └── testApp
                └── HelloWorld.java

HelloWorld.java

package com.mlen.testApp;

import javax.swing.*;
import net.sourceforge.jdatepicker.impl.*;

public class HelloWorld extends JFrame {

    public HelloWorld(){
        UtilDateModel model = new UtilDateModel();
        JDatePanelImpl datePanel = new JDatePanelImpl(model);
        JDatePickerImpl datePicker = new JDatePickerImpl(datePanel);

        add(datePicker);
        setSize(300, 200);
        setVisible(true);
    }


    public static void main(String[] args) {
     new HelloWorld();

    }

}