Java 的 jar : How do I create a . jar 文件来自特定的文件列表并且还使用 -C 选项?

标签 java jar

该 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 ,使其包含如下内容:

$> 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%)

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

我尝试使用此列表作为参数文件:

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

这对我来说看起来像是一个错误,但据我所知,这种情况已经存在很长时间了。

关于Java 的 jar : How do I create a . jar 文件来自特定的文件列表并且还使用 -C 选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60097535/

相关文章:

java - Spring mvc Controller 返回Collection(set, list),jsp将其用作数组

Android Library Project 作为已编译的 JAR 库

java - 如何在 netbeans 中更改 jar 文件?

java - Stanford-NER 自定义对软件编程关键字进行分类

jar - 如何在Gradle中将文件从JAR复制到WAR?

php - 在 php 中执行 .jar 时出现问题

java - 冲突的 jar 方法

java - 使用 Spring JPA 规范按子查询排序

java - 在同一输入行添加 3 个整数

java - 硬编码方法,仅存在于父类(super class)中被重写