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

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

问题描述

我有工作得很好,在命令行上一个Ant 的build.xml 文件:汇编,构建JAR,我能够从执行的主要方法该JAR就好了。在的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是无法找到的第三方libaries,从而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. 它显示(在文本编辑器)大量编译错误的,因为
    大量的类undefined,并且

  2. 不能执行JAR。

我可以通过手动指定,有两种不同的地方(即通过构建路径属性 - &GT解决以上两个问题>,并通过设置 - 运行 - GT;的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} $ {蜂房UTIL} $ {HPDB} $ {静态} )。我用这些来创建一个路径名为 dep.runtime 。然后,我在清单中包括 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 ),该生成的.classpath 文件Eclipse使用。

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天全站免登陆