Table of Contents
URL: https://www.progressiverobot.com/maven-eclipse-ide/
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.
- Creating a simple Maven project, build and run it
- Creating a Java Web Project in Eclipse using Maven
- 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.
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.
2. Building the Maven Project in Eclipse
First of all, select the project and go to "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".
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>
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".
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.
To run the maven project, select it and go to "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.
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.
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.
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.
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.
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.
Select the project and go to "Configure > 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.
Conclusion
Eclipse provides built-in support for Maven. It helps us in working with maven projects easily in Eclipse IDE.
References
- M2Eclipse Plugin Page
- [Creating a Java Project using Maven Archetypes](/community/tutorials/creating-java-project-maven-archetypes)