This post demonstrates creating a Maven web project with eclipse. This is fairly simple.
Let’s begin.
1: Start Eclipse, click on File->New->Other
2: In the popped New wizard, scroll down and select Maven->Maven Project, click Next
3: In above screen, leave the ‘Use default workspace location’ selected to create this project in the current workspace.Click Next. Scroll down and select option with Artifact Id as maven-archetype-webapp.
4: Click Next. Provide Group Id, Artifact Id & version. These parameters gives your project deliverable(jar/war/ear …) a unique identity in repository. Click Finish. Refresh your project.
5: Your project structure should like below one.
Note: In case you do not see the src/main/java and src/test/java folder in your project structure, go to Project>Properties>Java BuildPath>Libraries, select/change-to appropriate Java version, click OK, you should see those folders now. You may also perform a maven-update on your project.
Note: With recent Eclipse versions you don’t need to use mvn eclise:eclipse anymore, this functionality is built-in with recent m2e eclipse plugin.
6: Finally, update your pom.xml based on your project requirement (adding dependencies, profile, plugins, repositories, output format etc…)
Below is the default pom.xml generated for our project.
<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.websystique.maven</groupId> <artifactId>SampleMavenWebProject</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>SampleMavenWebProject 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>SampleMavenWebProject</finalName> </build> </project>
Dependency section refers to the library your project is replying upon. These libraries are found on different maven repositories.
For instance, if your code will be using spring framework and your test cases relies upon TestNG instead of junit, your pom.xml will include the dependency for Spring and TestNG like 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.websystique.maven</groupId> <artifactId>SampleMavenWebProject</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>SampleMavenWebProject Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <springframework.version>4.0.5.RELEASE</springframework.version> <testng.version>6.8.8</testng.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${springframework.version}</version> </dependency> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>${testng.version}</version> </dependency> </dependencies> <build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.2</version> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> </plugins> </pluginManagement> <finalName>SampleMavenWebProject</finalName> </build> </project>
Additionally, The Maven plugin maven-compiler-plugin
has been added here to explicitly specify the jdk-version we are going to use. Do note that it also forces eclipse to respect the jdk-version being used for the project. if it is not present, and you perform mvn-update from within your eclipse, eclipse switches jdk-version back to default jdk-version [1.5] which is annoying. So do add it in your project pom as well.
That’s it.
If you like tutorials on this site, why not take a step further and connect me on Facebook , Google Plus & Twitter as well? I would love to hear your thoughts on these articles, it will help improve further our learning process.
In this post we will be developing a full-blown CRUD application using Spring Boot, AngularJS, Spring Data, JPA/Hibernate and MySQL,…
Spring Boot complements Spring REST support by providing default dependencies/converters out of the box. Writing RESTful services in Spring Boot…
Being able to start the application as standalone jar is great, but sometimes it might not be possible to run…
Spring framework has taken the software development industry by storm. Dependency Injection, rock solid MVC framework, Transaction management, messaging support,…
Let's secure our Spring REST API using OAuth2 this time, a simple guide showing what is required to secure a…
This post shows how an AngularJS application can consume a REST API which is secured with Basic authentication using Spring…
View Comments
src/main/java is not appearing in my project structure. Can you please help?
Hi Bineet, Did you try to refresh and update your project afterwords?
Bineet Try this - Right click the Maven Project -> Build Path -> Configure Build Path
In Order and Export tab, you can see the message like '2 build path entries are missing'
Now select 'JRE System Library' and 'Maven Dependencies' checkbox