Issue
I have my.zip file under project’s target/
folder.
MyProject/
-target/
-my.zip
-pom.xml
Inside my.zip there is a file named names.txt. If I run linux command under project root:
unzip -p target/my.zip names.txt > target/names.txt
I successfully get the names.txt extracted to target/
folder:
MyProject/
-target/
-my.zip
-names.txt
-pom.xml
I want to execute the same command with exec-maven-plugin defined in pom.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>
But when I run maven clean install
, it doesn’t generate the names.txt , the terminal shows me unzip help document instead:
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
why? How can I make it working with exec-maven-plugin ?
Solution
>
isn't a valid option for unzip
. >
or stdout redirection is a feature of the shell which you use to execute the command. That means the shell will see >
, strip it and the next argument, create a new process for unzip
, redirect stdout of the new process and start it.
exec-maven-plugin
doesn't use a shell; instead it's using the same API which the shell uses internally to create new processes. That means unzip
will start, find strange options from the command line and quit with an error.
To fix this, run the executable
/bin/sh
or /bin/bash
with
<commandlineArgs>-c "unzip -p target/my.zip names.txt > target/names.txt"</commandlineArgs>
Note that you really should HTML escape >
.
To avoid all these problems, you can either put these commands into a shell script and execute that or use Maven AntRun Plugin and the unzip
task.
Answered By - Aaron Digulla
Answer Checked By - Mary Flores (JavaFixing Volunteer)