Writing a Nexus Plugin Using m2eclipse


January 8, 2010 By Damian Bradicich

nexus-small

In this post, I’m going to walk you through the process of creating a new Nexus plugin using the m2eclipse plugin for eclipse from start to finish including all steps in the process.  After reading this post you will know about the easiest way to get started working on your own Nexus plugin.

Some things to note are that I am using the latest m2e 0.9.9 dev build available from the http://m2eclipse.sonatype.org/update-dev update site. I am also using eclipse 3.5.1.  All maven builds have been done using local build of latest maven 3.0-SNAPSHOT, however, that is not a requirement for the plugins to work, just a recommendation as maven 3 (in my opinion) is much better than maven 2 :)

Configuring Your Environment for the Nexus Plugin Project

To successfully create a Nexus Plugin using the Nexus Plugin archetype, you will need to configure your Maven Settings to include both the Sonatype Forge repository and the Central Maven repository. If you are already using Nexus and you have configured your settings.xml to use a single, consolidated repository group, all you need to do is add a proxy repository for the Sonatype Forge repository to your repository group. If you are not using Nexus, you will need to use a settings.xml that resembles the following sample:

<settings>
  <profiles>
    <profile>
      <id>sonatype-forge</id>
      <repositories>
        <repository>
          <id>sonatype-forge</id>
          <url>http://repository.sonatype.org/content/groups/forge/</url>
          <releases>
            <enabled>true</enabled>
          </releases>
          <snapshots>
            <enabled>true</enabled>
          </snapshots>
        </repository>
      </repositories>
      <pluginRepositories>
        <pluginRepository>
          <id>sonatype-forge</id>
          <url>http://repository.sonatype.org/content/groups/forge/</url>
          <releases>
            <enabled>true</enabled>
          </releases>
          <snapshots>
            <enabled>true</enabled>
          </snapshots>
        </pluginRepository>
      </pluginRepositories>
    </profile>
  </profiles>
  <activeProfiles>
    <activeProfile>sonatype-forge</activeProfile>
  </activeProfiles>
</settings>

Creating a New Project with the Nexus Plugin Archetype

Let’s start by creating a ‘New Maven Project’ in m2eclipse.

new-project

Select Maven > Maven Project, and click on Next until you reach the archetype selection. On this screen you can search for and select the archetype which will create your new project. Note that you must use the 1.2 version of the nexus-plugin-archetype.

archetype-selection

Enter the GAV (groupId, artifactId, version) params that will uniquely define the plugin, and click on Next.

Targeting the Proper Nexus Version

After supplying the project identifiers, you will see a panel that display the archetype property ${nexusVersion}. This is the version of Nexus that your plugin will target and it will associate your project with the proper set of dependencies for a specific version of Nexus.

gav-params

By default the nexus-plugin-archetype will target Nexus version 1.4.1.   If you are targeting 1.4.1, it is important that you add the following to your project’s dependency management because of a problem w/ the parent POM of Nexus 1.4.1. If you are not tied to using Nexus 1.4.1 we strongly recommend that you update the Nexus version you are targeting 1.4.2-SNAPSHOT and disregard this change. The 1.4.2-SNAPSHOT POMs have been updated properly and they do not require the following dependency management workaround.

<dependencyManagement>
    <dependencies>
        <!- Workaround for swizzle-jira:1.3.3-SONATYPE-2, as it has an incorrect parent ->
        <dependency>
            <groupId>org.codehaus.swizzle</groupId>
            <artifactId>swizzle-jira</artifactId>
            <version>1.3.3-SONATYPE-221</version>
        </dependency>
    </dependencies>
</dependencyManagement>

If you have already generated the project, and you need to change the target Nexus version you will need to edit this new project’s POM. Open up the POM of your Nexus Plugin and find the section that defines properties. You will need to change the nexus-version property to target a Nexus version. After creating a project with the Nexus Plugin archetype, you will see a properties section of the POM that contains a ${nexusVersion} reference as follows:

<project>
  ...
  <properties>
    <!-- Set the Nexus version here, against which you build the plugin -->
    <nexus-version>${nexusVersion}</nexus-version>
  </properties>
  ...
</project>

Click Next after setting the ${nexusVersion}, and voila, we have a nexus-plugin project.

Building and Deploying the Plugin Project

Your new Nexus plugin project has some sample components you can use as a starting point for your own plugin development.

new-project-package-explorer

You can now build this project and expand the bundle right into your nexus installation ${nexus-work-dir}/plugin-repository/

nexus-work-directory-layout

And when Nexus starts it will find the plugin and load it automatically. For this sample plugin, you should see the log message in the Nexus log:

Plugin manager “ACTIVATE” on plugin “org.sonatype.nexus.plugin:test-plugin:0.0.1-SNAPSHOT” was successful.

As expected, you are now able to properly use the new plexus resource, I used curl to perform a GET against the default /sample/hello service added to my Nexus instance. The best part is that we have achieved everything you see above with absolutely zero code, Maven Archetypes really do simplify things for you :)

curl-example