I created this blog post to share some interesting facts that I discovered about
Maven plugins with you, written in form of FAQ. After reading this post, you’ll
understand:
How to create a new Maven project from command line?
How does Maven add default plugins to my project?
What happens if declaring plugin in plugins?
What happens if declaring plugin in pluginManagement?
Different configuration in each sub-module
1. How to create a new Maven project from command line?
2. How does Maven add default plugins to my project?
You might already notice that there’s no plugin declared in this XML file.
However, is there any plugin enabled when running any Maven command? In order to
check this, we need to check the effective POM. This can be achieved by using
mvn help:effective-pom
:
Actually, all the effective plugins are generated based on
Plugin Bindings for default Lifecycle Reference.
For example, our project my-app is a project with jar packaging. So the
plugin bindings are the following as described in the page above:
That’s why you don’t see them in the project’s POM, but only in the effective
POM. To prove the matching of plugin maven-jar-plugin between the
effective POM and the XML description from the Maven documentation, grep it:
pluginManagement: The maven-jar-plugin:3.0.2 is declared in plugin
management in our POM, so it is naturally declared in the same way in
effective POM.
plugins: Maven needs to bind the plugin maven-jar-plugin to jar
packaging. When no plugin version declared explicitly in the plugins
section, the version defined in pluginManagement is applied (inherited).
Therefore, the version and configurations are managed by plugin management.
And usage is defined by plugins of each Maven module.
Define define skipIfTrue in partA, and define nothing in partB.
<!-- ... --><artifactId>my-app-partA</artifactId><name>My App - Part A</name><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-jar-plugin</artifactId><configuration><skipIfEmpty>true</skipIfEmpty></configuration></plugin></plugins></build>
Now run the jar:jar goal:
my-app $ mvn clean jar:jar
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] My App - Parent
[INFO] My App - Part A
[INFO] My App - Part B
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building My App - Parent 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ my-app ---
[INFO]
[INFO] --- maven-jar-plugin:3.0.2:jar (default-cli) @ my-app ---
[WARNING] JAR will be empty - no content was marked for inclusion!
[INFO] Building jar: /Users/mincong/Desktop/myProject/my-app/target/my-app-1.0-SNAPSHOT.jar
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building My App - Part A 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ my-app-partA ---
[INFO]
[INFO] --- maven-jar-plugin:3.0.2:jar (default-cli) @ my-app-partA ---
[INFO] Skipping packaging of the jar
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building My App - Part B 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ my-app-partB ---
[INFO]
[INFO] --- maven-jar-plugin:3.0.2:jar (default-cli) @ my-app-partB ---
[WARNING] JAR will be empty - no content was marked for inclusion!
[INFO] Building jar: /Users/mincong/Desktop/myProject/my-app/partB/target/my-app-partB-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] My App - Parent .................................... SUCCESS [ 0.627 s]
[INFO] My App - Part A .................................... SUCCESS [ 0.006 s]
[INFO] My App - Part B .................................... SUCCESS [ 0.042 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.091 s
[INFO] Finished at: 2017-11-07T22:44:17+01:00
[INFO] Final Memory: 22M/981M
[INFO] ------------------------------------------------------------------------
You can see that:
Module “Part A” does not raise warning (<skipIfTrue>true</skipIfTrue>)
Module “Part B” raises a warning (<skipIfTrue>false</skipIfTrue>)
This is because Maven JAR plugin has been overridden in part A, but not part B.