在 Eclipse 中使用 Ant 的类路径 [英] Using Ant's classpath in Eclipse

查看:26
本文介绍了在 Eclipse 中使用 Ant 的类路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Ant build.xml 文件,它在命令行上工作得很好:它编译、构建 JAR,我能够很好地从 JAR 执行 main 方法.build.xml 文件引用了几个分散在各处的第三方库.构建 JAR 时,脚本不会将所有第三方库都包含到 JAR 本身中.相反,它将他们的路径放入 JAR 的清单中.这有助于让我的 JAR 保持苗条和整洁.

I have an Ant build.xml file that works just fine on the command line: it compiles, builds the JAR, and I am able to execute the main method from the JAR just fine. The build.xml file references several thirdparty libraries that are scattered here and there. When building the JAR, the script doesn't include all the thirdparty libraries into the JAR itself. Instead, it puts their path into the JAR's manifest. This helps to keep my JAR slim and tidy.

我希望能够在 Eclipse 中编辑和调试我的项目,但我找不到一种简单的方法来做到这一点.我可以让我的项目使用 Ant 文件来构建项目,这似乎有效.但是,Eclipse 无法找到第三方库,因此 Eclipse 有两个问题:

I'd like to be able to edit and debug my project in Eclipse, but I can't find an easy way to do so. I can have my project use the Ant file to build the project, and that seems to work. However, Eclipse is having trouble finding the thirdparty libaries, and thus Eclipse is having two problems:

  1. 它显示(在文本编辑器中)很多编译错误,因为许多类是未定义的,并且
  2. 它无法执行 JAR.

我可以通过在两个不同的地方手动指定来解决上述两个问题(即通过Properties->Java Build Path->Libraries的构建路径,以及执行类路径通过 Run Configurations->Classpath),所有第三方库.但似乎我不必手动执行此操作,因为所有第三方库都已列在我的 JAR 清单中.我做错了什么?

I can solve both of the above problems by specifying by hand, in two difference places (i.e., the build path via Properties->Java Build Path->Libraries, and the execution classpath via Run Configurations->Classpath), all the third party libraries. But it seems like I shouldn't have to do this manually, since all the third party libraries are already listed in my JAR's manifest. What am I doing wrong?

这是我的 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.runtimepath.然后,我在构建 jar 时在清单中包含 dep.runtime.如何让 Eclipse 在执行时对构建路径和类路径使用相同的 dep.runtime?

You can see that I have third-party libraries in several directories (${lib}, ${hive-util}, ${hpdb}, and ${static}). I use these to create a path called dep.runtime. I then include dep.runtime in the manifest when building my jar. How can I get Eclipse to use the same dep.runtime for the build path and the classpath when executing?

推荐答案

受到@leeand00 提供的链接的启发,我想出了以下解决方法.

I came up with the following workaround, inspired by the link provided by @leeand00.

首先,我编写了一个简单的 Perl 脚本(称为 genClasspath.pl),用于生成 Eclipse 使用的 .classpath 文件.

First, I wrote a simple Perl script (called genClasspath.pl) that generates the .classpath file that Eclipse uses.

#!/usr/bin/perl
use strict;

if (@ARGV != 2) {
  print STDERR "Usage: $0 OUTFILE CLASSPATHSTRING\n";
  print STDERR "e.g., $0 .classpath path1:path2:path3\n";
  exit 1;
}

my $OUTFILE         = $ARGV[0];
my $CLASSPATHSTRING = $ARGV[1];

open my $out_fh, '>', $OUTFILE or die "Couldn't open output file: $!";

print $out_fh q{<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" path="src"/>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
    <classpathentry kind="output" path="build"/>
};

my @libs = split(":", $CLASSPATHSTRING);
foreach my $thisLib (@libs){
    print $out_fh "    <classpathentry kind=\"lib\" path=\"$thisLib\"/>\n";
}
print $out_fh "</classpath>\n";

然后,我让我的 build.xml 文件使用 dep.runtime 的内容调用这个脚本:

Then, I have my build.xml file call this script with the content of dep.runtime:

<target name="compile" depends="init">
    <javac srcdir="${src}" destdir="${build}" debug="on" includeantruntime="false">
        <classpath refid="dep.runtime" />
    </javac>

    <property name="myclasspath" refid="dep.runtime"/>

    <exec dir="." executable="../../scripts/genClasspath.pl" os="Linux">
        <arg value=".classpath"/>
        <arg value="${myclasspath}"/>
    </exec>

</target>

唯一的问题是,在 Eclipse 中打开项目之前,我需要在命令行上至少运行一次 Ant.但是当我这样做时,Eclipse 能够很好地编译和执行我的项目,因为类路径与 Ant 的完全相同.

The only catch is that I need to run Ant on the command line at least once before I open the project in Eclipse. But when I do, Eclipse is able to compile and execute my project just fine, since the classpath is exactly the same as Ant's.

这篇关于在 Eclipse 中使用 Ant 的类路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆