I currently have a folder of jar files :
a1.jar , a2.jar , a3.jar , ........ , a300.jar
that need to include a "test.xml" file inside of each jar file, which will have descriptors inside corresponding to the name of each jar file.
For example:
Inside of a1.jar should be a test.xml that looks like:
<Home>
<Group>
<ID>a1</ID>
</Group>
</Home>
I am trying to do this in linux.
My attempt:
#!/bin/bash
jar_dir=.../jar
cd "$jar_dir" || exit
for file in *.jar; do
****
add text.xml file that has
<Home>
<Group>
<ID> $ name of jar file</ID>
</Group>
</Home>
****
done
It's just a matter of creating the XML file and then adding it to the JAR file.
$ cat so0597.sh
#!/usr/bin/env bash
# take in the jar directory as an argument, or use '.'
jar_dir=${1:-.}
cd "$jar_dir" || exit
for file in *.jar; do
# create the file in /tmp using a HEREDOC
cat <<EOX >/tmp/test.xml
<Home>
<Group>
<ID>$file</ID>
</Group>
</Home>
EOX
# add it using the 'jar' command
jar uvf $file -C /tmp test.xml
done
which results in:
$ ll
total 16
-rw-r--r-- 1 nobody nobody 684 Jun 18 21:12 a1.jar
-rw-r--r-- 1 nobody nobody 684 Jun 18 21:12 a2.jar
-rw-r--r-- 1 nobody nobody 684 Jun 18 21:12 a3.jar
-rwxrwxrwx 1 nobody nobody 201 Jun 18 21:10 so0597.sh
$ # JAR files are just ZIP files
$ # these JAR files are all identical
$ unzip -l a2.jar
Archive: a2.jar
Length Date Time Name
--------- ---------- ----- ----
0 06-18-2021 21:12 META-INF/
69 06-18-2021 21:12 META-INF/MANIFEST.MF
0 06-18-2021 20:53 bar
0 06-18-2021 20:53 foo
0 06-18-2021 20:53 test/
0 06-18-2021 20:53 test/baz
--------- -------
69 6 files
$ ./so0597.sh
adding: test.xml(in = 48) (out= 37)(deflated 22%)
adding: test.xml(in = 48) (out= 37)(deflated 22%)
adding: test.xml(in = 48) (out= 37)(deflated 22%)
$ unzip -l a2.jar
Archive: a2.jar
Length Date Time Name
--------- ---------- ----- ----
0 06-18-2021 21:12 META-INF/
69 06-18-2021 21:12 META-INF/MANIFEST.MF
0 06-18-2021 20:53 bar
0 06-18-2021 20:53 foo
0 06-18-2021 20:53 test/
0 06-18-2021 20:53 test/baz
48 06-18-2021 21:13 test.xml
--------- -------
117 7 files
$ unzip -c a2.jar test.xml
Archive: a2.jar
inflating: test.xml
<Home>
<Group>
<ID>a2.jar</ID>
</Group>
</Home>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.