Maven Commands

  

Common Maven Commands Explained (Including for Eclipse IDE)

Maven is a powerful build automation tool for Java projects. Whether you're using Maven via the command line or integrated with Eclipse IDE, the core commands remain the same. Here's a breakdown:


Common Maven Commands (Command Line & Eclipse IDE)


CommandDescription
mvn cleanDeletes the target/ directory. Ensures a clean build.
mvn compileCompiles the Java source code in the src/main/java directory.
mvn testRuns unit tests in src/test/java.
mvn packagePackages compiled code into a .jar or .war file.
mvn installInstalls the .jar or .war to your local Maven repository (~/.m2).
mvn deployDeploys the artifact to a remote repository (e.g., Nexus, Artifactory).
mvn siteGenerates project documentation (using plugins).
mvn validateValidates the project structure and checks for missing info in pom.xml.
mvn verifyRuns additional checks like integration tests.
mvn dependency:treeShows the project dependency tree. Helps find conflicts.
mvn help:effective-pomShows the fully resolved POM (with inheritance and profiles applied).
mvn archetype:generateCreates a new Maven project from an archetype (template).


Using Maven Commands in Eclipse IDE

Eclipse IDE (with M2E Plugin—Maven integration) allows you to run Maven commands without using the terminal.


To Run Maven Goals in Eclipse:

  1. Right-click on your Maven project in Project Explorer.

  2. Choose Run As > Maven build...

  3. In the Goals field, enter one or more of the following:

    • clean

    • compile

    • package

    • install

    • test

    • site

  4. Click Run.



After adding dependency in pom.xml, we should update project, as shown below:

  • Right-click → Maven → Update Project to refresh dependencies.


We can also execute maven commands from command line from project workspace.

go to project workspace and open cmd terminal and execute below command:
mvn clean install





No comments:

Post a Comment