如何使用 Ant 将文件复制到现有 JAR 中? [英] How do I copy files into an existing JAR with Ant?

查看:25
本文介绍了如何使用 Ant 将文件复制到现有 JAR 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目需要访问其自己的 JAR 文件中的资源.当我为项目创建 JAR 文件时,我想将一个目录复制到该 JAR 文件中(我猜 ZIP 等效项是将目录添加"到现有 ZIP 文件中).我只希望在创建 JAR 之后进行复制(如果我清理并删除 JAR 文件,我显然不希望复制发生).

I have a project that needs to access resources within its own JAR file. When I create the JAR file for the project, I would like to copy a directory into that JAR file (I guess the ZIP equivalent would be "adding" the directory to the existing ZIP file). I only want the copy to happen after the JAR has been created (and I obviously don't want the copy to happen if I clean and delete the JAR file).

当前构建文件如下所示:

Currently the build file looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<project name="foobar" basedir=".." default="jar">

    <!-- project-specific properties -->
    <property name="project.path" value="my/project/dir/foobar" />

    <patternset id="project.include">
        <include name="${project.path}/**" />
    </patternset>
    <patternset id="project.jar.include">
        <include name="${project.path}/**" />
    </patternset>

    <import file="common-tasks.xml" />

    <property name="jar.file" location="${test.dir}/foobar.jar" />    
    <property name="manifest.file" location="misc/foobar.manifest" />
</project>

有些构建任务是从另一个文件 (common-tasks.xml) 调用的,我无法在此处显示.

Some of the build tasks are called from another file (common-tasks.xml), which I can't display here.

推荐答案

Jar/Ear Ant 任务是更通用的 Zip 任务.这意味着您还可以在 Jar 任务中使用 zipfileset:

The Jar/Ear Ant tasks are subtasks of the more general Zip task. This means that you can also use zipfileset in your Jar task:

<jar destfile="${jar.file}" basedir="...">
    <zipfileset dir="${project.path}" includes="*.jar" prefix="libs"/>
</jar>

我还看到您定义了一个单独的清单文件以包含在 Jar 中.您还可以使用嵌套的 manifest 命令:

I've also seen that you define a separate manifest file for inclusion into the Jar. You can also use a nested manifest command:

<jar destfile="@{destfile}" basedir="@{basedir}">
    <manifest>
        <attribute name="Built-By" value="..."/>
        <attribute name="Built-Date" value="..."/>

        <attribute name="Implementation-Title" value="..."/>
        <attribute name="Implementation-Version" value="..."/>
        <attribute name="Implementation-Vendor" value="..."/>
    </manifest>
</jar>

这篇关于如何使用 Ant 将文件复制到现有 JAR 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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