Eclipse and Maven tutorial

User Rating: 0 / 5

Star InactiveStar InactiveStar InactiveStar InactiveStar Inactive
 

In this tutorial we will show how you can create a Maven project from Eclupse using M2Eclipse plugin.

Maven and Eclipse tutorial

The first requirement for this example is that Eclipse is equipped with Maven M2Eclipse. If your Eclipse installation does not have M2Eclipse pre-installed, you can manually install the plugin from the Eclipse Marketplace.

eclipse maven tutorial eclipse maven tutorial

The next step is verifying your Maven configuration. If you have already installed Maven on your Pc (like me) you would like to use the settings from your installation. Otherwise you can use the default (internal) installation of Maven.

eclipse maven tutorial eclipse maven tutorial

You can find the configuration settings in Window | Preferences | Maven| Installations.  Now it's time to create a new Maven Project, using the Eclipse plugin wizard.

eclipse maven tutorial eclipse maven tutorial

In the next screen confirm that you will use a Maven archetype and set up the project properties. Next choose an archetype as starting point for your projects such as the maven-archetype-quickstart:

eclipse maven tutorial eclipse maven tutorial

Next, we will specify the Archetype parameters, such as GroupId, ArtifactId, Version and package, as shown by the following picture:

maven jboss tutorial

We just created a project that is an Eclipse and Maven project. If you inspect the filesystem,you will find that a valid pom.xml file exists in the project root, which makes it a valid Maven project.

The pom.xml file barely contains a dependency to the JUnit framework, which will be used to unit test your application:


<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>SimpleMaven</groupId>
  <artifactId>SimpleMaven</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>SimpleMaven</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

In addition, you will also find .classpathand .projectfiles and the .settings folder which are Eclipse-specific resources, making it a valid Eclipse project as well.

eclipse maven tutorial eclipse maven tutorial
Now test your program. the shortest way is right-clicking on the pom.xml file which shows the available Maven goals (choose Test which will run the test class).

eclipse maven tutorial eclipse maven tutorial

On the other hand, if you want  to customize the Maven execution, create a new Runtime configuration for Maven. In this case, we decided to execute a Main class, using the mvn compile exec:java -Dexec.mainClass=com.mastertheboss.SampleMaven.App class

eclipse maven tutorial eclipse maven tutorial

Follow us on Twitter