当 ant 的 jar 任务包含在 jar 中时,如何重命名文件?
我想将一组看起来像这样的文件放入一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用带有
fullpath
属性的zipfileset
来重命名 jar 中的文件名:但是您不能使用这种技术的映射器,您必须列出每个文件jar 文件明确。如果您可以假设每个文件都名为
my.jar
,并且它们都位于直接子目录中,则可以使用subant
目标将它们全部组合起来:You can use a
zipfileset
with afullpath
attribute to rename the filename in the 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 thesubant
target to glob them all up:更新:您可能实际上想要 复制任务 而不是移动,但是 < href="http://ant.apache.org/manual/Types/mapper.html" rel="nofollow noreferrer">正则表达式映射器对于复制和移动的作用相同。
以下正则表达式会将 jars 目录中的所有 jar 复制到 jars_out,将 [folder]/[file].jar 映射到 [folder]_[file].jar。
正则表达式映射器需要类路径上有适当的正则表达式实现 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.
The regexp mapper needs an appropriate regexp implementation jar on the classpath to work. Various implementations are available:
如果您不想(或不容易)单独列出每个文件,一种解决方案是使用 Zip 任务,它允许嵌套
(Ant 1.8.0+) 。 Zip 任务完成后,您可以使用 Jar 任务添加/更新清单和/或添加索引。例如,假设除了想要
输出 JAR 之外,您还有一个目录
stuff/
,其中包含一些文件和子目录。如果您希望将stuff/
的内容包含在 JAR 中,但希望将stuff/subdir/*
添加为other_subdir/*
,请考虑以下事项:我测试了此构建文件,它适用于 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
in the output JAR, you also have a directory
stuff/
, containing some files and subdirectories. If you want the contents ofstuff/
included in the JAR, but you wantstuff/subdir/*
to be added asother_subdir/*
, consider the following: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.