0

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?

thekbb
  • 7,668
  • 1
  • 36
  • 61
Chris O'Brien
  • 372
  • 6
  • 25
  • The question which comes to my mind is what is packaged that way? – khmarbaise Mar 10 '16 at 12:12
  • It's just a collection of XML files. Why it's done this way is not my concern, as such, as it's a internal product I have to use, and have no say in changing so I just have to roll with it! Using ant-jar throws in META-INF folders, which is why I'm not doing in that way. – Chris O'Brien Mar 10 '16 at 12:28
  • The simplest solution i could imaging is to use maven-assembly-plugin with an appropriate descriptor.... – khmarbaise Mar 10 '16 at 12:33

1 Answers1

2

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.

Community
  • 1
  • 1
Tunaki
  • 132,869
  • 46
  • 340
  • 423
  • Thanks I had a look at the first link, but perhaps I'm doing it wrong updated the main post – Chris O'Brien Mar 10 '16 at 13:31
  • @ChrisO'Brien I know, that's why I added the second link: you can't have more than 2 target. But I would suggest using the other solution with iterator-maven-plugin. – Tunaki Mar 10 '16 at 13:35
  • Cool. I'll dig into that. The maven plugin may not be an option as it's an internal repo, but I'll keep in in mind if the external ant file doesn't work. Thanks! – Chris O'Brien Mar 10 '16 at 13:39
  • Can you have another quick look? It's still not picking it up. – Chris O'Brien Mar 10 '16 at 14:14
  • @ChrisO'Brien I think it should be `` – Tunaki Mar 10 '16 at 14:23
  • No luck. The job is getting run as I see [INFO] --- maven-antrun-plugin:1.7:run (process-javascript-plugin) @ war--- [INFO] Executing tasks in the - I'll keep on digging :) – Chris O'Brien Mar 10 '16 at 14:26