This tutorial describes the usage of Maven within the Eclipse IDE for building Java applications.

1. Using Maven with the Eclipse IDE

The Eclipse IDE supports Maven via the M2Eclipse (m2e) project. m2e provides an editor for the pom.xml file, downloads dependencies and manages the classpath of your projects. Wizards allow you to import existing Maven projects and to create new ones. == Installation and configuration of Maven for Eclipse

1.1. Installing the Maven tooling

The Eclipse IDE for Java Developers already includes Maven support. To check, select Help  About Eclipse IDE and look for the m2e logo.

Maven support in the About dialog
Installing Maven support

Select Help  Install New Software…​ and enter the following update site in the Work with field.

https://download.eclipse.org/releases/latest

For Java projects, you only need the m2e component.

m2e installation

1.2. Updating the project configuration automatically

Eclipse updates the project configuration automatically after you change the pom.xml file. The Automatically update Maven projects configuration setting under Window  Preferences  Maven controls this.

Maven preferences

2. Exercise: Create a Maven project

In this exercise, you create a Maven project with the Eclipse IDE.

2.1. Create the project

Select File  New  Other…​  Maven  Maven Project.

New Maven Project wizard

The first page allows you to create a simple project without an archetype. In this exercise, you use an archetype as template, so leave Create a simple project unchecked and press Next.

Filter for maven-archetype-quickstart and select the entry with the org.apache.maven.archetypes group.

maven-archetype-quickstart selected

On the last page, enter com.vogella as Group Id and com.vogella.maven.eclipse as Artifact Id and Package. Clear run archetype generation interactively and press Finish.

Group Id and Artifact Id of the new project

The archetype creates an App class, a JUnit 5 test and a pom.xml file.

2.2. Set the Java version

The generated pom.xml file compiles for Java 17. Open it, switch to the pom.xml tab of the editor and change the maven.compiler.release property to 25.

<properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <maven.compiler.release>25</maven.compiler.release>
</properties>

2.3. Update the project configuration

Eclipse updates the project configuration when you save the pom.xml file. To trigger an update manually, right-click the project and select Maven  Update Project…​.

Update Maven Project dialog

2.4. Validate

Right-click the project and select Run As  Maven build…​.

Run As menu of the project

Enter clean verify in the Goals field and press Run. The build succeeds.

Maven launch configuration

3. Exercise: Use an external library

In this exercise, you add the Gson library to the project of the previous exercise.

3.1. Add a dependency

You can add a dependency directly in the pom.xml file or via the Dependencies tab of the pom editor. Switch to the Dependencies tab and press Add…​.

Dependencies tab of the pom editor

Enter com.google.code.gson as Group Id, gson as Artifact Id and 2.15.0 as Version. You find the latest version on Maven Central.

Save the pom.xml file, Eclipse updates the project and downloads the library.

3.2. Use the library

Create or change the App class in the src/main/java folder.

package com.vogella.maven.eclipse;

import com.google.gson.Gson;

public class App {
  public static void main(String[] args) {
    Gson gson = new Gson();
    System.out.println(gson.toJson("Hello World!"));
  }
}

The class compiles, as Maven added Gson to the classpath. Right-click it and select Run As  Java Application.

3.3. Validate

Run the Maven build again with clean verify. The build succeeds.

4. Exercise: Converting a Java project (create with Eclipse) to Maven

This exercise demonstrates how to convert a Java project to a Maven project.

4.1. Create Java project

Create a new Java project called com.vogella.build.maven.simple in Eclipse.

Add one class called Main. This class should have a main method, which write "Hello Maven!" to the command line.

package com.vogella.build.maven.simple;

public class Main {

    public static void main(String[] args) {
        System.out.println("Hello Maven!");
    }
}

4.2. Convert to Maven project

Select your project, right-click on it and select Configure  Convert to Maven project…​.

Convert Java project to Maven

This creates a pom.xml file similar to the following.

<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>com.vogella.build.maven.simple</groupId>
  <artifactId>com.vogella.build.maven.simple</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

4.3. Execute the Maven build

Right-click the pom.xml file and select Run As  Maven build.

Run Maven build from Eclipse

Enter "clean install" as Goal.

You have to enter the goals manually. The Select…​ button does not work, the dialog it displays is always empty.

Press the Finish button. This starts the build, which you can follow in the Console view.

Once the build finishes, press F5 on the project to refresh it. You see a target folder that contains the build artifacts, e.g., a JAR file.

5. Exercise: Create a Java web project in Eclipse using Maven

This exercise demonstrates how to create a web application in Eclipse that uses Maven. It assumes that you have already configured Eclipse for the creation of web applications.

5.1. Create a Maven web project

Create a new Maven project called com.vogella.javaweb.maven.first via the File  New  Other  Maven  Maven Project entry. On the archetype selection, select the maven-archetype-webapp entry and click the Next button.

Webproject archetype with Maven

Enter the group, artifact and version of your new Maven component.

Webproject archetype with Maven

You may see the error: The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path. To fix this, right-click on your project and select Properties. On the Targeted Runtimes select your web server entry, e.g., Tomcat.

5.2. Build your project

Run your mvn clean verify build command from Eclipse. Validate that there are no issues with the build.

5.3. Run on the server

Right-click your project and select the Run As  Run on Server menu entry.

Start Maven project on server
Start Maven project on server
Start Maven project on server
Start Maven project on server

If you open a browser you should be able to access your webapplication.

Start Maven project on server

6. Exercise: Debug tests of a Maven build

Set a breakpoint in one of your tests.

Start the Maven build on the command line with the following command.

mvn -Dmaven.surefire.debug test

Before running the tests, the build waits for a debugger on port 5005.

In Eclipse, select Run  Debug Configurations…​ and create a Remote Java Application configuration.

Remote Java Application debug configuration

Select your project, enter port 5005 and press Debug. Eclipse connects to the build and stops at your breakpoint. == References for Webdevelopment with Eclipse

To use Maven in Eclipse for Java web development, you should install an configure the Eclipse web development tools (WTP). See Eclipse Web Development Tools for a tutorial.

7. Eclipse Maven (m2e) resources

Home Tutorials Training Consulting Books Company Contact us


Get more...