当 ant 的 jar 任务包含在 jar 中时,如何重命名文件?

发布于 2024-08-05 15:48:20 字数 344 浏览 1 评论 0原文

我想将一组看起来像这样的文件放入一个 jar 中:

yay/my.jar
boo/my.jar
foo/my.jar
bar/my.jar

在这个过程中,我希望将它们全部重命名如下:

yay_my.jar
boo_my.jar
foo_my.jar
bar_my.jar

我希望使用映射器来完成此操作,但是我正在使用的文件集元素和 jar任务似乎在任何地方都不支持它。

在构建 jar 时如何应用映射器,或者如何执行这样的转换?我想避免将所有文件复制到我想要的目录结构并在各处进行重复,这就是我们的构建系统现在的工作方式。

I want to put a set of files that look like this into a jar:

yay/my.jar
boo/my.jar
foo/my.jar
bar/my.jar

In the process, I want all of them renamed as follows:

yay_my.jar
boo_my.jar
foo_my.jar
bar_my.jar

I was hoping to use a mapper to accomplish this, but the fileset elements I am using and the jar task don't seem to support it anywhere.

How do I apply a mapper when building a jar, or else how can I perform a transformation like this? I want to avoid copying all the files to the directory structure I want and making duplicates all over the place, which is how our build system works now.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一花一树开 2024-08-12 15:48:20

您可以使用带有 fullpath 属性的 zipfileset 来重命名 jar 中的文件名:

<jar destfile="newjar.jar">
    <zipfileset dir="yay" includes="my.jar" fullpath="yay_my.jar"/>
    <zipfileset dir="boo" includes="my.jar" fullpath="boo_my.jar"/>
    <!-- etc. -->
</jar>

但是您不能使用这种技术的映射器,您必须列出每个文件jar 文件明确。如果您可以假设每个文件都名为 my.jar,并且它们都位于直接子目录中,则可以使用 subant 目标将它们全部组合起来:

<target name="glom">
    <subant genericantfile="${ant.file}" target="update-jar">
        <dirset dir="." includes="*"/>
    </subant>
</target>

<target name="update-jar">
    <basename file="${basedir}" property="dirname"/>
    <property name="path" value="${dirname}_my.jar"/>
    <jar destfile="../newjar.jar" update="yes">
        <zipfileset dir="." includes="my.jar" fullpath="${path}"/>
    </jar>
</target> 

You can use a zipfileset with a fullpath attribute to rename the filename in the jar:

<jar destfile="newjar.jar">
    <zipfileset dir="yay" includes="my.jar" fullpath="yay_my.jar"/>
    <zipfileset dir="boo" includes="my.jar" fullpath="boo_my.jar"/>
    <!-- etc. -->
</jar>

You can't use a mapper with this technique though, you'll have to list each jar file explicitly. If you can assume that every file is named my.jar, and they are all in an immediate child directory, you can use the subant target to glob them all up:

<target name="glom">
    <subant genericantfile="${ant.file}" target="update-jar">
        <dirset dir="." includes="*"/>
    </subant>
</target>

<target name="update-jar">
    <basename file="${basedir}" property="dirname"/>
    <property name="path" value="${dirname}_my.jar"/>
    <jar destfile="../newjar.jar" update="yes">
        <zipfileset dir="." includes="my.jar" fullpath="${path}"/>
    </jar>
</target> 
梦醒时光 2024-08-12 15:48:20

更新:您可能实际上想要 复制任务 而不是移动,但是 < href="http://ant.apache.org/manual/Types/mapper.html" rel="nofollow noreferrer">正则表达式映射器对于复制和移动的作用相同。

以下正则表达式会将 jars 目录中的所有 jar 复制到 jars_out,将 [folder]/[file].jar 映射到 [folder]_[file].jar。

<copy todir="./jars_out">
  <fileset dir="jars"/>
  <mapper type="regexp" from="([^/\\]*)[/\\](.*)\.jar$" to="\1_\2.jar"/>
</copy>

正则表达式映射器需要类路径上有适当的正则表达式实现 jar 才能工作。可以使用各种实现:

Update: you probably actually want the copy task rather than move, but the regexp mapper works the same for both copy and move.

The following regexp will copy all jars in the jars directory to jars_out, mapping [folder]/[file].jar to [folder]_[file].jar.

<copy todir="./jars_out">
  <fileset dir="jars"/>
  <mapper type="regexp" from="([^/\\]*)[/\\](.*)\.jar$" to="\1_\2.jar"/>
</copy>

The regexp mapper needs an appropriate regexp implementation jar on the classpath to work. Various implementations are available:

燃情 2024-08-12 15:48:20

如果您不想(或不容易)单独列出每个文件,一种解决方案是使用 Zip 任务,它允许嵌套 (Ant 1.8.0+) 。 Zip 任务完成后,您可以使用 Jar 任务添加/更新清单和/或添加索引。

例如,假设除了想要

yay_my.jar
boo_my.jar
foo_my.jar
bar_my.jar

输出 JAR 之外,您还有一个目录 stuff/,其中包含一些文件和子目录。如果您希望将 stuff/ 的内容包含在 JAR 中,但希望将 stuff/subdir/* 添加为 other_subdir/*,请考虑以下事项:

<zip destfile="newjar.jar">
    <zipfileset dir="yay" includes="my.jar" fullpath="yay_my.jar"/>
    <zipfileset dir="boo" includes="my.jar" fullpath="boo_my.jar"/>
    <!-- etc. -->

    <mappedresources>
        <fileset dir="stuff"/>
        <compositemapper>
            <globmapper from="subdir/*" to="other_subdir/*"/>
            <identitymapper/>
        </compositemapper>
    </mappedresources>
</zip>
<!-- Update the newly-created ZIP in-place to add a basic manifest -->
<jar destfile="newjar.jar" update="true"/>

我测试了此构建文件,它适用于 Ant 1.8.2。但是,如果更改后 Ant 开始抛出 NullPointerException,请参阅 错误 54026

If you don't want to (or can't readily) list each file individually, one solution is to use the Zip task, which allows a nested <mappedresources> (Ant 1.8.0+). Once the Zip task completes, you can use the Jar task to add/update the manifest and/or add an index.

Suppose, for example, that in addition to wanting

yay_my.jar
boo_my.jar
foo_my.jar
bar_my.jar

in the output JAR, you also have a directory stuff/, containing some files and subdirectories. If you want the contents of stuff/ included in the JAR, but you want stuff/subdir/* to be added as other_subdir/*, consider the following:

<zip destfile="newjar.jar">
    <zipfileset dir="yay" includes="my.jar" fullpath="yay_my.jar"/>
    <zipfileset dir="boo" includes="my.jar" fullpath="boo_my.jar"/>
    <!-- etc. -->

    <mappedresources>
        <fileset dir="stuff"/>
        <compositemapper>
            <globmapper from="subdir/*" to="other_subdir/*"/>
            <identitymapper/>
        </compositemapper>
    </mappedresources>
</zip>
<!-- Update the newly-created ZIP in-place to add a basic manifest -->
<jar destfile="newjar.jar" update="true"/>

I tested this build file and it works with Ant 1.8.2. However, if after changing it Ant begins throwing a NullPointerException, see Bug 54026.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文