Custom plugin – mantis-maven-plugin

In order to deepen all the concepts described earlier, we will expose a real-world experience. We will build up a plugin for automating the publication process of our transportation project.

Referring to the problem introduced in the beginning of this chapter, we move forward to its implementation.

Custom plugin implementations

In order to resolve this automation problem, we implemented a plugin named mantis-maven-plugin. Whenever a build is performed, our plugin queries for the release version of project ID that is passed through configurations. Once it resolves the project, it gets all the resolved issues related to the project and marks them as released in that build version.

Our plugin performs all these operations by means of the release_structure.xml file.

If the project ID was not present, then get projectId as the default value.

The mantis-maven-plugin architecture is quite simple. The main plugin goal is implemented by MarkResolved.java. This class simply retrieves all the parameters needed for a correct execution.

The IssueInfo.java class extends the MarkResolved functionalities to implement the second goal.

The last goal for our plugin is implemented in HelpMojo.java. As you probably imagine, this class has been generated through maven-plugin-plugin using the helpmojo goal.

In the following code snippet, we can see how all the concepts that were exposed earlier were implemented.

In order to accomplish the database update, we implemented a class named MySqlAccess.java. Such a class deals with database connections and performs update operations. Since these functionalities have been implemented through standard libraries, we won't complicate this class.

Instead, we find more interesting DataReader.class. This class, used by getProjectVersion to get XML data, uses utility classes from the org.codehaus.plexus.util package. As you remember, this is the same package used by AbstractMojo:

@Mojo(name = "mark-resolved", defaultPhase = LifecyclePhase.PACKAGE, requiresOnline = true, requiresProject = true, threadSafe = true)
public class MarkResolved extends AbstractMojo {

  @Parameter( property = "basedir", required = true )
  protected File basedir;
  
  @Parameter(property="rsName", required = true)
  protected String rsName;

  @Parameter(required = true)
  protected String jdbcDriver;

  @Parameter(property="projectId", required = true, defaultValue="${project.artifactId}")
  protected String projectId;
  
  @Parameter(property="projectVersion", required=true, defaultValue="${project.version}")
  protected String projectVersion;
  
  @Parameter(property="projectDescription", defaultValue="${project.description}")
  protected String projectDescription;

  @Parameter(property="databaseUrl", required = true)
  protected String databaseUrl;
  
  @Parameter(required=true, alias="dbUserName")
  protected String dbUser;
  
  @Parameter(required=true, alias="dbPassword")
  protected String dbPswd;
  
  @Parameter(required=true, alias="databaseName")
  protected String dbName;
  
  @Component(role=MavenProject.class)
  protected MavenProject projectArtifact;
  
  protected MySqlAccess mySqlAccess;
  
  protected DataReader dataReader;
  
  protected Log log;

  public void setDbUserName(String dbUserName) {
    this.dbUser = dbUserName;
  }

  public void setDbPassword(String dbPassword) {
    this.dbPswd = dbPassword;
  }


  public void setDatabaseName(String databaseName) {
    this.dbName = databaseName;
  }
  
  public void execute() throws MojoExecutionException {
    String projectV = null;
    log = getLog();

    log.info("base dir :: " + basedir);
    
    log.info("Jdbc Driver :: " + jdbcDriver);

    log.info("urldb passed :: " + databaseUrl);

    log.info("projectId :: " + projectId);
    
    try {
      
      projectV = getProjectVersion();
      
      if (projectV != null && !projectV.isEmpty()) {
        log.info("Current module version to update is :: " + projectV);
        
        // Start part with db management
        mySqlAccess = new MySqlAccess(dbUser, dbPswd, databaseUrl, dbName, jdbcDriver);

        mySqlAccess.updateStatus(projectId, projectV);

        
      }
      
      
    } catch (XmlPullParserException e) {
      throw new MojoExecutionException("Error on parsing xml file for version to update");
      
    } catch (IOException e) {
      throw new MojoExecutionException("Error on accessing xml file for version to update ");
      
    } catch (SQLException e) {
      throw new MojoExecutionException("Error on mantis database execution");
      
    } catch (ClassNotFoundException e) {
      throw new MojoExecutionException("Error on instantiation for driver class");
    }

  }
  
  
  protected String getProjectVersion() throws XmlPullParserException, IOException {
    String prjVersion = null;
    File xmlReleaseStructure = null;
    
    if (this.projectVersion == null || this.projectVersion.isEmpty()) {
      if (basedir != null && (projectVersion == null || projectVersion.isEmpty())) {
        xmlReleaseStructure = new File(basedir.getAbsolutePath() + File.separator + rsName);
        
        if (xmlReleaseStructure.exists()) {
          dataReader = new DataReader();
          prjVersion = dataReader.getVersionFromXml(xmlReleaseStructure, projectId);
          
        }
      }      
    } else {
      prjVersion = this.projectVersion;
      
    }
    
    return prjVersion;
  }
  
}

As mentioned earlier, we use the release_structure.xml file to get the information about the project name and version. The file has the following structure:

<releaseStructure>
  <module>
    <name>test</name>
    <version>2.4</version>
  </module>
</releaseStructure>

Since a software module can be composed of several submodules, the XML structure allows more than one occurrence for the module tag. This structure allows us to get data in a simple way through the DataReader class functionalities:

import java.io.File;
import java.io.IOException;

import org.codehaus.plexus.util.ReaderFactory;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;

public class DataReader {
  
  public String getVersionFromXml(File xmlReleaseStructure, String projectId) throws XmlPullParserException, IOException {
    String version = null;
    Xpp3Dom xmlDom = Xpp3DomBuilder.build( ReaderFactory.newXmlReader( xmlReleaseStructure ) );
    Xpp3Dom[] modules = xmlDom.getChildren("module");
    
    for (Xpp3Dom child : modules) {
      String nameModule = child.getChild("name").getValue();
      
// If name module isn't null and not empty then check for 
// searching name
      if (nameModule != null && !nameModule.isEmpty()) {
        if (projectId.equalsIgnoreCase(nameModule)) {
          // If we found correct module, then get version
          version = child.getChild("version").getValue();
          
          break;
        }
      }
    }    
    return version;
  }
    }
..................Content has been hidden....................

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