Create Web application using Maven

Maven installation steps

 

Step 1:

Download Maven from maven website. Lets assume maven is installed in c:\apache-maven-3.3.3

Step 2:

Take a note of your java installation path. We’ll assume you have java installed in path c:\java\jdk1.7.0_75

Step 3:

Set JAVA_HOME variable to your Java installation path.

In windows system, you can Right click on computer icon, go to properties -> Advanced System Settings ->Environment variables and add JAVA_HOME variable.

For example, JAVA_HOME = c:\java\jdk1.7.0_75

You can also add it from the command prompt as shown below :

set JAVA_HOME=c:\java\jdk1.7.0_75;

Step 4:

Add Maven installation bin directory to PATH variable.

For this, you can Right click on computer icon, go to properties -> Advanced System Settings ->Environment variables and add the bin directory path to the end of PATH variable.

You can do the following from command prompt for this :

set PATH=%PATH%;c:\apache-maven-3.3.3\bin;

Step 5:

Now run the following command from command prompt:

mvn -version

If you are getting Maven installation information, your setup is good, else check the above steps again.

 

 

Creating Web Application in Maven:

 

Step1 :

In command prompt go to the path where you want to create a web application and run the following :

mvn archetype:generate -DgroupId=com.topjava.app -DartifactId=MavenWebApp -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

This will create a basic web application with root folder as SpringWebApp

 

Step 2:

Now to create an Eclipse project go inside the MavenWebApp folder and run the following :

mvn eclipse:eclipse -Dwtpversion=2.0

Now import the project in Eclipse using File -> Import -> Existing projects into Workspace

 

Step 3:

As an alternative to step 2, you can also import the project as a Maven project in Eclipse using File -> Import -> Existing Maven Projects

 

mavenprojsetup

 

Step 4:

 

If you check the files generated in Eclipse, this is how the files look like :

pom.xml

<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.topjava.app</groupId>

<artifactId>SpringWebApp1</artifactId>

<packaging>war</packaging>

<version>1.0-SNAPSHOT</version>

<name> Maven Webapp</name>

<url>http://maven.apache.org</url>

<dependencies>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>3.8.1</version>

<scope>test</scope>

</dependency>

</dependencies>

<build>

<finalName>MavenWebApp</finalName>

</build>

</project>

web.xml

<web-app>

<display-name>Archetype Created Web Application</display-name>

</web-app>

 

index.jsp

<web-app>

<display-name>Archetype Created Web Application</display-name>

</web-app>

Step 5:

Deploy the project in a server like Tomcat.

 

Step 6:

Once the SpringWebApp1 is deployed on server, go to the below url:

http://localhost:8080/MavenWebApp/

http://localhost:8080/MavenWebApp/

You should get the below message in browser :

Hello World!

© 2015 – 2016, https:. All rights reserved. On republishing this post, you must provide link to original post

Leave a Reply.. code can be added in <code> </code> tags