开发者

How can I match this Eclipse classpath functionality in ant?

I have a project (in Eclipse) that does this method call.

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

This ui folder is within a folder on the project root called res. To access it I modified 开发者_运维问答my run configuration's classpath and put a user entry that referred to the res folder.

Everything works when I run this through the run configuration.

Now I want to move this to a build script. This is where my skill isn't as sharp.

This is a part of my current ant build script. It's not working since it reports that getResource is returning null (file not found basically).

How can I modify this so it works?

Setup

<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> 

Compile

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

Jar

<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>


The class.getResource(...) will call the classloader.getSystemResource(...). It will look for the resource on the same classpath used to load your classes.

You need to add your gameui.xml to your jar or addressed its location in the "Class-Path" attribute in the manifest. That way our classloader will be able to find it.

For option1: Add the resource to the classpath: provide an additional fileset to your jar task:

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

For option2: Add the directory location to your classpath:

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

If you're don't need edit access to this file (e.g. to configure your program) I would put it into the jar and avoid external path issues.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜