Maven at its core is a plugin execution framework. All work is done by plugins. Looking for a specific goal to execute? This page lists the core plugins and others. In this section, you will learn what are maven plugins, how to use and configure them.
Basically, there are 2 types of plugins, build plugins and reporting plugins.
1. Build plugins are executed during the build and they should be configured in the element from the POM.
2. Reporting plugins are executed during the site generation and they should be configured in the element from the POM.
As mentioned above, maven uses plugins to perform every action, which means there is a plugin for packaging
, there is a plugin for the goal that you execute mvn compile
, mvn build
etc.
- The complete list of available maven plugins is listed on the official Site.
Depending on whether the plugin is build
or reporting
, it is configured in the pom.xml accordingly. Will look into pom.xml of the example that we created earlier.
... <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build>
Maven plugin configuration
Maven plugin configuration is nothing but maven configuration, as every task in maven is performed by the plugins. You will learn various ways you can customize the plugins default behavior.
1. Passing parameters in maven
I will make use of the same project for reference. Ty running mvn clean install
to rebuild the app. You will notice that mvn test
(unit test cases) also gets executed by default. So if you want to customize the default behavior to skip the test cases, you can run the mvn clean install -DskipTests
.
Custom parameters are passed using
-D
e.gmvn install -DskipTests
When you run mvn clean install
inside awesome-maven-examples/spark-api-example
.
Now let’s skip the test cases.
mvn clean install -DskipTests
Maven uses surefire plugins for running Unit test cases. Please refer the official guide for more details.
2. Adding plugin dependencies in Maven
Occasionally, you may need to configure a plugin to use specific versions of dependencies. You can define these dependencies under a dependencies element under plugin. When the plugin executes, it will execute with a classpath that contains these dependencies.
<plugin> <groupId>com.agilejava.docbkx</groupId> <artifactId>docbkx-maven-plugin</artifactId> <version>2.0.9</version> <dependencies> <dependency> <groupId>docbook</groupId> <artifactId>docbook-xml</artifactId> <version>4.5</version> </dependency> <dependency> <groupId>org.apache.fop</groupId> <artifactId>fop-pdf-images</artifactId> <version>1.3</version> </dependency> ... </dependencies> </plugin>
3. Setting Global Plugin Parameters
You can set a value for a plugin configuration parameter in pom.xml
as shown below. Unless this configuration is overridden by a more specific plugin parameter, Maven will use the values defined directly under the plugin element for all goals which are executed in this plugin.
It is a good practice to set the default values in
<configuration>
and then override using-D
if needed.
... <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> ...
4. Setting Execution Specific Parameters
You can configure plugin parameters for specific executions of a plugin goal. Check the below example from undertow 2.0/pom.xml.
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-checkstyle-plugin</artifactId> <executions> .... <execution> <id>check-style-java9</id> <phase>compile</phase> <goals> <goal>checkstyle</goal> </goals> <configuration> <sourceDirectories> <sourceDirectory>${java9.sourceDirectory}</sourceDirectory> </sourceDirectories> </configuration> </execution> </executions> .... </plugin>
5. Setting Default Command Line Execution Parameters.
Maven 2.2.0 onwards, you can supply configuration parameters for goals which are executed from the command-line.
Default config parameters are supplied to the execution id –
default-cli
.
<plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <appendAssemblyId>false</appendAssemblyId> </configuration> <executions> <execution> <id>assemble-binary</id> <phase>package</phase> <goals> <goal>single</goal> </goals> <configuration> <descriptors> <descriptor>src/main/assembly/bin.xml</descriptor> </descriptors> </configuration> </execution> <execution> <id>default-cli</id> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> </execution> </executions> </plugin>
There are many other ways to customize and configure plugins. What you have seen above are the most common ways to achieve this. Officially, the list of maven plugins available can be found on official maven repo.