Usage

Brief examples on how to use the build-helper-maven-plugin's goals.

Add more source directories to your project

Use this example to add more source directories to your project, since pom.xml only allows one source directory.

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <version>1.3</version>
        <executions>
          <execution>
            <id>add-source</id>
            <phase>generate-sources</phase>
            <goals>
              <goal>add-source</goal>
            </goals>
            <configuration>
              <sources>
                <source>some directory</source>
                ...
              </sources>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

Add more test source directories to your project

The same as the previous example, but this time we add more test source directories to your project.

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <version>1.3</version>
        <executions>
          <execution>
            <id>add-test-source</id>
            <phase>generate-sources</phase>
            <goals>
              <goal>add-test-source</goal>
            </goals>
            <configuration>
              <sources>
                <source>some directory</source>
                ...
              </sources>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

Add more resource directories to your project

This example shows how to add additional resource directories to your project. Another goal called add-test-resource can be used in a similar way to add test resources to the project.

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <version>1.3</version>
        <executions>
          <execution>
            <id>add-resource</id>
            <phase>generate-sources</phase>
            <goals>
              <goal>add-test-source</goal>
            </goals>
            <configuration>
              <resources>
                <resource>
                  <directory>src/my-resources</directory>
                  <targetPath>my-resources</targetPath>
                  <excludes>
                    <exclude>**/junk/**</exclude>
                  </excludes>
                </resource>
              </resources>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

Attach additional artifacts to your project

Typically run after antrun:run to attach antrun's build outputs to the project for install and deploy.

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <!-- add antrun configuration here -->
      </plugin>
      ...
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <version>1.3</version>
        <executions>
          <execution>
            <id>attach-artifacts</id>
            <phase>package</phase>
            <goals>
              <goal>attach-artifact</goal>
            </goals>
            <configuration>
              <artifacts>
                <artifact>
                  <file>some file</file>
                  <type>extension of your file </type>
                  <classifier>optional</classifier>
                </artifact>
                ...
              </artifacts>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

Set the current version of maven in a property

This can be used to keep track of what version of maven was used to build a particular artifact. For example, the following pom sets the property and then uses the property to save the maven version to the project jar's manifest.

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <version>1.3</version>
        <executions>
          <execution>
            <id>maven-version</id>
            <goals>
              <goal>maven-version</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.2</version>
        <configuration>
          <archive>
            <manifestEntries>
              <Maven-Version>${maven.version}</Maven-Version>
            </manifestEntries>
          </archive>
        </configuration>
      </plugin>
    </plugins>
  </build>
  ...
</project>

Access the parsed components of a project version.

The parse-versions mojo can be used to access the component parts of a version string. For example, the major version or the qualifier by themselves. After executing the following plugin configuration will parse the version of the current project and set several properties.

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <version>1.3</version>
        <executions>
          <execution>
            <id>parse-version</id>
            <goals>
              <goal>parse-version</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  ...
</project>

After the goal executes (by default during the validate phase) the following properties will contain the components of the current project version.

  parsedVersion.majorVersion
  parsedVersion.minorVersion
  parsedVersion.incrementalVersion
  parsedVersion.qualifier
  parsedVersion.buildNumber

The property prefix "parsedVersion" can be configured using the propertyPrefix mojo parameter. Note that the behaviour of the parsing is determined by org.apache.maven.artifact.versioning.DefaultArtifactVersion

An osgi compatible version will also be created and made available through the property:

  parsedVersion.osgiVersion

This property contains the original version string with the first instance of '-' replaced by '.' For example, 1.0.2-beta-1 will be converted to 1.0.2.beta-1

Remove project's artifacts from local repository

Mainly used to remove previously installed large project artifacts in local repository before install phase starts. The below example removes all project's artifacts including all versions from its local repository at the implicit package phase. To remove only the current version, remove removeAll configuration element or set it to false.

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <version>1.3</version>
        <executions>
          <execution>
            <id>remove-old-installers</id>
            <goals>
              <goal>remove-project-artifact</goal>
            </goals>
            <configuration>
              <removeAll>true</removeAll>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

Reserve a list of random and unused network ports

Use this plugin to as reserve a set of next available unused network ports to be placed in a given property set, so that they can be passed in other plugin configurations.

The example below shows how one can pass in random ports to selenium server' port and tomcat http port. Useful for running multiple tests concurrently.

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <version>1.2</version>
        <executions>
          <execution>
            <id>reserve-network-port</id>
            <goals>
              <goal>reserve-network-port</goal>
            </goals>
            <phase>process-resources</phase>
            <configuration>
              <portNames>
                <portName>selenium.server.port</portName>
                <portName>tomcat.http.port</portName>
              </portNames>
            </configuration>
          </execution>
        </executions>
      </plugin>
      
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>selenium-maven-plugin</artifactId>
        <executions>
          <execution>
            <id>start-selenium</id>
            <phase>test</phase>
            <goals>
              <goal>start-server</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <port>${selenium.server.port}</port>
          [...]
        </configuration>
      </plugin>

      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>tomcat-maven-plugin</artifactId>
        <executions>
          <execution>
            <id>run-tomcat</id>
            <phase>test</phase>
            <goals>
              <goal>run</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <port>${tomcat.http.port}</port>
          [...]
        </configuration>
      </plugin>
      
      [...]
      
    </plugins>
  </build>
</project>