Using Maven in Eclipse IDE

Filed Under: Maven

Eclipse IDE has fantastic support for Maven. We can easily create maven projects, build, deploy, and run it using the Eclipse GUI based interface. In this tutorial, we will learn how to use maven in Eclipse. We will go through the following examples.

  1. Creating a simple Maven project, build and run it
  2. Creating a Java Web Project in Eclipse using Maven
  3. Converting a simple Java project to Maven

1. Creating a Simple Maven Project in Eclipse

Go to File -> New -> Project and it will open the new project wizard. Select “Maven Project” as shown in the below image and click on “Next” button.

Eclipse New Maven Project

Eclipse New Maven Project

In the next screen, we have the option to specify the project location and add it to any working set. We will not make any changes to it.

There is also an option to skip archetype selection and create a simple project. But, we will use the archetype to create the template project.

Eclipse New Maven Project Location Working Set

In the next screen, we have to select the archetype of the project. This is the most important step in creating the project. We want a simple maven JAR based application. So, we will choose the “maven-archetype-quickstart” artifact to create the project.

Eclipse New Maven Project Select Archetype

In the next screen, we have to specify the groupId, artifactId, and base package of the project. You can use the values from the image below or enter any values you want in your project.

Eclipse New Maven Project GroupId ArtifactId

After clicking the Finish button, the popup wizard will close and the project will get created. It will be visible in the project explorer. The below image shows all the directories and the pom.xml of the newly created project.

Eclipse Maven Project Package Explorer

Eclipse Maven Project Package Explorer

2. Building the Maven Project in Eclipse

First of all, select the project and go to “Run As -> Maven Build”.

Eclipse Run As Maven Build

Eclipse Run As Maven Build

The “Edit Configuration” popup window will open. Enter the “Goals” as “package” to build the project and click on the Run button.

Eclipse Maven Edit Configuration Goals Package Run

If you look at the Console messages, you will notice that the build failed with the compilation error messages as “Source option 5 is no longer supported. Use 7 or later“.

Eclipse Maven Compilation Failed Build Error

Eclipse Maven Compilation Failed Build Error

It’s because of the auto-generated pom.xml file, which is not compatible with the latest Java versions. We can fix it by using the latest version of maven-compiler-plugin and setting maven.compiler.release to the Java version we are using.


<properties>
	<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	<maven.compiler.release>13</maven.compiler.release>
</properties>
	
<build>
	<pluginManagement>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.8.1</version>
			</plugin>
		</plugins>
	</pluginManagement>
</build>

Fixing Maven Compiler Plugin Error Maven Compiler Release

3. Updating the Maven Project in Eclipse

Since we have changed the pom.xml file, we have to update the maven project to use the new configurations. You will notice a cross mark in the project root in the package explorer.

The Problems view will also show the error that the project pom.xml is changed and we have to update the project.

Select the project and go to “Maven > Update Project”.

Eclipse Maven Update Project

Eclipse Maven Update Project

In the popup window, make sure the project is selected. Also, “Update project configuration from pom.xml” should be checked. If you want maven to download all the required dependencies again, check the “Force update of Snapshots/Releases” option.

Click on the Ok button and the project will get updated with the latest pom.xml configurations.

Eclipse Update Maven Project Options

4. Building and Running the Maven Project in Eclipse

Now that we have fixed the pom.xml configurations, build the project once again as shown above. This time the Console should show the “BUILD SUCCESS” message.

Eclipse Maven Project Build Success

Eclipse Maven Project Build Success

To run the maven project, select it and go to “Run As > Java Application”.

Eclipse Run As Java Application

Eclipse Run As Java Application

In the next window, select the main class to execute. In this case, select the App class and click on the Ok button.

Eclipse Select Java Application

Eclipse Select Java Application

The App class code is:


package com.journaldev.classes;

public class App {

	public static void main(String[] args) {

		System.out.println("Hello World");
	}

}

You will see the “Hello World” output in the Console window.

Eclipse Java Application Console Output

Eclipse Java Application Console Output

5. Creating a Java Web Project in Eclipse using Maven

The process of creating a java web project using Maven is almost the same. The only change is that we will use “maven-archetype-webapp” so that a web application project is created.

Eclipse Maven Java Web Project

Eclipse Maven Java Web Project

The next step would be to fix the pom.xml file by using the latest version of the maven-compiler-plugin and using the latest java version.

Then build the project with the “package” goal and you should see the “BUILD SUCCESSFUL” message in the console.

Eclipse Maven Java Web App Build Success

Eclipse Maven Java Web App Build Success

Notice the project build generates maven-example-webapp.war file in the target directory.

We can integrate Apache Tomcat in the Eclipse itself, but that is out of the scope of this tutorial. You can deploy the WAR file into any servlet container and you should see the following output.

Eclipse Java Web App Output

Eclipse Java Web App Output

6. Converting a simple Java project to maven

Sometimes we have a simple Java project. It’s recommended to use Maven for dependency and build management. We can easily convert a simple Java project to a maven based project.

Let’s say we have a simple Java project like the below image.

Eclipse Simple Java Project

Eclipse Simple Java Project

Select the project and go to “Configure > Convert to Maven Project”.

Eclipse Convert To Maven Project

Eclipse Convert To Maven Project

It will open up a new window to create a pom.xml file. We have to provide project configurations such as groupId, artifactId, packaging, etc.

Eclipse Maven Pom Configurations

The below image shows the maven project structure and the newly generated pom.xml file.

Eclipse Converted Maven Project Structure

Eclipse Converted Maven Project Structure

Conclusion

Eclipse provides built-in support for Maven. It helps us in working with maven projects easily in Eclipse IDE.

References

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages