How to create a maven project in eclipse

Posted on December 11, 2016


In this post, we will show you how we can create a maven project in Eclipse IDE.

Technologies used -

  • Eclipse Mars.2 (4.5.2)
  • Maven 3.3.9
  • JavaSE 1.8

Let’s begin step by step to see how a maven project is created in eclipse.

maven-eclipse1.png

Step #2

In New Maven Project wizard, select Create a simple project then click on Next.

maven-eclipse2.png

Step #3

In next wizard, add project information and click on Finish.

Learn more about Maven naming conventions on groupid artifactId and version.

maven-eclipse3.png

 

Step #4

Review the newly created maven project structure.

maven-eclipse4.png

Step #5

As you can see in maven project structure, the default java compiler version ( i.e. source and target setting ) is 1.5. To change the default settings, add the following snippet to pom.xml.

<build>
	<plugins>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-compiler-plugin</artifactId>
			<version>3.6.0</version>
			<configuration>
				<source>1.8</source>
				<target>1.8</target>
			</configuration>
		</plugin>
	</plugins>
</build>

 

Step #6

After changes in pom.xml file, update the maven project. To update maven project right click on maven-project → Maven Update Project.

 maven-eclipse5.png

Step #7

Add some dependencies to pom.xml file. Here we are adding junit dependency to pom.xml.

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
</dependency>

 

Step #8

Review the complete pom.xml file.

<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>boraji.tutorail</groupId>
	<artifactId>maven-project</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>Maven Project</name>
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.6.0</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
		</dependency>
	</dependencies>
</project>

 

Step #9

Create a package, named as com.boraji.tutorial.maven, under src/main/java folder. Now create a class HelloMaven.java under com.boraji.tutorial.maven package and write the following code in it.

HelloMaven.java

package com.boraji.tutorial.maven;

import org.junit.Assert;
import org.junit.Test;

public class HelloMaven {
	@Test
	public void test() {
		Assert.assertEquals("Hello Maven", new String("Hello Maven"));
	}
}

 

Step #10

Run your first maven project. Right click on HelloMaven.javaRun asJUnit Test.

maven-eclipse6.png