Fork me on GitHub

Iterator Maven Plugin

Using Property

The iterator-maven-plugin will inject the current value as a property which means you can use this property to parameterize your build. First let us take a view on an example project:

.
├── pom.xml
└── src
    ├── main
    │   ├── assembly
    │   │   └── archive.xml
    │   ├── environment
    │   │   ├── dev
    │   │   │   └── server.properties
    │   │   ├── production
    │   │   │   └── server.properties
    │   │   ├── qa
    │   │   │   └── server.properties
    │   │   └── test
    │   │       └── server.properties
    │   ├── java
    │   │   └── ...
    └── test
        └── java
            └── ...

The next step is to look at the pom.xml file which will define the iteration over the values dev, production, qa, test which is defined by:

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
91
<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>dev</item>
          <item>test</item>
          <item>production</item>
          <item>qa</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}/src/assembly/archive.xml</descriptor>
              </descriptors>
            </configuration>
          </pluginExecutor>
        </pluginExecutors>
      </configuration>
    </execution>
  </executions>
</plugin>

During the execution the values for item will be injected as property in the maven model which means they can be accessed by the usual property usage like ${item}. This is done in the assembly descriptor to fulfill the needs in this case.

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
<assembly
 
  <id>${item}</id>
  <formats>
    <format>war</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>
  <dependencySets>
    <dependencySet>
      <unpack>true</unpack>
      <useProjectArtifact>true</useProjectArtifact>
    </dependencySet>
  </dependencySets>
  <fileSets>
    <fileSet>
      <outputDirectory>WEB-INF</outputDirectory>
      <directory>${basedir}/src/main/environment/${item}/</directory>
      <includes>
        <include>**</include>
      </includes>
    </fileSet>
    <fileSet>
      <outputDirectory>WEB-INF</outputDirectory>
      <directory>${project.build.directory}/environment/${item}/</directory>
      <includes>
        <include>**</include>
      </includes>
    </fileSet>
  </fileSets>
</assembly>