Search code examples
mavenantzipant-contrib

Ant - Zip Multiple Folders


I'm trying to run an Ant job within Maven to look at a folder, and based on how many folders are in that folder, to create x number of zip files with the name of the folder. I have it working manually, but it would be nice if I didn't have to edit the pom each time I added a new folder to this structure.

<configuration>
    <target name="zip">
        <zip destfile="root/sub1/sub1.jar"> 
        <zipfileset dir="root/sub1/unpacked/" includes="**" ></zipfileset>
        </zip>
    </target>
</configuration>

If I were to add sub2 to the root dir, I would like it to be picked up automagically, and create a sub2.jar file (yes, I'm aware I'm using .jar, but the program that is taking these files expects .jar files, but they're not jar files in that they contain any Java code, they're just zip files with jar extensions)

I've tried this

Thanks I had a look at the first link, but perhaps I'm doing it wrong

<target name="checkDir">
    <foreach target="zip" param="theFile">
        <dirset dir="root" casesensitive="yes">
            <include name="**"/>
        </dirset>
    </foreach>
    </target>
<target name="zip">
<!-- <zip destfile="root/${theFile}/${theFile}.jar"> 
                            <zipfileset dir="root/${theFile}/unpacked/" includes="**" ></zipfileset>
</zip> -->
<echo message="${theFile}"/>
</target>

I just get

[INFO] --- maven-antrun-plugin:1.7:run (process-javascript-plugin) @ war--- [INFO] Executing tasks

zip: [echo] ${theFile} [INFO] Executed tasks


Still doesn't seem to be working.

pom.xml

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.7</version>
                <executions>
                    <execution>
                        <id>process-cartridges</id>
                        <phase>compile</phase>
                        <configuration>
                        <target>
                           <ant antfile="root/build-main.xml"/>
                        </target>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

build-main.xml

<?xml version="1.0" encoding="UTF-8" ?>
<project>
    <target name="checkDir">
        <foreach target="zip" param="theFile">
            <dirset dir="root" casesensitive="yes">
                <include name="**"/>
            </dirset>
        </foreach>
    </target>
    <target name="zip">
        <zip destfile="root/${theFile}/${theFile}.jar"> 
            <zipfileset dir="root/${theFile}/unpacked/" includes="**" />
        </zip>
        <echo message="TESTZIP ${theFile}"/>
    </target>
</project>

Doesn't seem to be working. Am I missing anything?


Solution

  • You could use the foreach task of Ant to do this, combining the answers from those two posts: Ant: How to execute a command for each file in directory? and Calling foreach in maven-antrun-plugin.

    However, you can also have a Maven solution using the iterator-maven-plugin to iterate over all sub-folders and then use the maven-jar-plugin to make the jar:

    <plugin>
      <groupId>com.soebes.maven.plugins</groupId>
      <artifactId>iterator-maven-plugin</artifactId>
      <version>0.3</version>
      <executions>
        <execution>
          <phase>package</phase>
          <goals>
            <goal>iterator</goal>
          </goals>
          <configuration>
            <folder>root/</folder>
            <pluginExecutors>
              <pluginExecutor>
                <plugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-jar-plugin</artifactId>
                  <version>2.6</version>
                </plugin>
                <goal>jar</goal>
                <configuration>
                  <classesDirectory>root/@item@/unpacked</classesDirectory>
                  <outputDirectory>root/@item@</outputDirectory>
                  <finalName>@item@.jar</finalName>
                </configuration>
              </pluginExecutor>
            </pluginExecutors>
          </configuration>
        </execution>
      </executions>
    </plugin>
    

    This is bound to the package phase. It will iterate over all direct subfolders of the root folder and invoke the jar plugin with the given configuration. @item@ is used to refer to the current directory name.