Apache Maven: Plugins explanation

In earlier articles we have seen about Maven’s different lifecycles and their various build phases. Now we will extend these articles to understand, what is the role of plugins in executions of various build phases like compile, test, install, deploy, etc

With every build phases, there is always a plugins associated with it and this is applicable for all three built-in lifecycles of Maven

It means every time when we execute any of the build phase, then actually a goal configured in the plugins gets executed at the background implicitly and this is not configured exclusively in the pom.xml

Reason: We need these build phases to get executed in the sequence as defined in the Maven’s lifecycle. So configuring each and every plugins with their goals is quite cumbersome for this default behavior [means maven provides these plugins on-the-fly with their associated goals, when we execute any of the build phase]

For instance, we will consider default lifecycle of Maven and understand what are the plugins associated with every build phase

Syntax: [plugin:name]:[goal-name]

Build Phase plugins:goal
compile compiler:compile
test surefire:test
package jar:jar (depending on the POM element <packaging>)
install install:install
deploy deploy:deploy

Note: Plugins just instructs, actually configured goals in the plugins gets executed

All we learnt so far is the default behavior, now we will move on to understand how we can manually configure <plugins> under <build> section of pom.xml

Sometime, we need extra bit of work to be done in addition to the default behavior. So to achieve this, we can manually configure <plugins> in pom.xml

For instance, we will consider jaxws plugins

<plugin>
	<groupId>org.jvnet.jax-ws-commons</groupId>
	<artifactId>jaxws-maven-plugin</artifactId>
	<version>2.3</version>
	<executions>
		<execution>
			<id>basic</id>
			<phase>generate-sources</phase>
			<goals>
				<goal>wsimport</goal>
			</goals>
		</execution>
	</executions>
	<configuration>
		<keep>true</keep>
		<wsdlDirectory>${basedir}\src\main\resources</wsdlDirectory>
		<wsdlFiles>
			<wsdlFile>com\jaxws\series\top\down\approach\services\BookService.wsdl</wsdlFile>
		</wsdlFiles>
		<sourceDestDir>${basedir}\generated\java\source</sourceDestDir>
		<verbose>true</verbose>
		<target>2.1</target>
	</configuration>
</plugin>

Plugin –> jaxws
Goal   –> wsimport
Phase –> generate-sources

Above plugin in which configured “wsimport” goal reads the WSDL file from location specified and generates jax-ws classes at “generate-sources” build phase of the Maven’s default lifecycle

Note:  With <phase> tag, we can specify at which step of the lifecycle this needs to be executed

References

http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Packaging
http://en.wikipedia.org/wiki/Apache_Maven#Plugins
http://www.benchresources.net/apache-maven-lifecycle-and-basic-operations

 

Read Also:

 

Happy Coding !!
Happy Learning !!

Apache Maven: Skipping unit test using surefire plugin
Apache Maven: Basic Operations