Creating a Java web project in Eclipse with Maven support sounds simple as Eclipse has great support for Maven, but actually it doesn’t. You can create a Maven project for Java webapp by clicking menu File > New > Maven Project (you need to switch to the Java EE perspective to see this menu).

In the New Maven Project dialog appears, click Next. Then you see a list of built-in archetype (type of Maven project) to choose from, as shown below:

New Maven project dialog

I tested all of those archetypes but none can generate a properly configured basic Java web project. So here’s the proper way to create a Java web project in Eclipse with Maven support:

First, you create a new project as usual, click File > New > Dynamic Web Project:

New Dynamic Web Project menu

Then follow the wizards to create a Java dynamic web project normally. The newly created project would look like this:

New Java dynamic web project

Now, right click on the project name and click Configure > Convert to Maven Project:

Convert to Maven project menu

Then in the Create new POM dialog, enter essential information for a Maven project like Group Id, artifact Id, version, name and description:

Maven POM

Note that the Packaging type is war by default because this is a Java web project which will be packaged into a WAR file to deploy.

Then click Finish. You will the project’s icon is updated with “M” letter – indicating it is a Maven project:

Maven project

You also see the pom.xml file is generated in the project’s root directory. It is the Project Object Model configuration file used by Maven.

Now edit the pom.xml file to specify the dependency for Java Servlet API:

<dependencies>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.0.1</version>
			<scope>provided</scope>
		</dependency>
</dependencies>

This dependency is needed to write code that uses Servlet API, e.g. servlet classes. The whole content of the pom.xml file would look like this:

<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>net.codejava</groupId>
	<artifactId>MyJavaWebApp</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<build>
		<sourceDirectory>src</sourceDirectory>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.8.0</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>
			<plugin>
				<artifactId>maven-war-plugin</artifactId>
				<version>3.2.1</version>
				<configuration>
					<warSourceDirectory>WebContent</warSourceDirectory>
				</configuration>
			</plugin>
		</plugins>
	</build>
	<dependencies>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.0.1</version>
			<scope>provided</scope>
		</dependency>
	</dependencies>
</project>

Now you have a Java dynamic web project with Maven support. Happy coding!

You can watch the video:

 

Other Eclipse Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.



Add comment

   


Comments 

#2Jerry2023-05-05 08:56
Thanks. I too tried all of those archetypes but none can configure basic Java web project. Either it timed out or just failed.
Quote
#1vvrk2020-12-09 21:40
how to create src/main/java and src/main/resources
Quote