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

1. Using Maven with the Eclipse IDE

The Eclipse IDE provides back up for the Maven build. This support is developed in the M2Eclipse project.

It provides an editor for modifying the pom file and downloads dependencies if required. Information technology also manages the classpath of the projects in the IDE. You lot can besides utilize wizards to import existing Maven projects and to create new Maven projects.

2. Installation and configuration of Maven for Eclipse

ii.1. Installation of the Maven tooling for the Eclipse IDE

Most Eclipse IDE downloads already include back up for the Maven build system. To bank check, apply and bank check if you tin see the Maven logo (with the M2E) sign.

maven installed10

If Maven support is not yet installed, the following description can be used to install it.

Installation of Maven support into the Eclipse IDE

To install Maven support, select the menu entry. To use the latest release, enter the following URL in the Works with field.

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

For the usage of Maven for Coffee projects, you only demand the m2e component.

m2e installation

2.2. Configure automatically updates of Maven dependencies

In nigh cases information technology is useful if changes in the Maven configuration automatically updates the project settings.

Therefore, et the Automatically update Maven projects configuration flag under .

eclipse maven configuration projectupdate

3. Exercise: Create a new Maven project via the Eclipse IDE

In this exercise y'all learn how to create a new Maven projection with the Eclips IDE.

3.1. Create Maven project

Create a new Maven project via .

Create Maven project in Eclipse - Part 1

On the first magician page, you can select if you want to create a unproblematic project. In this instance you lot skip the archetype option. In this practice we want to apply an classic as template for our projection creation.

Create Maven project in Eclipse - Part 2

Press next, filter for the "maven-classic-quickstart" archetype and select the entry with the apache group. This is the classical Maven instance archetype for project creation.

Create Maven project in Eclipse - Part 3

On the terminal tab enter the GAV of your project similar to the post-obit screenshot.

Create Maven project in Eclipse - Part 4

three.2. Update project to use Java

Change the pom file of the generated projection to use Java 11. For this re-create the following code onto the content via the pom.xml tab of the Maven editor.

                                      <?xml version="1.0" encoding="UTF-8"?>                    <project                    xmlns=                    "http://maven.apache.org/POM/four.0.0"                    xmlns:xsi=                    "http://world wide web.w3.org/2001/XMLSchema-case"                    xsi:schemaLocation=                    "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-four.0.0.xsd"                    >                    <modelVersion>iv.0.0</modelVersion>                    <groupId>com.vogella</groupId>                    <artifactId>com.vogella.maven.eclipse</artifactId>                    <version>0.0.1-SNAPSHOT</version>                    <name>com.vogella.maven.eclipse</name>                    <url>http://www.vogella.com</url>                    <properties>                    <projection.build.sourceEncoding>UTF-eight</project.build.sourceEncoding>                    <maven.compiler.source>11</maven.compiler.source>                    <maven.compiler.target>eleven</maven.compiler.target>                    </backdrop>                    </project>                                  

three.3. Update Maven settings in Eclipse

Right-click your project and select and update your projection.

m2e newmavenproject44

3.4. Validate

Validate that the Maven build is correctly configured past running the build. For this right-click the pom.xml file and select .

Run Maven project in Eclipse

This opens a dialog which allows to define the parameters for the offset. Enter clean verify in the Goals: field and press the Run button.

Run Maven project in Eclipse

4. Exercise: Adjust generated projection to use JUnit5 and external libraries

In this exercise yous accommodate your generated project to employ JUnit5.

4.1. Modify generated project

Change the pom file of the generated projection to use JUnit 5. For this re-create the post-obit code onto the content via the pom.xml tab of the Maven editor.

                                      <?xml version="1.0" encoding="UTF-8"?>                    <project                    xmlns=                    "http://maven.apache.org/POM/4.0.0"                    xmlns:xsi=                    "http://world wide web.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>iv.0.0</modelVersion>                    <groupId>com.vogella</groupId>                    <artifactId>com.vogella.maven.eclipse</artifactId>                    <version>0.0.1-SNAPSHOT</version>                    <name>com.vogella.maven.eclipse</name>                    <url>http://www.vogella.com</url>                    <properties>                    <project.build.sourceEncoding>UTF-viii</project.build.sourceEncoding>                    <maven.compiler.source>11</maven.compiler.source>                    <maven.compiler.target>eleven</maven.compiler.target>                    </properties>                                        (i)                    <build>                    <plugins>                    <plugin>                    <artifactId>maven-surefire-plugin</artifactId>                    <version>ii.22.2</version>                    </plugin>                    <plugin>                    <artifactId>maven-failsafe-plugin</artifactId>                    <version>two.22.2</version>                    </plugin>                    </plugins>                    </build>                                        (2)                    <dependencies>                    <dependency>                    <groupId>org.junit.jupiter</groupId>                    <artifactId>junit-jupiter-api</artifactId>                    <version>v.seven.two</version>                    <scope>examination</telescopic>                    </dependency>                    <dependency>                    <groupId>org.junit.jupiter</groupId>                    <artifactId>junit-jupiter-engine</artifactId>                    <version>five.7.2</version>                    <scope>test</scope>                    </dependency>                    </dependencies>                    </projection>                                  

4.2. Update Maven

Correct-click your project and select and update your projection.

m2e newmavenproject44

4.three. Update test

Update your generated test to JUnit 5 via the following:

                                      parcel                    com.vogella.maven.eclipse                    ;                    import                    static                    org                    .                    junit                    .                    jupiter                    .                    api                    .                    Assertions                    .                    assertTrue                    ;                    import                    org.junit.jupiter.api.Examination                    ;                    public                    class                    AppTest                    {                    @Test                    public                    void                    shouldAnswerWithTrue                    ()                    {                    assertTrue                    (                    true                    );                    }                    }                                  

four.iv. Adding dependencies to your project

The Eclipse Maven tooling makes adding dependencies to the classpath of your project simple. In can directly add it to your pom file, or use the Dependencies tab of the pom editor.

Switch to the Dependencies tab and press the Add together button.

me2 adddependency20

If the Maven index was downloaded yous can also search directly this dependency via the dialog.

me2 adddependency30

Right-click your project and select and update your project.

4.five. Use library

Change or create the App.java grade in your src/main/coffee binder. This classes uses Gson. Equally Maven added it to your classpath, information technology should compile and you should be able to kickoff the grade via Eclipse.

                                      package                    com.vogella.maven.lars                    ;                    import                    com.google.gson.Gson                    ;                    public                    class                    App                    {                    public                    static                    void                    chief                    (                    String                    []                    args                    )                    {                    Gson                    gson                    =                    new                    Gson                    ();                    System                    .                    out                    .                    println                    (                    gson                    .                    toJson                    (                    "Hello World!"                    )                    );                    }                    }                                  

4.half dozen. Validate

Run the Maven build again and verify that the build runs successfully.

five. Do: Converting a Coffee project (create with Eclipse) to Maven

This exercise demonstrates how to catechumen a Coffee project to a Maven project.

5.1. Create Java project

Create a new Java project chosen com.vogella.build.maven.unproblematic in Eclipse.

Add together i grade called Principal. This class should have a main method, which write "Hello Maven!" to the command line.

                                      package                    com.vogella.build.maven.simple                    ;                    public                    class                    Chief                    {                    public                    static                    void                    main                    (                    String                    []                    args                    )                    {                    System                    .                    out                    .                    println                    (                    "Hullo Maven!"                    );                    }                    }                                  

5.2. Convert to Maven project

Select your project, right-click on information technology and select .

Convert Java project to Maven

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

                                      <project                    xmlns=                    "http://maven.apache.org/POM/four.0.0"                    xmlns:xsi=                    "http://www.w3.org/2001/XMLSchema-case"                    xsi:schemaLocation=                    "http://maven.apache.org/POM/iv.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"                    >                    <modelVersion>four.0.0</modelVersion>                    <groupId>com.vogella.build.maven.simple</groupId>                    <artifactId>com.vogella.build.maven.elementary</artifactId>                    <version>0.0.1-SNAPSHOT</version>                    <build>                    <sourceDirectory>src</sourceDirectory>                    <plugins>                    <plugin>                    <artifactId>maven-compiler-plugin</artifactId>                    <version>iii.one</version>                    <configuration>                    <source>one.8</source>                    <target>1.8</target>                    </configuration>                    </plugin>                    </plugins>                    </build>                    </projection>                                  

five.3. Execute the Maven build

Right-click the pom.xml file and select .

Run Maven build from Eclipse

Enter "clean install" as Goal.

You lot accept to enter the goals manually. The Select…​ button does non piece of work, the dialog it displays is ever empty.

Printing the Finish button. This starts the build, which y'all can follow in the Console view.

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

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

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

6.1. Create Maven web project project

Create a new Maven project called com.vogella.javaweb.maven.start via the entry. On the classic 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 Coffee Build Path. To fix this, correct click on your project and select Properties. On the Targeted Runtimes select your spider web server entry, e.grand., Tomcat.

6.two. Build your projection

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

6.3. Run on the server

Correct-click your project and select the 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

seven. Exercise: Debug tests

Add a breakpoint in Eclipse in one of your tests.

Start your Maven build on the commadline with the debug parameter.

                mvn -Dmaven.surefire.debug test              

This stop the command line build one time the tests are executed and listen on port 5005 for a remote connection.

In Eclipse select the and create a new Remote Java Application run configuration.

prototype::maven-exam-debug10.png

Ensure to enter the correct project and the port 5005. Press the btn:Debug[] to connect to the running build. Yous tin now debug your exam.

viii. References for Webdevelopment with Eclipse

To use Maven in Eclipse for Java web development, y'all should also install an configure the Eclipse spider web development tools (WTP). See Eclipse Web Evolution Tools for a tutorial.