繁体   English   中英

Java 的 jar:如何从特定的文件列表创建 .jar 文件并使用 -C 选项?

[英]Java's jar: How do I create a .jar file from a specific list of files and also use the -C option?

jar 和 POSIX tar 一样,支持 -C 选项,允许用户在包含某些文件之前更改目录

$> man jar
[... snip ...]
-C dir
    When creating (c) or updating (u) a JAR file, this option temporarily changes the directory while processing files specified
    by the file operands. Its operation is intended to be similar to the -C option of the UNIX tar utility. For example, the
    following command changes to the classes directory and adds the Bar.class file from that directory to my.jar:

    jar uf my.jar -C classes Bar.class
[... snip ...]

但是,我很难将这个 -C 特性与 @arg-file 特性结合起来:

$> man jar
<... snip ...>
jar c[efmMnv0] [entrypoint] [jarfile] [manifest] [-C dir] file ... [-Joption ...] [@arg-file ...]
[... snip ...]
@arg-file
    To shorten or simplify the jar command, you can specify arguments in a separate text file and pass it to the jar command
    with the at sign (@) as a prefix. When the jar command encounters an argument beginning with the at sign, it expands the
    contents of that file into the argument list.
[... snip ...]

在这个例子中,我有这棵树:

$> find .
.
./out
./src
./src/com
./src/com/dot
./src/com/dot/something
./src/com/dot/something/etc
./src/com/dot/something/etc/Two.class
./src/com/dot/something/etc/Three.class
./src/com/dot/something/etc/One.class
./src/com/dot/something/etc/Four.class
./list.txt

这个想法是打包罐子,使其包含如下内容:

$> jar tvf out/myjar.jar
     0 Fri Jan 03 16:17:44 GMT 2020 META-INF/
    69 Fri Jan 03 16:17:44 GMT 2020 META-INF/MANIFEST.MF
     0 Fri Jan 03 16:11:16 GMT 2020 com/dot/something/etc/One.class
     0 Fri Jan 03 16:11:20 GMT 2020 com/dot/something/etc/Two.class
     0 Fri Jan 03 16:11:24 GMT 2020 com/dot/something/etc/Three.class
     0 Fri Jan 03 16:11:26 GMT 2020 com/dot/something/etc/Four.class

即它删除了“src/”

我已经尝试过显而易见的:

$> jar cvf out/myjar.jar   -C src/     com/dot/something/etc/One.class com/dot/something/etc/Two.class
com/dot/something/etc/Two.class : no such file or directory
added manifest
adding: com/dot/something/etc/One.class(in = 0) (out= 0)(stored 0%)

但这失败了,因为它可以找到一些文件,但不能找到其他文件,这很奇怪!

我曾尝试使用此列表作为 arg 文件:

$> cat list.txt
com/dot/something/etc/One.class
com/dot/something/etc/Two.class
com/dot/something/etc/Three.class
com/dot/something/etc/Four.class

$> jar cvf out/myjar.jar   -C src/   @list.txt
com/dot/something/etc/Two.class : no such file or directory
com/dot/something/etc/Three.class : no such file or directory
com/dot/something/etc/Four.class : no such file or directory
added manifest
adding: com/dot/something/etc/One.class(in = 0) (out= 0)(stored 0%)

但这以同样的方式失败。

我还尝试在名称前添加src/目录的前缀,但它通过找到一些文件而不是其他文件而失败——但这次以相反的方式!

$> jar cvf out/myjar.jar   -C src/    src/com/dot/something/etc/One.class src/com/dot/something/etc/Two.class
src/src/com/dot/something/etc/One.class : no such file or directory
added manifest
adding: com/dot/something/etc/Two.class(in = 0) (out= 0)(stored 0%)

问:您如何使jar工具与特定文件列表和-C选项一起使用?

AFAICT 使其工作的唯一方法是两种类型路径的疯狂混合,即:

$> jar cvf out/myjar.jar   -C src/   com/dot/something/etc/One.class src/com/dot/something/etc/Two.class
added manifest
adding: com/dot/something/etc/One.class(in = 0) (out= 0)(stored 0%)
adding: com/dot/something/etc/Two.class(in = 0) (out= 0)(stored 0%)
$> jar tvf out/myjar.jar
     0 Fri Jan 03 16:16:22 GMT 2020 META-INF/
    69 Fri Jan 03 16:16:22 GMT 2020 META-INF/MANIFEST.MF
     0 Fri Jan 03 16:11:16 GMT 2020 com/dot/something/etc/One.class
     0 Fri Jan 03 16:11:20 GMT 2020 com/dot/something/etc/Two.class

您会注意到第一个路径不包含传递给 -C 的前缀,但所有后续文件都包含。

或者,使用此列表(与相关列表不同,因为它包含前缀):

$> cat list.txt
src/com/dot/something/etc/One.class
src/com/dot/something/etc/Two.class
src/com/dot/something/etc/Three.class
src/com/dot/something/etc/Four.class

如果你用较短的路径重新指定第一个元素,那么它都会起作用。

$> jar cvf out/myjar.jar -C src/ com/dot/something/etc/One.class   @list.txt
added manifest
adding: com/dot/something/etc/One.class(in = 0) (out= 0)(stored 0%)
adding: com/dot/something/etc/Two.class(in = 0) (out= 0)(stored 0%)
adding: com/dot/something/etc/Three.class(in = 0) (out= 0)(stored 0%)
adding: com/dot/something/etc/Four.class(in = 0) (out= 0)(stored 0%)
$> jar tvf out/myjar.jar
     0 Fri Jan 03 16:17:44 GMT 2020 META-INF/
    69 Fri Jan 03 16:17:44 GMT 2020 META-INF/MANIFEST.MF
     0 Fri Jan 03 16:11:16 GMT 2020 com/dot/something/etc/One.class
     0 Fri Jan 03 16:11:20 GMT 2020 com/dot/something/etc/Two.class
     0 Fri Jan 03 16:11:24 GMT 2020 com/dot/something/etc/Three.class
     0 Fri Jan 03 16:11:26 GMT 2020 com/dot/something/etc/Four.class

这对我来说似乎是一个错误,但从我所见,这种情况已经存在很长时间了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM