Apache Maven – Basic Operations

In this article, we will walk through some of the basic maven operations

Basic operations

  1. compile –> compiles the source code
  2. test –> tests the compiled source code, according to testing framework configured
  3. package –> packages the source code, but before that it executes/runs compile and tests
  4. install –> compiles, tests, packages and finally installs to local maven repository
  5. clean –> command to delete/remove older version of project build, before running to build a project to be deployed

For Maven lifecycle refer here

1. mvn compile

  • This command just compiles the source code
  • When we issue this command all preceding build phases’ executed including this phase

2. mvn test

  • This command tests the compiled source code
  • For test to execute, we need to configure test framework in the test folder (as per maven folder structure)
  • Similarly, all preceding steps like validate, compile, etc gets executed along with this step

3. mvn package

  • This command packages the compiled source code into deployable artifacts. This packaging could be JAR, WAR, EAR or POM depending on the pom attribute <packaging>
  • All preceding steps like validate, compile, test are executed before packaging to JAR/WAR/EAR
  • Note: Default is JAR, if nothing is specified in the <packaging> attribute of pom.xml

4. mvn install

  • This command when issued, all preceding steps like validate, compile, test, package and finally this step install gets executed
  • When finally “install” executes, then it installs the deployable to local maven repository thereby allowing to use this project as <dependency> in some other project (restricted to environment, where its gets executed)

5. mvn clean

  • This command removes/deletes the older version of deployable artifacts from maven’s “target” folder
  • Note: It’s always advisable to execute clean command before executing any build command so as to make sure we are getting latest project build every time

6. mvn clean install

  • This is the most preferred command in the development or production environment which makes sure to delete the older version of the project build that exists in the maven’s “target” folder, before executing the new project build command i.e.; (install)
  • Also this ensures to install a copy of the deployable artifact in any environment wherever it gets executed (assume we are executing in production environment)

Useful Eclipse IDE shortcuts :

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Apache Maven - Plugins explanation
Apache Maven - Lifecycle and Basic Operations