Plugins in Maven and POM.xml

JavaMavenMaven 3pom.xmlMaven Plugin

Java Problem Overview


I just started using Maven and I read that plugins are additional components that can be used.
A typical structure of pom.xml file is

<project>
  <groupId>org.koshik.javabrains</groupId>
  <artifactId>JarName</artifactId> (A fldernamed JarName was created) 
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>JarName</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

Question: Where should I insert a plugin tag? such as the following:

<plugin>
  <groupId>org.jibx</groupId>
  <artifactId>jibx-maven-plugin</artifactId>
  <version>1.2.4</version>
  <executions>
    <execution>
      <goals>
        <goal>bind</goal>
      </goals>
    </execution>
  </executions>
</plugin>

Before the dependency or after the dependency tag? Does it matter?

Java Solutions


Solution 1 - Java

<project>
	<groupId>org.koshik.javabrains</groupId>
	<artifactId>JarName</artifactId> (A fldernamed JarName was created) 
	<version>1.0-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>JarName</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<build>
		<plugins>
			<plugin>
				<groupId>org.jibx</groupId>
				<artifactId>jibx-maven-plugin</artifactId>
				<version>1.2.4</version>
				<executions>
					<execution>
						<goals>
							<goal>bind</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
</project>

You can also place plugins in the <build> section of <profile> if you use maven profiles. The order doesn't matter.

Solution 2 - Java

A late clarification on two important points

Where to place plugin
A plugin should indeed be added in most of the cases within the build/plugins section, however there is an important difference between placing it within plugins against placing it within pluginManagement/plugins.

This misunderstanding is often the cause of a non invoked plugin in Maven or an harder troubleshooting:

  • Plugins under build/plugins are directly part of the default Maven build, if they specify an execution or if they configure something for the default build (see below)

  • Plugins under build/pluginManagement/plugins are not necessarely part of the default Maven build, that is, is a management, it's an hint to maven: it you happen to use this plugin, then please use the version, the configuration, the executions I specify here, in this management.

    But what happen to use means? Means: if the same plugin is also present in the build/plugins section, then apply this management (and only then it will be effective); or if the plugin is invoked by default by Maven, then also apply it.

But how is a plugin invoked by default? That's part of the main philosophy behind maven: convention over configuration. By convention, when you specify a certain packaging (default jar, but it can be war for example), you want certain plugins to be invoked. To build a jar, by default invoke the maven-jar-plugin; to build a war, by default invoke the maven-war-plugin and so on. So, if you specify a plugin configuration in the build/pluginManagement/plugin for a plugin which has a default binding to the Maven build, then it will be also be used.

Ordering
Concerning the ordering of sections within the pom.xml file, a further clarification is required: it's indeed irrelevant in most of the cases, however the order of plugin element wihtin the build/plugins section may be important. Since Maven 3.0.3 (MNG-2258), different plugin executions attached to the same Maven phase will be invoked in their order of declaration in the pom.xml file. That is, ordering is important in this case, since it may affect the behavior of the build.

Additionally, also order of dependency declarations may affect your build towards Dependency Mediation, that is, the first declared dependency wins in case of conflict against a transitive dependency. So, once again, ordering is important in certain cases.

Last but not least, although ordering is not important for other sections of the pom.xml file, good habit is to follow official Maven recommendations and, as a simplified version, follow this order of declaration:

<project>
  <modelVersion/>
 
  <parent/>
 
  <groupId/>
  <artifactId/>
  <version/>
  <packaging/>
 
  <properties/>
 
  <dependencyManagement/>
  <dependencies/>
 
  <build/>
 
  <reporting/>
 
  <profiles/>
</project>

The sortpom-maven-plugin can also be used to automatically apply this standard ordering, simply invoking the following on the concerned pom.xml file:

mvn com.github.ekryd.sortpom:sortpom-maven-plugin:2.5.0:sort \
     -Dsort.keepBlankLines -Dsort.predefinedSortOrder=recommended_2008_06 

For further reading:

Solution 3 - Java

<plugin>should be placed into <plugins> section which should be placed into <build> or <pluginManagement> section. The order of <dependency> or <build> section doesn't matter.

The full reference about pom.xml is here: http://maven.apache.org/pom.html

Solution 4 - Java

If you want to use the plugin for build you can use the below structure.

<project>
 <build>
  <plugins>
  </plugins>
 </build>
</project>

Solution 5 - Java

You can insert your second snippet anywhere in the pom.xml file between two <plugins> </plugins> tags.

Solution 6 - Java

Sections order in POM doesn't matter. In general, there are build plugins and reporting plugins in Maven. Your case is to use build plugin so you have to put this <plugin> block into <project><build><plugins>... section.

Look at this for some basics about plugins.

Solution 7 - Java

As additional answer for Reporting Plugins (e.g. maven-checkstyle-plugin) there are 2 tags under which plugins can go in pom.xml, under build and reporting.

>Using the reporting Tag VS build Tag > > Configuring a reporting plugin in the or elements > in the pom does NOT have the same behavior! > > mvn site > > It uses only the parameters defined in the > element of each reporting Plugin specified in the element, > i.e. site always ignores the parameters defined in the > element of each plugin specified in . > > mvn aplugin:areportgoal > > It uses firstly the parameters defined in the element > of each reporting Plugin specified in the element; if a > parameter is not found, it will look up to a parameter defined in the > element of each plugin specified in .

Source: https://maven.apache.org/guides/mini/guide-configuring-plugins.html#Using_the_reporting_Tag_VS_build_Tag

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRajeshwarView Question on Stackoverflow
Solution 1 - JavaomnomnomView Answer on Stackoverflow
Solution 2 - JavaA_Di-MatteoView Answer on Stackoverflow
Solution 3 - JavayatulView Answer on Stackoverflow
Solution 4 - JavaSubir Kumar SaoView Answer on Stackoverflow
Solution 5 - Javauser520288View Answer on Stackoverflow
Solution 6 - JavaMichał KalinowskiView Answer on Stackoverflow
Solution 7 - JavaVipinView Answer on Stackoverflow