Maven Hello World Example

In this tutorial, we will install and configure Maven on Windows. Then we will create our first project using Maven.

 

1. Technologies used

  • Java 1.6
  • Maven 3.0.5
  • Windows 8
  • Eclipse 4.4

 

2. Java configuration

Install JDK and in environment variables, add a new system variable called JAVA_HOME :

Maven-Hello-World-Example-2

Add JAVA_HOME variable to the system PATH :

Maven-Hello-World-Example-1

 

3. Maven configuration

First, download Maven from maven.apache,  then unzip the package to a selected folder (ex : C:\apache-maven-3.0.5)

Add a new environment variable M2_HOME as shown below :

Maven-Hello-World-Example-4

Add M2_HOME to the PATH :

Maven-Hello-World-Example-3

 

4. Create Maven project

To verify that Maven was correctly installed, open a command prompt and tape : mvn -version

 

Now, we will create our first java project using Maven.

Open a command line terminal, navigate to a selected folder, and tape the following command :

Where :
groupId is the project identity, it must be unique and it must follow the package name rules.
artifactId is the project name.

That will generate a skeleton sample Maven project called : MavenHelloWorldExample

 

5. Import the project into Eclipse

First of all, we must convert our project to support Eclipse.

Open a command line terminal, navigate to the project folder (cd ../MavenHelloWorldExample), then execute the following command :

Now we can import our project into Eclipse using : File> Import> Existing Projects into Workspace> Browse..

Maven-Hello-World-Example-5

 

6. Test It

In Eclipse, open the project and go to App class, and  execute As Java Application :

Output :

Hello World!

 

Download  source code