Fork me on GitHub

Iterator Maven Plugin

Overview

The iterator goal is intended to call one or more maven plugins during the iteration of the values.

Let us take a look at a real example which makes explanations simpler. We have the following layout of the project:

.
├── dev.xml
├── pom.xml
├── prod.xml
├── test.xml
└── src
    ├── main
    │   ├── java
    │   ├──   ...
    │   └── resources
    │       └── log4j.properties
    └── test
        └── java
               ...

Where the pom.xml file contains the following:

55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<plugin>
  <groupId>com.soebes.maven.plugins</groupId>
  <artifactId>iterator-maven-plugin</artifactId>
  <version>0.5.1</version>
  <executions>
    <execution>
      <phase>package</phase>
      <goals>
        <goal>iterator</goal>
      </goals>
      <configuration>
        <items>
          <item>test</item>
          <item>prod</item>
          <item>dev</item>
        </items>
 
        <pluginExecutors>
          <pluginExecutor>
            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-assembly-plugin</artifactId>
              <version>2.5.2</version>
            </plugin>
            <goal>single</goal>
            <configuration>
              <descriptors>
                <descriptor>${project.basedir}/@item@.xml</descriptor>
              </descriptors>
            </configuration>
          </pluginExecutor>
        </pluginExecutors>
      </configuration>
    </execution>
  </executions>
</plugin>

The above call will produce an output like the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
[INFO] Scanning for projects...
[INFO]                                                                        
[INFO] ------------------------------------------------------------------------
[INFO] Building Iterator Executor Invoker Maven Plugin mavenAssemblyTest 0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.6.1:clean (default-clean) @ mavenAssemblyTest ---
[INFO]
[INFO] --- maven-resources-plugin:2.7:resources (default-resources) @ mavenAssemblyTest ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.2:compile (default-compile) @ mavenAssemblyTest ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /home/iterator/target/classes
[INFO]
[INFO] --- maven-resources-plugin:2.7:testResources (default-testResources) @ mavenAssemblyTest ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/iterator/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.2:testCompile (default-testCompile) @ mavenAssemblyTest ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /home/iterator/target/test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ mavenAssemblyTest ---
[INFO] Surefire report directory: /home/iterator/target/surefire-reports
 
-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.soebes.maven.multiple.configuration.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec - in com.soebes.maven.multiple.configuration.AppTest
 
Results :
 
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
 
[INFO]
[INFO] --- maven-jar-plugin:2.5:jar (default-jar) @ mavenAssemblyTest ---
[INFO] Building jar: /home/iterator/target/mavenAssemblyTest-0.1-SNAPSHOT.jar
[INFO]
[INFO] --- iterator-maven-plugin:0.5.1:iterator (default) @ mavenAssemblyTest ---
[INFO] ------ (test) org.apache.maven.plugins:maven-assembly-plugin:2.5.2:single
[INFO] Reading assembly descriptor: /home/iterator/test.xml
[INFO] Building war: /home/iterator/target/mavenAssemblyTest-0.1-SNAPSHOT-test.war
[INFO] ------ (prod) org.apache.maven.plugins:maven-assembly-plugin:2.5.2:single
[INFO] Reading assembly descriptor: /home/iterator/prod.xml
[INFO] Building war: /home/iterator/target/mavenAssemblyTest-0.1-SNAPSHOT-prod.war
[INFO] ------ (dev) org.apache.maven.plugins:maven-assembly-plugin:2.5.2:single
[INFO] Reading assembly descriptor: /home/iterator/dev.xml
[INFO] Building war: /home/iterator/target/mavenAssemblyTest-0.1-SNAPSHOT-dev.war
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.878 s
[INFO] Finished at: 2017-08-07T19:17:34+02:00
[INFO] Final Memory: 19M/310M
[INFO] ------------------------------------------------------------------------

As you can see the maven-assembly-plugin is being called three times with the parameter test, prod and dev which adresses the appropriate descriptor in the folder.