Maven Plugins 👷🏼♂️👷🏼♀️
https://maven.apache.org/plugins/index.html
Maven is effectively a framework that runs plugins. It just knows which plugins to run and when.
Lifecycle Plugins
maven-clean-plugin
- one goal - clean
- remove files generated during build process
- by default removes /target directory from project root and submodule root folders
Can add the clean cycle to another lifecycle by adding the following within the project tag e.g.
This runs the clean:clean plugin goal as part of the initialize phase.
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
<!-- can omit the version number - it will be inherited, but will generate a warning that it's missing -->
<executions>
<execution>
<id>auto-clean</id>
<phase>initialize</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
</plugin>
</plugins>
</build>
...
</project>
Common practice: By hooking the clean plugin into specific phases (like initialize), it can save you from having to type in clean on the command line, e.g.
Instead of mvn clean package
, you can now use mvn package
Instead of mvn clean install
, you can now use mvn install
Effectively this is adding the clean plugin goal into the default lifecycle.
maven-compiler-plugin
- in Default Lifecycle
- two goals - compiler:compile, compiler:testCompile (and a compiler:help goal)
- by default, uses the compiler 'javax.tools.JavaCompiler' - can be configured to use javac
- default source and target language levels are Java 1.6, apache encourages these values to be set
Can change the compiler version in the POM.xml file.
<properties>
...
<java.version>11</java.version>
<!-- The following two lines might also be required -->
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target> ...
</properties>
This will compile to java version 11 - changing this (e.g. to 1.6) will compile the code to a different version.
Resources Plugin
https://maven.apache.org/plugins/maven-resources-plugin/
- in Default Lifecycle
- 3 goals - resources:resources, resources:testResources, resources:copy-resources
- purpose is to copy project resources to output directory (target dir)
Resources normally live in src -> main -> resources and src -> test -> resources
resources:resources copies files in resources into target/classes ready to be packaged into a jar.
Can add an external resource directory by adding the following to POM.xml
<project>
...
<build>
<resources>
<resource>
<directory>[your files here]</directory>
</resource>
</resources>
</build>
...
</project>
This overrides the default resources directory, but you can add multiple resources
<resources>
<resource>
<directory>resource1</directory>
</resource>
<resource>
<directory>resource2</directory>
</resource>
<resource>
<directory>resource3</directory>
</resource>
</resources>
Other things this plugin can be configured for is:
- specifying a character encoding scheme
- filtering
- including and excluding files/folders
Surefire Plugin
- in Default Lifecycle
- one goal: surefire:test (also surefire:help)
- by default supports: JUnit3, JUnit4, JUnit5 and TestNG
- by default includes classes named: Test, Test, Tests, TestCase (can change these)
- also handles POJO tests
Runs tests out of your test directory. Reports are available in target/surefire-reports.
JAR Plugin
- in Default Lifecycle
- two goals: jar:jar, jar:test-jar
- purpose - build jars from compiled artifacts and project resources
- can be configured for custom manifests and to make executable jars
Adding a ClassPath entry to the Manifest
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
...
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
...
</plugin>
</plugins>
</build>
...
<dependencies>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
<version>1.1</version>
</dependency>
</dependencies>
...
</project>
This creates the following manifest
Manifest-Version: 1.0
Created-By: Apache Maven ${maven.version}
Build-Jdk: ${java.version}
Class-Path: plexus-utils-1.1.jar commons-lang-2.1.jar
Creating an Executable JAR
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
...
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>fully.qualified.MainClass</mainClass>
</manifest>
</archive>
</configuration>
...
</plugin>
</plugins>
</build>
...
</project>
Manifest-Version: 1.0
Created-By: Apache Maven ${maven.version}
Build-Jdk: ${java.version}
Main-Class: fully.qualified.MainClass
Class-Path: plexus-utils-1.1.jar commons-lang-2.1.jar
Deploy Plugin
- in Default Lifecycle
- two goals: deploy:deploy (deploys artifacts), deploy:deploy-file (deploys specific files)
- purpose: to deploy project artifacts to remote Maven repositories
- typical done in CI environments (not normally done from a developer workstation)
- configuration is typically part of Maven POM - attributes for deployment are built into the POM
Maven Site Plugin
- in SITE Lifecycle
- 7 goals
- site:site - generate site for project
- site:deploy - deploy site via Wagon
- site:run - run site locally using Jetty as web server
- site:stage - generate site to a local staging location (e.g. for testing)
- site:stage-deploy - deploy site to a remote staging location
- site:attach-descriptor - adds site.xml (site map file used by search engines) to files for deployment
- site:jar - bundles site into a jar for deployment to a repository
- site:effective-site - generates the site.xml file
Can use a variety of formats (including XML and Markdown)
Maven and Source Control
You do not need to check in the following as part of a maven project..
- the target directory
- *.iml file
- the .idea folder
Other Plugins
Reference can be found at: maven.apache.org/ref/3.6.0/plugin-management.html