Maven Plugin Goals

Maven plugin is a collection of one or more goals which do some task or job. It is also known as Mojo - Maven Plain Old Java Object.

For example, Maven uses maven-surefire-plugin to run tests and it comes with two goals.

Maven Plugin Goals - maven-surefire-plugin goals

Similarly, Maven uses maven-compiler-plugin to compile the source and test files and it provides three goals - compiler:compile, compiler:testCompile and compiler:help.

 
 

List Plugin Goals

We can use help goal to list the plugin goals. To list goals of maven-compiler-plugin use following command.

$ mvn compiler:help

We can also refer Plugin Reference documentation to know more about plugin goals and usage. Go to Maven Plugin Directory and select the plugin to visit its documentation page.

Run Plugin Goal

Normally, we use lifecycle phase with mvn command, but it can also execute plugin goals. The usage description of mvn is

mvn [options] [<goal(s)>] [<phase(s)>]

For example, we can directly compile the source with the following command.

$ cd simple-app
$ mvn compiler:compile

To run a goal with mvn, use the format <plugin prefix>:<goal>. In the above example, compiler:compile, the compiler is plugin prefix of maven-compiler-plugin and compile is the goal name. We can get the prefix of all plugins from Maven Plugin Directory.

When we invoke a goal directly, Maven executes just that goal, whereas when we invoke a lifecycle phase all the phases up to that phase are executed. We can see this in action with following example.

$ cd simple-app
$ mvn clean
$ mvn surefire:test

Here, after cleaning the project, we try to run tests with maven-surefire-plugin test goal. It reports that there are no tests to run as Surefire plugin is unable to find any compiled test cases. So, we have to compile source files and test cases before hand.

$ mvn compiler:compile
$ mvn compiler:testCompile
$ mvn surefire:test

Here we execute goals - compiler:compile and compiler:testCompile to compile source files and test cases and once class files are in place we can run surefire:test and tests goes through successfully.

 
 

We showed the above example just to demonstrate the difference between running lifecycle phase and plugins goal. But, in real-life we never do that, instead we invoke lifecycle phase test as mvn test, which executes all the dependent goals for us as explained in Maven Lifecycle Phases

Only in some specific cases, it make sense to invoke plugin goal directly. For example, help goal of plugin is always invoked directly. Apart from help goal, there are some specific plugins whose goals are invoked directly and two such examples are: maven-archetype-plugin and maven-help-plugin. We cover these two plugins bit later.

So far, we depended upon the out-of-the-box functionality of Maven, but we can accomplish much more through the pom.xml (POM). In the next tutorial, we cover the plugin configuration.