Apache Maven: Basic Operations

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


Basic operations covered in this article are

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

For Maven lifecycle refer here

mvn compile

This command just compiles the source code

When we issue this command all preceding build phases’ executed including this phase

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

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

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)

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

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)




Read Also:


Happy Coding !!
Happy Learning !!

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