Maven Plugin Development

Integration Test Code Example

In this example we will implement a very simple Maven Plugin which will printout some information about the current project. The outcome of this is the layout of the project.

.
|-- pom.xml
`-- src
    `-- main
        `-- java
            `-- com
                `-- soebes
                    `-- maven
                        `-- guide
                            `-- mp
                                `-- it
                                    `-- IntegrationTestMojo.java

The code of the Mojo (IntegrationTestMojo.java) is very simple and looks like the following:

/**
 *
 * @goal verify
 * @phase prepare-package
 */
public class IntegrationTestMojo extends AbstractMojo
{
    /**
     * The Maven Project Object
     *
     * @parameter default-value="${project}"
     * @readonly
     */
    private MavenProject project;

    /**
     * @parameter default-value="${localRepository}"
     */
    private ArtifactRepository localRepository;

    /**
     * List of Remote Repositories used by the resolver
     *
     * @parameter default-value="${project.remoteArtifactRepositories}"
     * @readonly
     */
    private List<ArtifactRepository> remoteRepositories;

    /**
     * @parameter default-value="${settings}"
     */
    private Settings settings;

    /**
     * This will turn on verbose behavior and will print out
     * all information about the artifacts.
     *
     * @parameter expression="${verbose}" default-value="false"
     */
    private boolean verbose;

    public void execute()
        throws MojoExecutionException
    {
        if (isVerbose()) {
            getLog().info("This is an output in verbose mode");
        }

        getLog().info("groupId:" + project.getGroupId());
        getLog().info("artifactId:" + project.getArtifactId());
        getLog().info("artifactId:" + project.getVersion());

        getLog().info("name:" + project.getName());
        getLog().info("description:" + project.getDescription());
        getLog().info("file: " + project.getFile());

        getLog().info("Active profiles:");
        for (String profile : settings.getActiveProfiles()) {
            getLog().info("profile: " + profile);
        }

        if (isVerbose()) {
            getLog().info("-- Local Repository --");
            getLog().info("Id:" + localRepository.getId());
            getLog().info("baseDir:" + localRepository.getBasedir());
            getLog().info("key:" + localRepository.getKey());
            getLog().info("Url:" + localRepository.getUrl());

            getLog().info("-- Remote Repositories --");
            for (ArtifactRepository ar : remoteRepositories) {
                getLog().info("Id:" + ar.getId());
                getLog().info("baseDir:" + ar.getBasedir());
                getLog().info("key:" + ar.getKey());
                getLog().info("Url:" + ar.getUrl());
            }
        }
    }

    public void setVerbose(boolean verbose) {
        this.verbose = verbose;
    }

    public boolean isVerbose() {
        return verbose;
    }

}

The mojo will be executed and will printout some information about the current project which in this case are the groupId, artifactId etc.

The pom.xml for the Maven Plugin looks like this. This excerpt contains only the relevant parts.

<project..>
  [...]
  <packaging>maven-plugin</packaging>
  [...]
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.8</version>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-report-plugin</artifactId>
        <version>2.8</version>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.1</version>
        <configuration>
          <source>1.5</source>
          <target>1.5</target>
          <debug>false</debug>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-plugin-plugin</artifactId>
        <version>2.7</version>
        <executions>
          <execution>
            <id>generated-helpmojo</id>
            <goals>
              <goal>helpmojo</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

  <dependencies>
    <dependency>
      <groupId>org.apache.maven</groupId>
      <artifactId>maven-plugin-api</artifactId>
      <version>${mavenVersion}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.maven</groupId>
      <artifactId>maven-project</artifactId>
      <version>2.2.1</version>
    </dependency>
    <dependency>
      <groupId>org.apache.maven</groupId>
      <artifactId>maven-settings</artifactId>
      <version>${mavenVersion}</version>
    </dependency>
  </dependencies>
</project>