简体   繁体   English

无法从eclipse项目构建路径中的apache ivy依赖项导入类

[英]can not import class from apache ivy dependencies in eclipse project build path

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 嗨,我是apache ant和ivy的新手。我最近知道ant不支持依赖管理。所以我听说了ant的依赖管理器IVY。现在的问题是,我已将ivy依赖添加到ivy.xml文件中。

<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. 这是秋千应用程序的jdatepicker。 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. 常春藤将依赖项下载到项目目录下的lib文件夹中。

My build.xml file 我的build.xml文件

<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???? 我做对了吗????

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.xml和build.xml中没有错误,并且ant run任务运行良好。 Ivy resolved jar file as it supposed to, and ant target included right directory in the classpath. 常春藤解析的jar文件,它应该,并且ant目标在类路径中包括正确的目录。 I think you just forget to import classes into HelloWorld.java 我认为您只是忘了将类导入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. 为了避免这些错误,您可以使用IDE(Eclipse,Netbeans,Idea w / e),它们都支持Ant和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. 如果已compile尝试回显classpath并使用带有类路径的javac src/HelloWorld.java -cp ./lib/jdatepicker-1.3.2.jar运行相同的ant命令。
  • If it's run try it the same way with java -jar ... 如果run ,请使用java -jar ...以相同的方式尝试java -jar ...

Ivy is a dependency management for ANT not eclipse. Ivy是ANT 而不是 Eclipse的依赖项管理。 If you want ivy to resolve Eclipse classpaths you need to install the ivy plugin (See ivyDE ) 如果您希望ivy解析Eclipse类路径,则需要安装ivy插件(请参阅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. 正如@ deathangel908指出的那样,您需要正确设置软件包声明并从第3方jars中导入类。

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

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();

    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM