Linux命令(unzip -p)与exec-maven-plugin一起运行

时间:2015-04-27 15:44:03

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

我在项目的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?

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:1)

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

exec-maven-plugin没有使用shell;相反,它使用shell在内部使用的相同API来创建新进程。这意味着unzip将启动,从命令行中找到奇怪的选项并退出并显示错误。

要解决此问题,请使用

运行executable /bin/sh/bin/bash
<commandlineArgs>-c unzip -p target/my.zip names.txt &gt; target/names.txt</commandlineArgs>

请注意,您确实应该HTML转义>

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