“Do I need these instructions?”

You only need these instructions when it comes to test that your code runs using the Maven wrapper that was provided to you—and you want to test that from within Eclipse.

You are encouraged to just run the test cases using the JUnit GUI tool in Eclipse, as this will be more convenient and easier to navigate through the test cases. In this case, you will be ignoring the Maven wrapper provided.

However, when it comes time to submitting your assignments, you still need to make sure your assignment runs using the provided Maven wrapper. In this case, either follow the instructions below to use the Maven wrapper from within Eclipse, or simply use the Maven wrapper from the command line like was explained here.


“But Eclipse already has Maven built into it!”

Correct! While Eclipse already has Maven built in, we want to make sure we use the Maven wrapper provided in the assignment repositories. This ensures that we are all using the same Maven version to compile and test the code.

This is similar logic to why we would want to use the Maven wrapper from the command line, rather than relying on whatever Maven version developers would install themselves.


Using the Maven Wrapper (as an “External Program”) in Eclipse

As you might recall, the Maven wrapper is that mvnw file (if you are on Unix/MacOS) or mvnw.cmd (if you are on Windows). As such, we need to tell Eclipse to use this file. From Eclipse’s point of view, the Maven wrapper will be seen as an “external program”.

Import the code into Eclipse as a Maven project

First, make sure you have cloned the code from GitHub to your computer.

If you have already imported the code as a Maven Project in Eclipse, you can jump to “Setting up the Maven wrapper as an External Tool”.


From Eclipse, go to File, then Import...

Screenshot

Select Existing Maven Projects, then Next >

Screenshot

Click on Browse...

Screenshot

Screenshot

You will see the dialog populate, similar to this. Click Finish

Screenshot

Your Eclipse project has been created. The name of the project will depend on what’s in the <name> section of your pom.xml file.

Even though the project is recognised as a Maven project, we don’t want to use Eclipse’s built-in Maven feature. So, don’t use these Maven options.

Screenshot


Setting up the Maven wrapper as an External Tool

We want to tell Eclipse to run our Maven wrapper as an “External Tool”. This is the relevant section in Eclipse:

Screenshot

Click on the tiny arrow in the External Tools menu section, then External Tools Configurations...

Screenshot

Double-click the Program option

Screenshot

Overwrite the Name to something more meaningful. For example, Maven wrapper clean test so we know what this configuration is for.

Then click Browse Workspace... under the Location section

Screenshot

Select your project from the list on the left.

Depending on whether you are Unix/MacOS or Windows, select the correct wrapper file.

Then click OK

Screenshot

Now click Browse Workspace... under the Working Directory section

Screenshot

Select your project from the list, then click OK

Screenshot

Type clean test in the Arguments section, then click Apply, then click Run

Screenshot

You will see your project run. You can see the corresponding test cases, as usual.

Screenshot

When you need to run the Maven wrapper again, remember it’s in the External Tools section.

Screenshot

The above instructions were to make a single run configuration, namely Maven’s test phase. You will notice we specified clean test, so that the old compiled code is deleted before running the test cases again. It’s just safer, to be sure you are always running the latest code.

Since this configuration is using test, it will do the compile automatically. Keep this in mind if you see a BUILD FAILURE—it might be due to your code not compiling, and not necessarily that your test cases failed!

If you would like to have separate run configurations for each of the phases (not essential), you can if you want. Repeat the instructions above for each phase you want. All you would need to change is the Name of the configuration (Step 12) and the Arguments (Step 20). For example:

  • Maven compile:
    • Name = Maven wrapper compile
    • Arguments = clean compile
  • Maven test: (i.e. what we have already done above)
    • Name = Maven wrapper test
    • Arguments = clean test
  • Maven clean:
    • Name = Maven wrapper clean
    • Arguments = clean