【问题标题】:How can I match this Eclipse classpath functionality in ant?如何在 ant 中匹配这个 Eclipse 类路径功能?
【发布时间】:2011-09-23 00:59:00
【问题描述】:

我有一个执行此方法调用的项目(在 Eclipse 中)。

GameMain.class.getResource("/ui/gameui.xml");

ui 文件夹位于项目根目录中名为res 的文件夹中。为了访问它,我修改了运行配置的类路径并放置了一个引用 res 文件夹的用户条目。

当我通过运行配置运行它时,一切正常。

现在我想将其移至构建脚本。这是我的技能不那么敏锐的地方。

这是我当前的 ant 构建脚本的一部分。它不起作用,因为它报告 getResource 正在返回 null(基本上找不到文件)。

如何修改它以使其正常工作?

设置

<property name="lib.dir" value="lib" />
<property name="res.dir" value="res" />

<path id="classpath">
    <fileset dir="${lib.dir}" includes="**/*.jar" />
    <fileset dir="${res.dir}" includes="**/*" />
</path> 

编译

<target name="compile" depends="clean">
    <mkdir dir="buildClient/classes" />
    <javac srcdir="src" destdir="buildClient/classes" classpathref="classpath" />
</target>

罐子

<target name="jar" depends="compile">
    <mkdir dir="buildClient/jar" />

    <manifestclasspath property="lib.list" jarfile="buildClient/jar/Client.jar">
        <classpath refid="classpath" />
    </manifestclasspath>

    <jar destfile="buildClient/jar/Client.jar" basedir="buildClient/classes">
        <manifest>
            <attribute name="Main-Class" value="murk.main.GameMain" />
            <attribute name="Class-Path" value="${lib.list}" />
        </manifest>
    </jar>
</target>

【问题讨论】:

    标签: java eclipse ant classpath


    【解决方案1】:

    class.getResource(...) 将调用 classloader.getSystemResource(...)。它将在用于加载类的同一类路径中查找资源。

    您需要将 gameui.xml 添加到您的 jar 在清单中的“Class-Path”属性中指定其位置。这样我们的类加载器就能找到它。

    对于选项 1:将资源添加到类路径:为您的 jar 任务提供额外的文件集:

    <jar destfile="buildClient/jar/Client.jar" >
    <fileset dir="buildClient/classes"/>
    <fileset dir="<path to the ui/ dir>" />
    ...
    

    对于选项 2: 将目录位置添加到您的类路径:

    <attribute name="Class-Path" value="${lib.list};<path to the ui/ dir>" />
    

    如果您不需要对此文件的编辑权限(例如配置您的程序),我会将其放入 jar 中并避免外部路径问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-30
      • 2010-10-14
      • 2011-01-25
      • 1970-01-01
      • 1970-01-01
      • 2017-02-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多