Introduction

This tutorial will show you how to create both war and jar file in a web application using maven. Sometimes we need to create both war and jar files for a web application using maven because some other standalone project needs to include the jar file of the web application.

Prerequisites

JDK 1.8

Maven Configurations

Adding war Plugin

This is a default goal if you mention your application’s packaging type is war then you need not to add this plugin. So it will build war file.

We need to add maven war plugin into pom.xml file to create an war file for the web application.

In the below plugin we have mentioned goal as inplace that indicates the webapp is generated in webapplication’s source directory, which is src/main/resources by default.

The value of <attachclasses> configuration is by default false. We have set this field’s value true to attach classes for the war.

The value of <webXml> specifies the path of the XML file to use.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <executions>
        <execution>
            <phase>package</phase>
            <configuration>
                <attachClasses>true</attachClasses>
                <webXml>${basedir}/src/main/webapp/WEB-INF/web.xml</webXml>
            </configuration>
            <goals>
                <goal>inplace</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Adding jar Plugin

This plugin provides capability to build jars.

The goal jar creates a jar file for your project classes inclusive resources.

The phase represents a stage in lifecycle of the build. The phase:compile compiles the source code of the project.

We need maven jar plugin to create a jar file for the web application.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <executions>
        <execution>
            <id>make-a-jar</id>
            <phase>compile</phase>
            <goals>
                <goal>jar</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Adding install Plugin

The Install Plugin is used during the install phase to add artifact(s) to the local repository. The Install Plugin uses the information in the POM (groupId, artifactId, version) to determine the proper location for the artifact within the local repository.

We need to keep the generated jar file into the build directory. So use the below maven plugin.

<file> indicates the location of the jar file.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-install-plugin</artifactId>
    <executions>
        <execution>
            <id>Add-2-local-repository</id>
            <phase>package</phase>
            <configuration>
                <packaging>jar</packaging>
                <file>${project.build.directory}\${artifactId}-${version}.jar</file>
            </configuration>
            <goals>
                <goal>install-file</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Whole pom.xml

Putting all together, the entire pom.xml file is given below:

<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.roytuts</groupId>
    <artifactId>webapp</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>webapp Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <jdk.version>1.8</jdk.version>
        <junit.version>4.11</junit.version>
        <servlet.version>3.1.0</servlet.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>${servlet.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <finalName>webapp</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${jdk.version}</source>
                    <target>${jdk.version}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <configuration>
                            <attachClasses>true</attachClasses>
                            <webXml>${basedir}/src/main/webapp/WEB-INF/web.xml</webXml>
                        </configuration>
                        <goals>
                            <goal>inplace</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <executions>
                    <execution>
                        <id>make-a-jar</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-install-plugin</artifactId>
                <executions>
                    <execution>
                        <id>Add-2-local-repository</id>
                        <phase>package</phase>
                        <configuration>
                            <packaging>jar</packaging>
                            <file>${project.build.directory}\${artifactId}-${version}.jar</file>
                        </configuration>
                        <goals>
                            <goal>install-file</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

That’s all. Thanks for reading.

Tags:

2 thoughts on “Create both war and jar files using maven

  1. A tip from “test” – When developing in Eclipse, in the maven-jar-plugin, the “compile” phase line caused an annoying syntax error (it’s a issue with the Eclipse maven plugin apparently).
    I got around it by changing the maven-jar-plugin phase to “prepare-package” (I also changed the maven-install-plugin phase to “install” – seems more logical to me)

Leave a Reply

Your email address will not be published. Required fields are marked *