我有一个在命令行上工作得很好的Ant build.xml
文件:编译,构建JAR,并且能够从JAR执行主方法。 build.xml
文件引用了分散在这里和那里的几个第三方库。在构建JAR时,脚本不会将所有第三方库包含到JAR本身中。相反,它将它们的路径放入JAR的清单中。这有助于保持我的JAR苗条和整齐。在Eclipse中使用Ant的类路径
我希望能够在Eclipse中编辑和调试我的项目,但我找不到一个简单的方法来做到这一点。我可以让我的项目使用Ant文件来构建项目,这似乎工作。然而,Eclipse是很难找到的第三方libaries,因此Eclipse的是有两个问题:
- 它显示(在文本编辑器)大量编译错误的,因为 很多类是不确定的,并且
- 它不能执行JAR。
我可以通过用手指定,在两个差的地方同时解决上述问题(即,经由Properties->Java Build Path->Libraries
构建路径,和经由Run Configurations->Classpath
执行类路径)中,所有的第三方库。但似乎我不应该手动执行此操作,因为所有第三方库都已列在我的JAR清单中。我究竟做错了什么?
这是我build.xml
文件:
<!-- Set global properties for this build -->
<property name="src" location="./src" />
<property name="build" location="./build"/>
<property name="dist" location="./dist"/>
<property name="logs" location="./logs"/>
<property name="docs" location="./docs"/>
<property name="jar" location="${dist}/dynamic_analyzer.jar"/>
<property name="lib" location="../../thirdparty/lib"/>
<property name="hive-util" location="../../hive-utils/dist"/>
<property name="hpdb" location="../../hive-db/hpdb/dist"/>
<property name="static" location="../../hive-backend/static_analyzer/dist"/>
<property name="mainclass" value="com.datawarellc.main.DynamicMain"/>
<path id="dep.runtime">
<fileset dir="${lib}" includes="**/*.jar"/>
<fileset dir="${hive-util}" includes="**/*.jar"/>
<fileset dir="${hpdb}" includes="**/*.jar"/>
<fileset dir="${static}" includes="**/*.jar"/>
</path>
<target name="clean">
<delete dir="${build}"/>
<delete dir="${dist}"/>
<delete dir="${docs}"/>
<delete dir="${logs}"/>
</target>
<target name="init">
<tstamp/>
<mkdir dir="${build}"/>
<mkdir dir="${dist}"/>
<mkdir dir="${logs}"/>
</target>
<target name="compile" depends="init">
<javac srcdir="${src}" destdir="${build}" debug="on" includeantruntime="false">
<classpath refid="dep.runtime" />
</javac>
<!-- Debug output of classpath -->
<property name="myclasspath" refid="dep.runtime"/>
<echo message="Classpath = ${myclasspath}"/>
</target>
<target name="jar" depends="compile">
<!-- Put the classpath in the manifest -->
<manifestclasspath property="manifest_cp" jarfile="${jar}" maxParentLevels="10">
<classpath refid="dep.runtime" />
</manifestclasspath>
<jar jarfile="${jar}" basedir="${build}">
<manifest>
<attribute name="Main-Class" value="${mainclass}"/>
<attribute name="Class-Path" value="${manifest_cp}"/>
</manifest>
<zipfileset dir="${src}" includes="**/*.xml" />
</jar>
</target>
你可以看到,我有第三方库的几个目录(${lib}
,${hive-util}
,${hpdb}
,并${static}
)。我使用这些来创建path
,称为dep.runtime
。然后在构建我的罐子时将dep.runtime
包含在清单中。 如何让Eclipse在执行时为构建路径和类路径使用相同的dep.runtime
?
这里检查文档:http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Freference%2Fpreferences% 2Fjava%2Fbuildpath%2Fref-preferences-user-libraries.htm (我想这是一个答案,但我没有安装Eclipse) – leeand00
@ leeand00我没有看到该文档如何帮助我。链接所说的菜单正是我用来手动添加所有第三方库的菜单(作为临时解决方法),但这正是我想要避免的。 – stepthom
应该有一个按钮,说在那里添加目录或添加类路径,这应该做的伎俩...让我知道,如果它不。 – leeand00