【问题标题】:Linux command (unzip -p) run with exec-maven-pluginLinux 命令 (unzip -p) 与 exec-maven-plugin 一起运行
【发布时间】:2015-07-06 04:07:27
【问题描述】:

我在项目的target/ 文件夹下有 my.zip 文件。

MyProject/
    -target/
           -my.zip
     -pom.xml

my.zip 中有一个名为 names.txt 的文件。如果我在项目根目录下运行 linux 命令:

unzip -p target/my.zip names.txt > target/names.txt

我成功将names.txt解压到target/文件夹:

MyProject/
    -target/
           -my.zip
           -names.txt
     -pom.xml

我想用 pom.xml 中定义的exec-maven-plugin 执行相同的命令。

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.4.0</version>
    <executions>
       <execution>
          <id>get names.txt</id>
          <phase>package</phase>
          <goals>
             <goal>exec</goal>
           </goals>
           <configuration>
             <!-- define the command to execute -->
             <executable>unzip</executable>
             <arguments>
               <commandlineArgs>-p target/my.zip names.txt > target/names.txt</commandlineArgs>
              </arguments>
            </configuration>
         </execution>
      </executions>
   </plugin>

但是当我运行maven clean install 时,它不会生成 names.txt ,终端会显示我解压缩帮助文档:

UnZip 5.52 of 28 February 2005, by Info-ZIP.  Maintained by C. Spieler.  Send
bug reports using http://www.info-zip.org/zip-bug.html; see README for details.

Usage: unzip [-Z] [-opts[modifiers]] file[.zip] [list] [-x xlist] [-d exdir]
  Default action is to extract files in list, except those in xlist, to exdir;
  file[.zip] may be a wildcard.  -Z => ZipInfo mode ("unzip -Z" for usage).

  -p  extract files to pipe, no messages     -l  list files (short format)
  -f  freshen existing files, create none    -t  test compressed archive 

为什么?如何使其与 exec-maven-plugin 一起使用?

【问题讨论】:

    标签: maven command-line maven-3 unzip exec-maven-plugin


    【解决方案1】:

    exec-maven-plugin 有一个选项 outputFile,它将命令的输出写入适当的文件。

    【讨论】:

      【解决方案2】:

      &amp;gt; 不是unzip 的有效选项。 &amp;gt; 或 stdout 重定向是用于执行命令的 shell 的一项功能。这意味着 shell 将看到&amp;gt;,剥离它和下一个参数,为unzip 创建一个新进程,重定向新进程的标准输出并启动它。

      exec-maven-plugin 不使用外壳;相反,它使用与外壳内部用于创建新进程的 API 相同的 API。这意味着unzip 将启动,从命令行中找到奇怪的选项并退出并出现错误。

      要解决此问题,请运行 executable /bin/sh/bin/bash with

      <commandlineArgs>-c unzip -p target/my.zip names.txt &gt; target/names.txt</commandlineArgs>
      

      请注意,您确实应该 HTML 转义 &amp;gt;

      为避免所有这些问题,您可以将这些命令放入 shell 脚本并执行该脚本,或者使用 Maven AntRun Pluginthe unzip task

      【讨论】:

      • 尝试使用&amp;gt; 而不是&amp;gt;,它也不起作用
      • @user842225:这不是您需要更改的唯一内容。
      • @ Aaron ,我没有注意到您通过添加 -c unzip 更新了您的答案,但它也不起作用。
      • 我使用 Maven AntRun 插件解决了这个问题。谢谢!
      猜你喜欢
      • 2011-07-03
      • 2019-02-24
      • 2014-12-25
      • 2019-10-16
      • 2013-07-01
      • 2012-03-03
      • 2017-08-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多