【发布时间】:2013-05-30 20:20:03
【问题描述】:
我有一个 Ant build.xml 文件,它在命令行上运行良好:它编译、构建 JAR,并且我能够从 JAR 执行 main 方法。 build.xml 文件引用了分散在各处的几个第三方库。构建 JAR 时,脚本不会将所有第三方库包含到 JAR 本身中。相反,它将它们的路径放入 JAR 的清单中。这有助于让我的 JAR 保持苗条和整洁。
我希望能够在 Eclipse 中编辑和调试我的项目,但我找不到一个简单的方法来做到这一点。我可以让我的项目使用 Ant 文件来构建项目,这似乎可行。但是,Eclipse 无法找到第三方库,因此 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})。我使用这些来创建一个名为dep.runtime 的path。然后,在构建我的 jar 时,我在清单中包含 dep.runtime。 如何让 Eclipse 在执行时对构建路径和类路径使用相同的 dep.runtime?
【问题讨论】:
-
在此处查看文档:help.eclipse.org/juno/…(我会将此作为答案,但我手头没有 Eclipse 安装)
-
@leeand00 我看不出该文档对我有何帮助。链接谈到的菜单正是我用来手动添加所有第三方库的菜单(作为临时解决方法),但这正是我试图避免的。
-
应该有一个按钮,上面写着
add directory或add classpath,这应该可以解决问题...如果没有,请告诉我。 -
@leeand00 让我看看我是否理解您的意思:我应该编写一个脚本来编辑
.classpath文件。然后我应该从我的build.xml文件中调用脚本,将dep.runtime变量的内容传递给脚本。类似的东西?