Linux 如何使用单个命令在 UNIX 文件系统中提取 JAR 并使用 JAR 命令指定其目标目录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1079693/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 17:29:22  来源:igfitidea点击:

How do you extract a JAR in a UNIX filesystem with a single command and specify its target directory using the JAR command?

javapythonlinuxunixjar

提问by Christopher Tokar

I am creating a Python script within which I am executing UNIX system commands. I have a war archive named Binaries.war which is within an ear archive named Portal.ear

我正在创建一个 Python 脚本,在其中执行 UNIX 系统命令。我有一个名为 Binaries.war 的战争档案,它在一个名为 Portal.ear 的耳朵档案中

The Portal ear file resides in, say /home/foo/bar/

门户耳朵文件驻留在,说 /home/foo/bar/

jar xf /home/foo/bar/Portal.ear Binaries.war

Will extract the Binaries.war file out of the /home/foo/bar/Portal.ear archive into the current directory I am running the script from.

将 Binaries.war 文件从 /home/foo/bar/Portal.ear 存档中提取到我正在运行脚本的当前目录中。

How do you specify a target directory to be extracted to using just one command? I would like to do something like this to extract Binaries.war into the directory /home/foo/bar/baz

如何仅使用一个命令指定要提取到的目标目录?我想做这样的事情来将 Binaries.war 解压到目录 /home/foo/bar/baz

jar xf /home/foo/bar/Portal.ear Binaries.war [into target directory /home/foo/bar/baz]

I searched the the JAR man page for options and can't seem to find a simple way to do this. Of course I can extract the archive into my current directory and then move it using mv but I'd like to do this in one shot and not shuffle directories and files around.

我在 JAR 手册页中搜索了选项,但似乎找不到一种简单的方法来执行此操作。当然,我可以将存档解压缩到我当前的目录中,然后使用 mv 移动它,但我想一次性完成此操作,而不是随机播放目录和文件。

采纳答案by Jonathan Leffler

If your jar file already has an absolute pathname as shown, it is particularly easy:

如果您的 jar 文件已经具有如图所示的绝对路径名,则特别容易:

cd /where/you/want/it; jar xf /path/to/jarfile.jar

That is, you have the shell executed by Python change directory for you and then run the extraction.

也就是说,您已由 Python 执行的 shell 为您更改目录,然后运行提取。

If your jar file does not already have an absolute pathname, then you have to convert the relative name to absolute (by prefixing it with the path of the current directory) so that jarcan find it after the change of directory.

如果您的 jar 文件还没有绝对路径名,那么您必须将相对名称转换为绝对名称(通过在它前面加上当前目录的路径),以便jar在目录更改后可以找到它。

The only issues left to worry about are things like blanks in the path names.

唯一需要担心的问题是路径名中的空格。

回答by tddmonkey

I don't think the jar tool supports this natively, but you can just unzip a JAR file with "unzip" and specify the output directory with that with the "-d" option, so something like:

我认为 jar 工具本身不支持此功能,但是您可以使用“unzip”解压缩 JAR 文件,并使用“-d”选项指定输出目录,例如:

$ unzip -d /home/foo/bar/baz /home/foo/bar/Portal.ear Binaries.war

回答by Nick Fortescue

Can't you just change working directory within the python script using os.chdir(target)? I agree, I can't see any way of doing it from the jar command itself.

您不能使用 更改python 脚本中的工作目录os.chdir(target)吗?我同意,我从 jar 命令本身看不到任何方法。

If you don't want to permanently change directory, then store the current directory (using os.getcwd())in a variable and change back afterwards.

如果您不想永久更改目录,则将当前目录(使用os.getcwd())存储在一个变量中,然后再更改回来。

回答by Xiong Chiamiov

If this is a personal script, rather than one you're planning on distributing, it might be simpler to write a shell function for this:

如果这是一个个人脚本,而不是您计划分发的脚本,为此编写一个 shell 函数可能会更简单:

function warextract { jar xf   && mv   }

which you could then call from python like so:

然后你可以像这样从 python 调用它:

warextract /home/foo/bar/Portal.ear Binaries.war /home/foo/bar/baz/

If you really feel like it, you could use sed to parse out the filename from the path, so that you'd be able to call it with

如果你真的喜欢它,你可以使用 sed 从路径中解析出文件名,这样你就可以用

warextract /home/foo/bar/Portal.ear /home/foo/bar/baz/Binaries.war

I'll leave that as an excercise to the reader, though.

不过,我会将其作为练习留给读者。

Of course, since this will extract the .war out into the current directory first, and then move it, it has the possibility of overwriting something with the same name where you are.

当然,由于这会先将 .war 提取到当前目录中,然后再移动它,因此它有可能覆盖您所在位置的同名内容。

Changing directory, extracting it, and cd-ing back is a bit cleaner, but I find myself using little one-line shell functions like this all the time when I want to reduce code clutter.

更改目录、提取它并返回 cd 会更简洁一些,但是当我想减少代码混乱时,我发现自己一直在使用这样的单行 shell 函数。