There is a Mojo in My Dojo (How to write a Maven plugin)
I have been up to my armpits involved using Maven at work. For good number of developers I will hear, “So what.” The difference is that I normally work in environments where I do not have access to the Internet directly. So when I say I have been using Maven a lot, it means something.
Dependency Hell
To be fair, I have been using Maven casually in my examples. I have found it to be more convenient to get downloads of dependencies and avoid “dependency hell.” The situation where I have to download a library for a library that i am using. For example, one has to download Hamcrest to use JUnit. At home, put in the dependency for JUnit and Maven downloads Hamcrest for me because it is a dependency of JUnit. If there was a dependency of Hamcrest, Maven would download that too. When I am at work, I need to research what dependencies JUnit has and then research what dependencies the dependencies have. I have avoided using libraries because of this very situation.
Situations Change
The change is because I am using Spring Roo at work. Roo uses Maven to manage Spring dependencies that it needs to incorporate. Because of this change, I set up a Nexus server on the development network and started the process of bringing over the dependencies from the Internet to the development network. This got me learning about Maven.
What I Learned about Maven
After reading two books, Introducing Maven and Maven Build Customization, I got a pretty good idea about Maven and how to create the subject of this post. I can go on and on about what I learned but I will keep it focused to what is needed to learn about Maven plugins. I do assume one has seen a pom file and run a few Maven builds from now on in the post. If one has not, purchase the books I read or go to http://maven.apache.org first.
Maven is Plugin Rich
Maven is based on a plugin architecture. Anything that does something in Maven is a plugin. That goes from core functionality like compiling to creating sites. As one can imagine, every plugin has certain things in common.
Maven is Package, Lifecycle, Phase and Goal Oriented
Maven is known for building something into a packaged item of some sort, for example a jar file. That is obvious, that is one of the first lines of a pom file. What may not be known is that there is a series of “phases” or “lifecycle” that happen to accomplish building the package (see what I did there). In fact, one of those phases is named “package.” The list of default phases in a lifecycle are as follows:
- validate
- generate-sources
- process-sources
- generate-resources
- process-resources
- compile
- process-classes
- generate-test-sources
- process-test-sources
- generate-test-resources
- process-test-resources
- test-compile
- process-test-classes
- test
- prepare-package
- package
- pre-integration-test
- integration-test
- post-integration-test
- verify
- install
- deploy
There is a lot of stuff going on in a Maven build! All of that is being run by some sort of plugin. Every plugin is made of goals which can be set to run at a certain phase of the lifecycle. For example, the maven-jar-plugin’s jar goal is set to run in the package phase.
The Making of a Plugin
Now that one has a more in-depth knowledge of what is going on in a build, it is time to explain what is needed to create a Maven plugin.
Plugins Are Full of Mojos
What is a mojo? Mojo is short for Maven plain Old Java Objects. It is the smallest unit of a plugin Maven recognizes. All plugins are made of mojos. Each mojo is associated to a goal. So for a plugin to have multiple goals, it needs multiple mojos. The example I will show only has one mojo sadly but the example will also show best practices in testing a plugin.
Best Practices are the Only Practices Allowed
See what I did there to tie in with the Dojo deal in the title? There is naming convention, unit testing and integration testing involved with writing plugins if one is inclined. The naming convention is the most important so
- You don’t bust an Apache trademark
- Others know that one made a plugin.
What is in a Name?
The naming convention for Apache’s plugins are maven-<title>-plugin. For example, the jar plugin is named maven-jar-plugin. For everyone else, the naming convention is <title>-maven-plugin. For example, the example I created is named reminder-maven-plugin. Another example that used when making this post is Spring Boot‘s plugin and it is named spring-boot-maven-plugin. The source code to Spring Boot is here. I forked it so I could peruse and abuse the code. My fork can be found here. If one wants to abuse it together, please fork my copy and send me a pull request when your particular piece of abuse is done. Anyway if one uses Apache’s naming convention, it is a trademark infringement. You have been warned.
Unit Testing
Automated unit and integration testing is important too. Unit testing follows a little different directory pattern than normal unit testing so hang on.
The directory structure when doing a unit test of a plugin is
Notice that all of the test directories are organized under the test directory. What one is making is a little version of a project that will be using the plugin. Under the test resources directory is a unit directory followed by the name of the unit in the child directory. The goal is to test a single mojo at a time. Since my example only has one mojo, I only set up one test. There are other differences than the directory setup but that will be covered in the example section.
Integration Testing
I found that this testing will teach one the most about one’s particular plugin and how it works. The goal is to test a certain situation as if it was part of an actual project build. When I mean actual project build, I mean that there is even a temporary repository just for the integration build. After reading about how to set up the tests, I borrowed heavily from spring-boot-maven-plugin’s integration test setup and mini pom files. OK, I copied some of the files over to my example code. Just informing one that Spring Boot did it right. Just be safe a clone read only or fork their code just to be safe. The directory structure is displayed below.
The integration tests are located not under the test directory but directly underneath the src directory in the it directory. I could have done more integration tests but one is good enough for now.
Example
The example plugin was inspired by the fact that I am absent minded and need to be reminded of everything I do. I thought of creating a wash-the-dogs-reminder-maven-plugin but I decided on a plain reminder-maven-plugin because then I could use it to remind me of anything I needed to do.
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.darylmathison</groupId> <artifactId>reminder-maven-plugin</artifactId> <packaging>maven-plugin</packaging> <version>1.0-SNAPSHOT</version> <name>reminder-maven-plugin Maven Mojo</name> <url>http://maven.apache.org</url> <properties> <mavenVersion>3.2.1</mavenVersion> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </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> <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-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <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>run-its</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> <cloneClean>true</cloneClean> <pomIncludes> <pomInclude>*/pom.xml</pomInclude> </pomIncludes> <addTestClassPath>true</addTestClassPath> <postBuildHookScript>verify</postBuildHookScript> <localRepositoryPath>${project.build.directory}/local-repo</localRepositoryPath> <settingsFile>src/it/settings.xml</settingsFile> <goals> <goal>clean</goal> <goal>compile</goal> <goal>package</goal> </goals> </configuration> <executions> <execution> <id>integration-test</id> <goals> <goal>install</goal> <goal>run</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </profile> </profiles> </project>
As one can see, quite a few plugins and dependencies are needed to build one. There is one dependency of note here. This is the version of Junit. The version needs to be 3.8.1. This is because Maven extended the TestCase class to make it easier to unit test. That will be seen soon. Two plugins are of note, one is the maven-plugin-plugin and the other is the maven-invoker-plugin. The maven-plugin-plugin automates the process of creating a help goal for one’s plugin. The maven-invoker-plugin is used in the integration tests. Its function is to run Maven projects which is handy if one is running in a test pom.
ReminderMojo.java
package com.darylmathison; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoUnit; @Mojo(name = "remind", defaultPhase = LifecyclePhase.PACKAGE, requiresOnline = false, requiresProject = true, threadSafe = false) public class ReminderMojo extends AbstractMojo { @Parameter(property = "basedir", required = true) protected File basedir; @Parameter(property = "message", required = true) protected String message; @Parameter(property = "numOfWeeks", defaultValue = "6", required = true) protected int numOfWeeks; public void execute() throws MojoExecutionException { File timestampFile = new File(basedir, "timestamp.txt"); getLog().debug("basedir is " + basedir.getName()); if(!timestampFile.exists()) { basedir.mkdirs(); getLog().info(message); timestamp(timestampFile); } else { LocalDateTime date = readTimestamp(timestampFile); date.plus(numOfWeeks, ChronoUnit.WEEKS); if(date.isBefore(LocalDateTime.now())) { getLog().info(message); timestamp(timestampFile); } } } private void timestamp(File file) throws MojoExecutionException { try(FileWriter w = new FileWriter(file)) { LocalDateTime localDateTime = LocalDateTime.now(); w.write(localDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)); } catch (IOException e) { throw new MojoExecutionException("Error creating file " + file, e); } } private LocalDateTime readTimestamp(File file) throws MojoExecutionException { try(FileReader r = new FileReader(file)) { char[] buffer = new char[1024]; int len = r.read(buffer); LocalDateTime date = LocalDateTime.parse(String.valueOf(buffer, 0, len)); return date; } catch(IOException ioe) { throw new MojoExecutionException("Error reading file " + file, ioe); } } }
This is the only Mojo in the plugin and as one can find, it is very simple but shows some of the cool features the mojo api provides. The class annotation defines that the name of the goal is “remind” and that it is not thread safe. It also defines the default phase is the package phase. The last thing I will mention is that any member variable can become a parameter. This becomes a parameter for the plugin of a goal.
ReminderMojoTest
package com.darylmathison; import org.apache.maven.plugin.testing.AbstractMojoTestCase; import java.io.File; /** * Created by Daryl on 3/31/2015. */ public class ReminderMojoTest extends AbstractMojoTestCase { @Override protected void setUp() throws Exception { super.setUp(); } @Override protected void tearDown() throws Exception { super.tearDown(); } public void testJustMessage() throws Exception { File pom = getTestFile("src/test/resources/unit/reminder-mojo/pom.xml"); assertNotNull(pom); assertTrue(pom.exists()); ReminderMojo myMojo = (ReminderMojo) lookupMojo("remind", pom); assertNotNull(myMojo); myMojo.execute(); } }
Here is a basic unit test case of a mojo. The test class extends AbstractMojoTestCase to gain some functionality like getTestFile and lookupMojo. The following is the test pom file.
Unit Test 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.darylmathison.test</groupId> <artifactId>reminder-maven-plugin-test-reminder</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>reminder-maven-plugin Maven Mojo</name> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>com.darylmathison</groupId> <artifactId>reminder-maven-plugin</artifactId> <version>1.0-SNAPSHOT</version> <configuration> <basedir>target/test-classes/unit/reminder-mojo</basedir> <message>Wash the doggies</message> </configuration> </plugin> </plugins> </build> </project>
Just a mini version of the main pom file that defined the plugin.
Integration Test
This deserves its own section because it is really a separate Maven project within a Maven project. The main focus of this exercise is to see what the plugin will do and not anything else. The sample application is simple and just there for the Maven project to build. The other thing to note is that the pom file uses some filtering to match the groupId, artifactId, and version of the main plugin pom.
Pom File
<?xml version="1.0" encoding="UTF-8"?> <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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.darylmathison.it</groupId> <artifactId>new-timestamp</artifactId> <version>0.0.1.BUILD-SNAPSHOT</version> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <build> <plugins> <plugin> <groupId>@project.groupId@</groupId> <artifactId>@project.artifactId@</artifactId> <version>@project.version@</version> <executions> <execution> <id>blah</id> <goals> <goal>remind</goal> </goals> </execution> </executions> <configuration> <message>Wash the doggies</message> </configuration> </plugin> </plugins> </build> </project>
SampleApp
package java.test; /** * Created by Daryl on 4/5/2015. */ public class SampleApp { public static void Main(String[] args) { System.out.println("out"); } }
Verify.groovy
System.out.println(basedir); def file = new File(basedir, "timestamp.txt"); return file.exists();
The verify script is to make sure that the plugin does what it intended to do. It just checks for the existence of the timestamp.txt file because the plugin creates one when it cannot find a timestamp file. Maven checks for a true or false output of the verify script.
Conclusion
Wow! I covered a lot in this post. I went and gave an example of how to create a Maven plugin. I also showed how to test that plugin using best practices. I got the information between two books and an example of a real on going open source project. The example code is hosted on github here. This represents the first example in my new example home.
References
Reference: | There is a Mojo in My Dojo (How to write a Maven plugin) from our JCG partner Daryl Mathison at the Daryl Mathison’s Java Blog blog. |
in your unit test you do not define a goal. In your integration test you do. Why is that?
In the unit test the pom file is just to make sure that the plugin can be read from a pom file and then is called within the unit test so it doesn’t need a goal because the maven does not run the plugin. In the integration test maven actually runs the plugin and so the goal is needed.
It’s missing many many maven dependencies from the pom file.
This project wil not run as-is in the tutorial.
I have used this project several times to create other plugins. If you feel that it needs improving, please fork my github project at https://github.com/darylmathison/reminder-maven-plugin-example and we can discuss any changes it needs.
Really well-written tutorial. Thanks !