Running A Maven Project Build Using Eclipse
Running a Build
Right-click the project in Eclipse, choose Run As > Maven Build, and specify goals such as clean test in the Goals field.
Common Maven Goals
clean— removes previously compiled files.compile— compiles the source code.test— runs the test suite.package— bundles the project into a jar file.
Why Use Maven Builds Instead of Just Running a Class
A Maven build follows the full, repeatable lifecycle — ensuring dependencies are resolved and everything compiles before tests run, exactly as it would in a CI/CD pipeline.
Setting Up a Reusable Run Configuration
Typing goals each time works, but a saved configuration is quicker and avoids typos. In Eclipse, open Run > Run Configurations, right-click Maven Build and choose New Configuration.
- Name: something clear, such as "Smoke tests".
- Base directory: click Workspace and pick the project, which fills in
${project_loc:your-project}. - Goals: for example
clean test. - Profiles: add a profile name if your POM defines one, such as
qa. - Parameters: add name and value pairs, which Maven receives as
-Dname=value. - JRE tab: select a JDK so compilation works.
- Click Run. Next time, pick it from the Run menu's history.
The Lifecycle Order and Useful Command Options
Maven phases run in order, and asking for one phase runs every earlier phase too. Running package therefore also validates, compiles and tests the project first.
| Phase | What happens |
|---|---|
| validate | Checks the POM and project structure |
| compile | Compiles src/main/java |
| test | Compiles and runs tests in src/test/java |
| package | Builds the jar or war in target |
| verify | Runs extra checks such as integration tests |
| install | Copies the artifact into your local repository |
clean belongs to a separate lifecycle, which is why it is usually written first. The same goals typed in Eclipse work from a terminal as well:
mvn clean test # full clean build and tests
mvn test -Dtest=LoginTest # run one test class
mvn test -Dtest=LoginTest#validLogin # run one test method
mvn test -DsuiteXmlFile=smoke.xml # needs Surefire configured to read this property
mvn clean install -DskipTests # build without running tests
mvn -o test # offline, use only cached dependencies
mvn -U clean test # force update of dependencies
Reading the Build Output
The Eclipse console shows the same log as the command line. Scroll to the bottom first: BUILD SUCCESS or BUILD FAILURE tells you the outcome, and just above it Surefire prints a line with tests run, failures, errors and skipped. Failures mean an assertion did not hold; errors mean an unexpected exception was thrown.
Detailed results are saved in target/surefire-reports, including text and XML files for each test class. CI tools read those XML files to show trends, so the build you run in Eclipse produces exactly what a pipeline would publish. Refresh the project with F5 if the folder does not appear.
Fixing Common Build Errors
- "No compiler is provided in this environment": Eclipse is running Maven with a JRE. Choose a JDK in the configuration's JRE tab or in Installed JREs.
- "Could not resolve dependencies": check your internet connection or proxy, confirm the version exists, then run with
-U. - Tests run: 0: test class names or folders do not match Surefire's defaults, or a TestNG suite file is not configured.
- Project shows red marks but the build passes: right-click the project and choose Maven > Update Project to resync Eclipse with the POM.
- Wrong Java level: set
maven.compiler.releasein the POM so Eclipse and Maven agree.
Ready to master real-world software testing?
Learn manual and automation testing hands-on with mentor-led sessions.