Quantcast
Channel: Crunchify
Viewing all articles
Browse latest Browse all 1037

How to Create a .war file from Eclipse using Maven Plugin? Apache ‘maven-war-plugin’ usage

$
0
0

Maven is pretty amazing, isn’t it? Well – for me at least I love using Maven in my daily Java Development practice. Maven is nothing but a plugin execution framework; all work is done by plugins.

I’ve written number of Maven tutorials on Crunchify mainly on Maven Plugins. Here are few of them:

  1. ‘maven-shade-plugin': How to Create Java+Spring Based executable .jar with all Required Dependencies, Properties and Resources?
    • Maven Share Plugin just creates only one .jar file with all dependencies which you could execute with java -jar fileName.jar
  2. How to Create Build Java Project including all Dependencies Using Maven? ‘maven-resources’, ‘maven-dependency’ & ‘maven-jar’ Plugins
    • This tutorial generates a folder with executable .jar file and all dependencies under the same folder.

Now how can I build WAR with Maven in Eclipse? This Maven Tutorial focuses on maven-war-plugin. Our requirement here is to generate single .war file using Maven which you could run on Apache Tomcat or any other web container.

Let’s get started:

  1. Create Dynamic Web Project in Eclipse, i.e. CrunchifyTutorial
  2. Create simple .java files into your project, i.e. CrunchifyWarUsingMaven.java
  3. Convert Project to Maven project in Eclipse
  4. Add maven-war-plugin to pom.xml file
  5. Run command clean install to generate .war file

Step-1

Create Dynamic Web Project in to Eclipse, i.e. CrunchifyTutorial

Click

  • File >
  • New >
  • Other >
  • Dynamic Web Project >
  • Provide ProjectName (CrunchifyTutorial)
  • Finish

Step-2

Convert Project to Maven Project. Follow these steps: http://crunchify.com/how-to-convert-existing-java-project-to-maven-in-eclipse/

Step-3

Create simple java file named CrunchifyWarUsingMaven.java

NOTE: In your dynamic web project you must have web.xml file which represents your servlet and servlet-mapping. You could use also Simple Spring MVC project too if you want for Step-3 here. If you don’t see web.xml then you could create one by following this tutorial.

package crunchify.com.tutorials;

/**
 * @author Crunchify.com
 *
 */

public class CrunchifyWarUsingMaven {
	public static void main(String[] args) {
		System.out
				.println("Test.. Test by Crunchify.. \nThis is a simple tutorial on how to create .war file using Maven Plugin..");
	}
}

Here is a project structure. You could ignore other files and folders from below diagram icon smile How to Create a .war file from Eclipse using Maven Plugin? Apache maven war plugin usage

Crunchify War File generate tutorial using Maven How to Create a .war file from Eclipse using Maven Plugin? Apache maven war plugin usage

Step-4

Open your pom.xml file and update it with below code. Mainly look at line 7 and 20. We are packaging project as war using <packaging>war</packaging>.

<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/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>CrunchifyTutorial</groupId>
	<artifactId>CrunchifyTutorial</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<build>
		<sourceDirectory>src</sourceDirectory>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.1</version>
				<configuration>
					<source>1.7</source>
					<target>1.7</target>
				</configuration>
			</plugin>
			<plugin>
				<artifactId>maven-war-plugin</artifactId>
				<version>2.4</version>
				<configuration>
					<warSourceDirectory>WebContent</warSourceDirectory>
					<failOnMissingWebXml>false</failOnMissingWebXml>
				</configuration>
			</plugin>
		</plugins>
	</build>
	<dependencies>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>2.5</version>
		</dependency>
	</dependencies>
</project>

Step-5

Now Right Click on Project -> Run-As ->Maven Build (Number 5).

Maven Build Option in Eclipse How to Create a .war file from Eclipse using Maven Plugin? Apache maven war plugin usage

Step-6

Maven clean install on project.

Maven Clean Install Steps For Eclipse Web Project How to Create a .war file from Eclipse using Maven Plugin? Apache maven war plugin usage

Step-7

Checkout your Build result in Eclipse console.

Maven war plugin Building War Console Output How to Create a .war file from Eclipse using Maven Plugin? Apache maven war plugin usage

Step-8

That’s it. Now get your .war file from /target directory and deploy into Apache Tomcat or any other Web Container.

Have anything to add to this article? Please chime in and join the conversion.


The post How to Create a .war file from Eclipse using Maven Plugin? Apache ‘maven-war-plugin’ usage appeared first on Crunchify.
Author: Arpit Shah


Viewing all articles
Browse latest Browse all 1037

Trending Articles