'extract out one file from zip in maven

I have an zip file under a folder named output. The zip contains 3 files inside.

Project root/
    -output/
        -my.zip
    pom.xml

my.zip (contains names.txt, schools.txt, teachers.txt).

How can I extract out only names.txt from zip & put it under output/ in maven?

===What I have tried:===

I can achieve it by running command from project root :

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

But when I try to use exec-maven-plugin & run maven:

<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>
                <executable>unzip</executable>
                <commandlineArgs>-p output/my.zip names.txt > output/names.txt</commandlineArgs> 
            </configuration>
        </execution>
    </executions>
</plugin>

I always get Failed to execute goal with exec-maven-plugin

What is wrong with my exec-maven-plugin configuration, and any other ways to achieve it in maven?



Solution 1:[1]

You should take a look at the maven-dependency-plugin which offers a goal unpack:

<project>
   [...]
   <build>
     <plugins>
       <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-dependency-plugin</artifactId>
         <version>2.10</version>
         <executions>
           <execution>
             <id>unpack</id>
             <phase>package</phase>
             <goals>
               <goal>unpack</goal>
             </goals>
             <configuration>
               <artifactItems>
                 <artifactItem>
                   <groupId>junit</groupId>
                   <artifactId>junit</artifactId>
                   <version>3.8.1</version>
                   <type>jar</type>
                   <overWrite>false</overWrite>
                   <outputDirectory>${project.build.directory}/alternateLocation</outputDirectory>
                   <destFileName>optional-new-name.jar</destFileName>
                   <includes>**/*.class,**/*.xml</includes>
                   <excludes>**/*test.class</excludes>
                 </artifactItem>
               </artifactItems>
               <includes>**/*.java</includes>
               <excludes>**/*.properties</excludes>
               <outputDirectory>${project.build.directory}/wars</outputDirectory>
               <overWriteReleases>false</overWriteReleases>
               <overWriteSnapshots>true</overWriteSnapshots>
             </configuration>
           </execution>
         </executions>
       </plugin>
     </plugins>
   </build>
   [...]
 </project>

Solution 2:[2]

You can do it with the maven-antrun-plugin, with this example I think you will have your answer.

<plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <executions>
        <execution>
            <phase>package</phase>
            <configuration>
                <tasks>
                    <unzip src="${project.basedir}/output/test.zip" dest="${project.basedir}/output/unzip">
                        <patternset>
                            <include name="**/*.xml"/>
                        </patternset>
                    </unzip>
                </tasks>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Solution 3:[3]

It could be because of way commandLineArgs are being given.

For example: According to the plugin documentation the standard-out should be directed using an optional parameter "outputFile"

http://mojo.codehaus.org/exec-maven-plugin/exec-mojo.html#outputFile

Try to execute some simple commands like pwd or ls -la and then go back to your unzip example.

Update:

You may also need to look at giving arguments using XML CDATA

<sampleTag>
<![CDATA[
    Some text with > and <
 ]]>
</sampleTag>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sébastien Le Callonnec
Solution 2
Solution 3 Fabien