2015-04-27 47 views
1

我有my.zip文件在項目的target/文件夾下。Linux命令(unzip -p)使用exec-maven-plugin運行

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定義exec-maven-plugin執行相同的命令.xml

<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一起工作?

回答

1

>不是unzip的有效選項。 >或stdout重定向是您用來執行命令的shell的一項功能。這意味着shell將會看到>,剝離它和下一個參數,爲unzip創建一個新進程,重定向新進程的stdout並啓動它。

exec-maven-plugin不使用shell;而是使用shell在內部用來創建新進程的相同API。這意味着unzip將啓動,從命令行中找到奇怪的選項,並退出並出現錯誤。

爲了解決這個問題,有

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

請注意,你真的應該經過HTML逃脫>運行executable/bin/sh/bin/bash

要避免所有這些問題,您可以將這些命令放入shell腳本中並執行該命令,或使用Maven AntRun Pluginthe unzip task

+0

用'>'而不是'>'試過,它不起作用 – user842225

+0

@ user842225:這不是你唯一需要改變的東西。 –

+0

@Aaron,我非常感謝你通過添加'-c unzip'來更新你的答案,但它也不起作用。 – user842225