top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

How to run Maven Goals from Eclipse and Command Prompt?

Introduction:


Maven is a Build Automation Tool that comes from Apache. It is mainly used for Java projects. In testing point of view, it helps to manage the dependencies which are .jar files. It is used to compile source code, test, package and deploy it.


Maven Goals:


Below are the basic goals of a Maven Build Life Cycle. Each goal is related to a task.


1. clean : It deletes all the artifacts and files generated by the previous build and keeps the project clean.


2. compile: It compiles the source code of the project.


3. test: It tests the complied source code . It is not required to be packaged or deployed.


4. package: It converts the whole project into a .jar file or .war file


5. install: It installs the package into a local repository for use of another project.


Pre-requisite:

We need to make sure that the following plugins are added in pom.xml in order to execute our Maven project as Maven goals.


Maven-surefire-plugin : It is used to execute Maven project as Maven goal as well as CI/CD integration such as Jenkins.

Maven-compiler-plugin: It is used the compile the source code and make sure that it is ready for execution without any errors.

Note: In case of BDD, mention the name of the runner class in the testng.xml file. Remember the goals are case sensitive.




To run Maven Goals from Eclipse:

1. compile

Step 1: Go to Eclipse and Right click on your Maven Project which is to be compiled

Step 2: Click on “Run As”

Step 3: Click on “Maven build…”

Step 4: Type “compile” in Maven Goals textbox. Click on 'Run' button.


Step 5. We can see BUILD SUCCESS information on the console if the code is compiled without any errors.

2.test

When we execute the Maven goal as ‘test’, first the source code is compiled and then executed. Always it starts from the first goal and executes till the previous goal, then it executes whatever the goal we mention. It will execute the testng.xml file, the one we specified in maven-surefire-plugin. After execution, it will give the status as “BUILD SUCCESS” on the console.


Order of execution for ‘test’ goal : compile -> test


Step 1: Right click on your Maven Project which is to be tested

Step 2: Click on “Run As”

Step 3: Click on “Maven build…”

Step 4: Type “test” in Maven Goals textbox. Click on 'Run' button.

Step 5. We can see BUILD SUCCESS information on the console if the code is compiled and run.

3. package

This Maven goal converts the Maven project into executable jar file. When the execution is completed successfully, it will start packaging the code.

Order of execution for ‘package’ goal : compile -> test -> package

Step 1: Right click on your Maven Project which is to be tested

Step 2: Click on “Run As”

Step 3: Click on “Maven build…”

Step 4: Type “package” in Maven Goals textbox. Click on 'Run' button.





Step 5. We can see BUILD SUCCESS information on the console if the code is compiled, run and packaged into .jar file.


Step 6: Refresh the project.




Step 7 : Under ‘target’ folder, the .jar file is created. We can see the .jar file of the project.

4. clean

Step 1: Right click on Maven Project

Step 2: Click on “Run As”, Click on "Maven clean"

Step 3: Or Click on "Run As", Click on "Maven build..." , Type “clean” in Maven Goals textbox and Click on 'Run' button


Step 5: 'clean’ goal cleans ‘target’ folder which has the artifacts and files generated from the previous builds.

5. install

It will move the .jar file into the local repository named .m2.

Order of execution for ‘install’ goal : compile -> test -> package -> install

Step 1: Right click on Maven Project

Step 2: Click on “Run As”

Step 3: Click on “Maven build…”

Step 4: Type “install” in Maven Goals textbox. Click on 'Run' button.




Step 5: It moved the .jar file to local repository .m2. We can see the information 'BUILD SUCCESS'.

To run Maven Goals from Command Prompt:

If any non technical team member wants to run this .exe file in their system , they do not need to have Eclipse for execution. Just Java and Maven installation is enough.

Step 1: To navigate to command prompt, right click on the Maven Project.

Step 2: Click on 'Show In'

Step 3: Click on 'System Explorer'



Step 4: Type 'cmd' on the Address bar



Step 5: A command prompt window is opened. To run 'clean' maven goal, type the following command

> mvn clean



Step 6: To run 'compile' maven goal, type the following command in Command Prompt

> mvn compile




Step 7: To run 'test' maven goal, type the following command in Command Prompt

>mvn test

Step 8: We can see the message 'BUILD SUCCESS' after the run.

Step 9: To run 'package' goal, type the following command

>mvn package



Step 10 : To run 'install' goal, type the following command

> mvn install


Conclusion:

In this blog, we learned how to run Maven goals for a Maven project from Eclipse as well as command prompt.

13,595 views1 comment

+1 (302) 200-8320

NumPy_Ninja_Logo (1).png

Numpy Ninja Inc. 8 The Grn Ste A Dover, DE 19901

© Copyright 2022 by NumPy Ninja

  • Twitter
  • LinkedIn
bottom of page