【发布时间】:2010-09-15 13:43:01
【问题描述】:
如何将文件夹(例如包含艺术的资源文件夹)添加到 netbeans 项目的类路径?我设法通过编辑项目的 NB 生成的 jar 文件(即它的 MANIFEST.MF 文件 + 手动复制资源)手动做到这一点,但是应该有一种方法来告诉 netbeans 以及注意资源,不是吗?
文件夹结构如下:
/project/art/
/project/dist/lib/
/project/dist/art/
/project/dist/project.jar
/project/lib/
/project/src/
我不想将艺术品打包到罐子中,因为我希望艺术品可以轻松更换。如果我将 art 文件夹添加到 src 文件夹中,那么 NB 可以正常编译,但 art 资源最终会在 jar 中。
将 art 文件夹添加到 netbeans 项目库(属性 -> 库 -> 添加 JAR/文件夹)似乎不起作用,因为我最终得到一个错误'...\project\art is a directory or can不被阅读。不复制库。这反过来甚至阻止了真正的库文件夹被复制。
有什么想法吗?
最好的问候 克里斯
2 基于 gpeche 的 cmets 的观察结果: a)而不是在“运行”选项卡中指定附加资源文件夹而不是在项目属性的“编译”选项卡中 - >库似乎在Netbeans中没有太大区别(我目前使用的是6.9.1) .输出(因此错误)保持不变,即根本没有复制任何内容:
Created dir: C:\Users\Chrisi\Desktop\vocabulary\VocabularyTrainer\dist
C:\Users\Chrisi\Desktop\vocabulary\VocabularyTrainer\art is a directory or can't be read. Not copying the libraries.
Not copying the libraries.
Building jar: C:\Users\Chrisi\Desktop\vocabulary\VocabularyTrainer\dist\VocabularyTrainer.jar
另一个有趣的方面是,在“库”面板的帮助菜单中,没有明确提及将文件夹作为库。是否有可能,Netbeans 中的按钮被错误命名,即只允许使用真正的 jar?
b) 将资源文件夹添加到库列表确实会产生影响,将另一个条目添加到 MANIFEST.MF。虽然 - 这是较小的问题 - 类路径条目似乎总是期望资源文件夹是库文件夹的子文件夹(例如“lib/arts”),主要问题是似乎缺少斜线。 如前所述,在 MANIFEST.MF 中 NB 生成的条目看起来像这样“lib/arts”(这对我不起作用),而(手动设置)“lib/arts/”呢?!
我使用文件夹中资源的方式是这样的:
URL resource = getClass().getResource("/gui/refresh.png");
ImageIcon tmp = new ImageIcon(resource);
编辑:
根据 Tushars 的评论和 this posting,我发现以下解决方案是功能性和舒适性之间可接受的折衷方案。
我从自动生成的“build-impl.xml”文件中覆盖了 ANT 目标,该文件在 Netbeans 项目的基本“build.xml”文件中的 MANIFEST.MF 文件中创建了类路径。转到“build.xml”文件的代码如下所示:
<property name="art.classpath" value="art/" />
<target name="-post-jar">
<mkdir dir="${dist.dir}/art"/>
<copy todir="${dist.dir}/art">
<fileset dir="${basedir}/art">
<!-- <exclude name="**/!source/**"/> if you want to exclude something... -->
</fileset>
</copy>
</target>
<target name="-init-macrodef-copylibs">
<macrodef name="copylibs" uri="http://www.netbeans.org/ns/j2se-project/3">
<element name="customize" optional="true"/>
<sequential>
<property location="${build.classes.dir}" name="build.classes.dir.resolved"/>
<pathconvert property="run.classpath.without.build.classes.dir">
<path path="${run.classpath}"/>
<map from="${build.classes.dir.resolved}" to=""/>
</pathconvert>
<pathconvert pathsep=" " property="jar.classpath">
<path path="${run.classpath.without.build.classes.dir}"/>
<chainedmapper>
<flattenmapper/>
<globmapper from="*" to="lib/*"/>
</chainedmapper>
</pathconvert>
<taskdef classname="org.netbeans.modules.java.j2seproject.copylibstask.CopyLibs" classpath="${libs.CopyLibs.classpath}" name="copylibs"/>
<copylibs compress="${jar.compress}" index="${jar.index}" jarfile="${dist.jar}" manifest="${manifest.file}" runtimeclasspath="${run.classpath.without.build.classes.dir}">
<fileset dir="${build.classes.dir}"/>
<manifest>
<attribute name="Class-Path" value="${jar.classpath} ${art.classpath}"/>
<customize/>
</manifest>
</copylibs>
</sequential>
</macrodef>
</target>
权衡的是,为了在 Netbeans 中进行开发,我仍然必须将资源文件夹(例如“art”)添加到库列表中,以使项目在 Netbeans 中运行。这将导致在 MANIFEST.MF 文件 ('lib/art') 中增加一个条目,同时库不会自动复制到 'dist' 文件夹中,并显示消息
...\art is a directory or can't be read. Not copying the libraries.
Not copying the libraries.
这种行为是 - afaik - 旨在(强制将所有东西捆绑在一个罐子中),即使有关于它的讨论正在进行。要制作真正的分发包,我必须从 NB 的库列表中删除资源文件夹并重新构建。
当然,我们仍然欢迎有关更简化设置且无需任何权衡的想法。 :)
【问题讨论】: