Developing a new plugin

For the development of a Java Maven plugin, we will use the same tool chain we presented in Chapter 1, Maven and Its Philosophy.

Using Eclipse's utility, we can create a new project with Maven archetype. So, we obtain a skeleton for the new Maven plugin project. This is a good starting point for our project. Perform the following steps:

  1. First of all, we will create a new Eclipse project as a new Maven project from the menu, as shown in the following screenshot:
    Developing a new plugin
  2. Then, we can choose the location for our project and click on Next:
    Developing a new plugin
  3. Now, we have to select the artifact type for our project. First, we choose Nexus Indexer, then we select maven-archetype-plugin, as shown in the following screenshot, and then click on Next:
    Developing a new plugin
  4. The last step in the creation procedure is the selection of the project's coordinates, as shown in the following screenshot:
    Developing a new plugin

The encouraged naming convention for artifactId is <ourplugin>-maven-plugin.

Tip

The name pattern, maven-<pluginname>-plugin, is reserved for official Apache Maven plugins. Such plugins are maintained by the official Apache Maven team and have groupId as org.apache.maven.plugins.

Using this name pattern is an infringement of the Apache Maven Trademark.

The archetype we chose during the creation phase created a pom.xml file with some dependencies imported by default:

  • maven-plugin-api: The articfact generation is set as default version 2.0. This is the basic library containing plugin utility classes.
  • maven-plugin-annotation: This contains the annotation system. More details are available at http://maven.apache.org/plugin-tools/maven-plugin-tools-annotations/.
  • maven-testing-plugin-harness: This is a library developed by Apache, for developing unit tests based on JUnit.
  • junit: This is imported with the scope test for testing purposes.

The POM file generated by our IDE will also contain a build profile that is created for integration tests, named maven-invoker-plugin.

Before you start writing the plugin source code, we have to slightly edit the POM file that has been generated. First, we need to change the plexus-utils default dependency with maven-core. The second and last modification consists of aligning the version of maven-plugin-api with the version we chose for maven-core.

The result of our work is the following POM file:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.example.mojo</groupId>
  <artifactId>mantis-maven-plugin</artifactId>
  <packaging>maven-plugin</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>mantis-plugin Maven Mojo</name>
  <url>http://maven.apache.org</url>

  <properties>
    <mavenVersion>3.2.1</mavenVersion>
  </properties>

  <dependencies>
    <!-- Maven dependencies -->
    <dependency>
      <groupId>org.apache.maven</groupId>
    <artifactId>maven-plugin-api</artifactId>
    <version>${mavenVersion}</version>
    </dependency>

    <dependency>
      <groupId>org.apache.maven</groupId>
      <artifactId>maven-core</artifactId>
      <version>${mavenVersion}</version>
     </dependency>

     <dependency>
       <groupId>org.apache.maven.plugin-tools</groupId>
       <artifactId>maven-plugin-annotations</artifactId>
       <version>3.2</version>
       <scope>provided</scope>
     </dependency>

     <!-- MySql driver -->
     <dependency>
       <groupId>mysql</groupId>
       <artifactId>mysql-connector-java</artifactId>
       <version>5.1.30</version>
     </dependency>
    
   <!-- Test dependencies -->
  <!-- Mandatory in order to works with maven-plugin-testing-       harness v. 3.1.0 -->
   <dependency>
   <groupId>org.apache.maven</groupId>
   <artifactId>maven-compat</artifactId>
   <version>3.2.1</version>
   <scope>test</scope>
  </dependency>

 <dependency>
   <groupId>org.apache.maven.plugin-testing</groupId>
  <artifactId>maven-plugin-testing-harness</artifactId>
  <version>3.1.0</version>
  <scope>test</scope>
 </dependency>

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>3.8.1</version>
  <scope>test</scope>
</dependency>
</dependencies>

<build>
<pluginManagement>
  <plugins>
   <plugin>                                   
   <groupId>
     org.apache.maven.plugins
   </groupId>
     <artifactId>maven-plugin-plugin</artifactId>
     <version>3.2</version>
     <executions>
    <execution>
      <id>mojo-descriptor</id>
        <goals>
          <goal>descriptor</goal>
        </goals>
    </execution>
    </executions>
    <configuration>
      <skipErrorNoDescriptorsFound>
      true
      <skipErrorNoDescriptorsFound>
    </configuration>

  </plugin>
</plugins>
</pluginManagement>
</build>
 <profiles>
    <profile>
      <id>integration-tests</id>
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-invoker-plugin</artifactId>
            <version>1.7</version>
            <configuration>
              <debug>true</debug>
              <cloneProjectsTo>
                ${project.build.directory}/it
              </cloneProjectsTo>
              <pomIncludes>
                <pomInclude>*/pom.xml</pomInclude>
              </pomIncludes>
              <postBuildHookScript>verify</postBuildHookScript>
              <localRepositoryPath>
                ${project.build.directory}/local-repo
              </localRepositoryPath>
              <settingsFile>src/it/settings.xml</settingsFile>
              <goals>
                <goal>clean</goal>
                <goal>test-compile</goal>
              </goals>
            </configuration>
            <executions>
              <execution>
                <id>integration-test</id>
                <goals>
                  <goal>install</goal>
                  <goal>integration-test</goal>
                  <goal>verify</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>

Tip

The maven-plugin-annotations dependency has the provided scope. Since annotations are not needed during plugin execution, this dependency can be excluded from the built package.

The maven-compat Version 3.2.1 is mandatory in order to make it possible to us the maven-plugin-testing-harness Version 3.1.0.

Once we perform a Maven update from our IDE, we can start to code the first Mojo.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset