簡體   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