Search

Monday, 23 September 2013

GitHub: test tracking via Maven plugin

Recently we've organized integration with GitHub by retrieving issues content into the files. All this was done in a form of command line utility. In this post I'll extend the functionality to use this solution as Maven plugin.

Building Maven plugin skeleton

The plugin is developped using the following guide. So, we'll create skeleton like:

/**
 * 
 */
package sirius.utils.retriever;

import java.io.IOException;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.*;

/**
 * @author Myk Kolisnyk
 * 
 */
@Mojo(name = "generate",defaultPhase=LifecyclePhase.GENERATE_SOURCES)
public class IssueGetPlugin extends AbstractMojo {

    public void execute() throws MojoExecutionException {
  ;
    }
}
Bus this is just a base. We've created new Maven goal named generate which should be run as a part of generate-sources stage.

Next step is to add properties:

    @Parameter(property = "issueget.user", defaultValue = "")
    private String   userName       = "";

    @Parameter(property = "issueget.password", defaultValue = "")
    private String   password       = "";

    @Parameter(property = "issueget.repository", defaultValue = "Sirius")
    private String   repository     = "";

    @Parameter(property = "issueget.type", defaultValue = "trace")
    private String   outputType     = "";

    @Parameter(property = "issueget.groups", defaultValue = "Test")
    private String groups;

    @Parameter(property = "issueget.output", defaultValue = ".")
    private String   outputLocation = "";
These are actually the parameters which were originally passed to retriever utility in the previous example. And to make picture complete we'll add setters to the above properties:
   /**
     * @param userName the userName to set
     */
    public void setUserName(String userName) {
        this.userName = userName;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @param repository the repository to set
     */
    public void setRepository(String repository) {
        this.repository = repository;
    }

    /**
     * @param outputType the outputType to set
     */
    public void setOutputType(String outputType) {
        this.outputType = outputType;
    }

    /**
     * @param groups the groups to set
     */
    public void setGroups(String groups) {
        this.groups = groups;
    }

    /**
     * @param outputLocation the outputLocation to set
     */
    public void setOutputLocation(String outputLocation) {
        this.outputLocation = outputLocation;
    }
This is the skeleton.

Implementing plugin logic

We've created the base for the plugin. So, now it's time to implement the actions to that. For this purpose we'll fill in the content of execute method:

    public void execute() throws MojoExecutionException {
        String[] args = {
                Program.REPO_NAME,
                this.repository,
                Program.USER_NAME,
                this.userName,
                Program.USER_PASS,
                this.password,
                Program.GROUPS,
                this.groups,
                Program.OUTPUT_TYPE,
                this.outputType,
                Program.OUTPUT_LOCATION,
                this.outputLocation
        };
        try {
            Program.main(args);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
Well, it appears to be a bit more simple than it was thought initially.

Now the plugin is ready to use. We can compile it and publish to some local repository. Then it can be included into any other project as:

 <build>
  <plugins>
   <plugin>
    <groupId>com.github.mkolisnyk</groupId>
    <artifactId>issueget-maven-plugin</artifactId>
    <version>1.0-SNAPSHOT</version>
    <configuration>
     <repository>Sirius</repository>
     <groups>Test;Win32</groups>
     <outputType>cucumber</outputType>
     <outputLocation>src/test/java/org/sirius/server/test/features/win32/controls</outputLocation>
     <goal>generate</goal>
    </configuration>
   </plugin>
  </plugins>
 </build>

Some small tunings

The above example should have some additional settings. Firstly, there's no username and password properties specified. Well, usually, it's all about security purposes to avoid explicit password definition. But we still need them.

Also, at the moment we can invoke this plugin by the following command line:

mvn com.github.mkolisnyk:issueget-maven-plugin:generate
which is a bit inconvenient. So, we should be able to make this goal name shorter.

All the above stuff can be configured at the %MAVEN_HOME%/conf/settings.xml file. There're 2 major settings to be done there:

  1. Update the default list of plugin packages:
      <pluginGroups>
     <pluginGroup>com.github.mkolisnyk</pluginGroup>
      </pluginGroups>
    
  2. Update default profile with issueget login/password information:
     <profile>
      <id>default</id>
      
      <properties>
       <issueget.user>mkolisnyk</issueget.user>
       <issueget.password>****</issueget.password>
      </properties>
     </profile>
      </profiles>
    
After the above settings this plugin will be called as:
mvn issueget:generate
and by default the user credentials will be taken from the profile settings. Thus, you can specify different credentials in the different profiles.

Summary

Now we can generate Cucumber definitions from the Maven. Of course, it's not the final version and it would be improved to load results into multiple locations, to load data from different systems (e.g. JIRA) etc. But the first step is done. The code is available here.

No comments:

Post a Comment