Maven

Maven War Plugin Example

In this example we are going to see some of the capabilities from the maven war plugin.

Maven is a build automation tool used mainly for java projects from apache.

You can acces to the maven war plugin here.

We are going to see some examples of the capabilities of the maven war plugin.

For this example we use the following technologies:

  • MAC OSX
  • Eclipse Mars.1
  • Maven3
  • JDK 1.8.0_65 64bits
  • Maven war plugin 2.6

1. Introduction

The maven war plugin is called implicitely by the maven lifecycle in the appropiate phase so it is a ‘special’ plugin. We don´t need to define it inside pom.xml it will be downloaded and executed when maven needs it.

Despite that, we can define it inside pom.xml in order to build our project as a war file.

The maven war plugin has some goals defined:

  • war: Default goal. Invoked during the package phase for projects with a packaging of war
  • exploded: This goal creates a exploded web app in a specified directory
  • inplace: This goal is an exploded goal variant which generates a exploded web app inside the web application folder in src/main/webapp
  • manifest: this goal generates a manifest file for this web app

There is no need to define that goal inside pom.xml, as we said before, maven will invoke that goal when the maven lifecycle have to build the war file.

2. Example project

For this example, we are going to use a java project with maven nature that will be packaged as a war file. Eclipse Mars comes with maven support out of the box, so you don´t have to install anything. Our project will look like this:

Initial project
Initial project

At this point, we have an empty maven project. We are going to define the maven war plugin inside pom.xml in order to test the plugin capabilities.

The pom.xml will look like this:

pom:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.javacodegeeks.examples</groupId>
	<artifactId>maven-war-plugin-example</artifactId>
	<version>1.0.0-SNAPSHOT</version>
	<name>Maven war ::  example</name>
	<url>http://maven.apache.org</url>
	<packaging>war</packaging>

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

	<build>
		<plugins>
			<plugin>
				<artifactId>maven-war-plugin</artifactId>
				<version>2.6</version>
			</plugin>
		</plugins>
	</build>
</project>

The project has one dummy class called MyClass, a dummy web.xml file inside src/main/webapp/WEB-INF folder and a hello world index.html file on the root webapp folder (src/main/webapp). Also, the project contains a folder called more_resources with a dummy file called resources.txt.

In the following bullets, we are going to see some of the maven war plugin capabilities applied to this project.

3. Generate a exploded war

The plugin allow you to generate a exploded war as a folder, you can do it running the mvn war:exploded command. After running it, you will see a new folder under target folder with the generated war exploded, this is, as a normal directory with all of its files inside of it.

4. Filtering the war file content

The plugin allow you to filter the war content, you can include/exclude resources from the output war file. The following pom.xml shows how to include some stuffs to the war file

pom:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.javacodegeeks.examples</groupId>
	<artifactId>maven-war-plugin-example</artifactId>
	<version>1.0.0-SNAPSHOT</version>
	<name>Maven war ::  example</name>
	<url>http://maven.apache.org</url>
	<packaging>war</packaging>

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

	<build>
		<plugins>
			<plugin>
				<artifactId>maven-war-plugin</artifactId>
				<version>2.6</version>
				<configuration>
					<webResources>
						<resource>
							<!-- Relative path to the pom.xml directory -->
							<directory>more_resources</directory>
						</resource>
					</webResources>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

After running mvn clean install, inside the generated war structure you will find the resource.txt file in the root folder that comes from the more_resources folder in the example project. This is usefull when we have other resources (like reports or whatever kind of resources needed) and we want to include it inside war file.

You can find more details of how use this feature here.

5. Customizing manifest file

The plugin allow you to control the manifest file, you can include the classpath inside the manifest file for example. This is usefull when the war file is under a more complex structure like an ear file and you want to share the dependencies along several modules.

The following pom.xml shows how to use this feature

pom:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.javacodegeeks.examples</groupId>
	<artifactId>maven-war-plugin-example</artifactId>
	<version>1.0.0-SNAPSHOT</version>
	<name>Maven war ::  example</name>
	<url>http://maven.apache.org</url>
	<packaging>war</packaging>

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

	<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>4.2.4.RELEASE</version>
			<optional>true</optional>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<artifactId>maven-war-plugin</artifactId>
				<version>2.6</version>
				<configuration>
					<archive>
						<manifest>
							<addClasspath>true</addClasspath>
						</manifest>
					</archive>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

In order to test the classpath inclusion in manifest.mf file we have added the spring dependency in pom.xml, after run mvn clean install we can see a manifest.mf file like this:

manifest.mf:

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: fhernandez
Class-Path: spring-core-4.2.4.RELEASE.jar commons-logging-1.2.jar
Created-By: Apache Maven 3.3.3
Build-Jdk: 1.8.0_65

As you can see, the manifest file include a Class-Path: property with the classpath entry based on the dependencies defined in pom.xml in a transitive way.

6. Conclusions

As we have saw the maven war plugin offers some interesting capabilities which we can take advantage of in order to build war files, you can get more details in the link above at the introduction of this example.

7. Download the eclipse project

Download
You can download the full source code of this example here: maven-war-plugin-example.zip

Francisco Hernandez

JEE technologies geek and development engineer. I have over 13 years of experience as software engineer in JEE architectures: Design, development, improvement etc. Currently I work as software architect and consultant. I am mainly involved in projects related to the bank and energy sectors based on Java technologies and Oracle products. I am also very interested in open-source projects
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button