5 Useful Maven2 Plugins
In this blog we will talk about some common problems faced while building, deploying and releasing java based projects in a maven build environment and also explain solutions for them using different maven plugin(s). This post will cover usage of the following maven plugin(s): -

Problem: Overlay ZIP or WAR file on another WAR source.
Description: We have a maven project with some artifact dependencies, classes and resources which need to be bundled together into a WAR file. The artifact dependencies to be bundled together are: - axis2 AAR file (Axis Archive), a zipped folder and another WAR file.
Solution: Use maven WAR plugin with overlays. Add below plugin configuration to the project POM file.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1-beta-1</version>
<configuration>
<warName>FinalProject</warName>
<overlays>
<!--Overlay an AAR file inside the generated WAR file -->
<overlay>
<groupId>com.test.services</groupId>
<artifactId>test-services</artifactId>
<type>aar</type>
<targetPath>WEB-INF/services</targetPath>
</overlay>
<!--Overlay a ZIP file and place extracted content under WEB-INF/knowledge -->
<overlay>
<groupId>com.test.knowledge</groupId>
<artifactId>test-knowledge</artifactId>
<type>zip</type>
<targetPath>WEB-INF/knowledge</targetPath>
</overlay>
<!--Overlay a WAR file and exclude the .jpg in final war -->
<overlay>
<groupId>com.example.project</groupId>
<artifactId>projectdependency</artifactId>
<excludes>
<exclude>images/sampleimage-dependency.jpg</exclude>
</excludes>
</overlay>
</overlays>
</configuration>
</plugin>
Overlays are applied in the order in which they are defined in the overlays configuration. By default, the source of the project (a.k.a the current build) is added first (e.g. before any overlay is applied). The current build is defined as a special overlay with no groupId, artifactId. If overlays need to be applied first, simply configure the current build after those overlays.
Problem: Ensure build breaks if code coverage < threshold.
Description: Ensure that during each build code coverage is above the required threshold value, if not then the maven build fails.
Solution: Use Maven cobertura plugin.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.4</version>
<configuration>
<instrumentation>
<excludes>
<exclude>com/project/test/TestMain.class</exclude>
</excludes>
</instrumentation>
<check>
<totalLineRate>85</totalLineRate>
</check>
<formats>
<format>html</format>
<format>xml</format>
</formats>
</configuration>
<executions>
<execution>
<id>clean</id>
<phase>pre-site</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
<execution>
<id>instrument</id>
<phase>install</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
Problem: Generate WSDL from a Java class at build time.
Solution: Use Maven Java2WSDL plugin. Below configuration creates a TestProject.wsdl from the class com.test.service.TestWebService.
<plugin>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-java2wsdl-maven-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>generate web service wsdl</id>
<goals>
<goal>java2wsdl</goal>
</goals>
<configuration>
<serviceName>TestService</serviceName>
<className>com.test.service.TestWebService</className>
<targetNamespace>http://www.test.com/TestProject</targetNamespace>
<schemaTargetNamespace>http://www.test.com/TestProject</schemaTargetNamespace>
<elementFormDefault>qualified</elementFormDefault>
<package2Namespace>
<property>
<name>com.test.domain</name>
<value>http://www.test.com/TestProject</value>
</property>
</package2Namespace>
<outputFileName>target/generated-resources/TestProject.wsdl</outputFileName>
</configuration>
</execution>
</executions>
</plugin>
NOTE: The plugin will be invoked automatically in the generate-sources phase. You can also invoke it directly from the command line by running the command.
mvn java2wsdl:java2wsdl
Problem: Generate web service stub from the wsdl file.
Solution: Use Maven WSDL2Code plugin. This plugin takes as input a WSDL and generates client and server stubs for calling or implementing a Web service matching the WSDL.
<!--
Generate Java code for the Admin web service (target/wsdl/test.wsdl).
Places result in target/generated-sources/axis2/wsdl2code/src
-->
<plugin>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-wsdl2code-maven-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<goals>
<goal>wsdl2code</goal>
</goals>
<configuration>
<packageName>com.test.wsclient</packageName>
<wsdlFile>${project.build.directory}/wsdl/test.wsdl</wsdlFile>
<syncMode>sync</syncMode>
</configuration>
</execution>
</executions>
</plugin>
NOTE: The plugin will be invoked automatically in the generate-sources phase. You can also invoke it directly from the command line by running the command.
mvn wsdl2code:wsdl2code
Problem: Releasing project artifacts and branching.
Description: Release the project artifacts for release 1.0 into local repository and create a new branch out of trunk for bug fixes on release 1.0. All new development for release 2.0 will be done on trunk.
Solution: Use maven release plugin.
Add the following configuration in the parent projects POM file.
<scm>
<!--SCM connection URL to checkout from -->
<connection>scm:svn:svn://svn/trunk/java/testproject/root</connection>
<developerConnection>scm:svn:svn://svn/trunk/java/testproject/root</developerConnection>
</scm>
<repositories>
<repository>
<id>nexus</id>
<name>Nexus maven repo at HI</name>
<url>http://build-cordys:8080/nexus/content/groups/public</url>
</repository>
</repositories>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.0-beta-9</version>
<configuration>
<!--Goals to execute while release preparation -->
<preparationGoals>clean install</preparationGoals>
</configuration>
</plugin>
In order to perform the release and then create a branch we run the following commands in sequence.
mvn release:prepare
mvn release:prepare executes the following task: -
- Check that there are no uncommitted changes in the sources
- Check that there are no SNAPSHOT dependencies
- Change the version in the POMs from x-SNAPSHOT to a new version (you will be prompted for the versions to use)
- Transform the SCM information in the POM to include the final destination of the tag
- Run the project tests against the modified POMs to confirm everything is in working order
- Commit the modified POMs
- Tag the code in the SCM with a version name (this will be prompted for)
- Bump the version in the POMs to a new value y-SNAPSHOT (these values will also be prompted for)
- Commit the modified POMs
mvn release:perform
mvn release:perform executes the following tasks: -
- Checkout from an SCM URL with optional tag
- Run the predefined Maven goals to release the project (by default, deploy site-deploy
In order to create a new branch from the trunk we run the following commands in sequence.
mvn release:branch -DbranchName=<branchName>
mvn release:branch executes the following tasks: -
- Check that there are no uncommitted changes in the sources
- Change the version in the POMs if you want to change it in the branch (you will be prompted for the versions to use)
- Transform the SCM information in the POM to include the final destination of the tag
- Commit the modified POMs
- Tag the code in the SCM as a new branch with a version name (this will be prompted for)
- Bump the version in the POMs if you want to change it to a new value y-SNAPSHOT (these values will also be prompted for)
- Commit the modified POMs
Filed under: Build and Release, Java2WSDL, Maven, Maven Plugins, WSDL2Code



