In this article, we will walk through some of the basic maven operations
Basic operations
- 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
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 :
- Eclipse IDE – How to show line numbers ?
- Eclipse IDE – How to GO TO any line number directly ?
- Eclipse IDE – How to remove unused imports ?
- Eclipse IDE – How to clean project ?
- Eclipse IDE – How to build Java project automatically ?
- Eclipse IDE – How to comment and un-comment line & block ?
- Eclipse IDE – How to generate constructor using fields ?
- Eclipse IDE – How to generate getters and setters ?
- Eclipse IDE – How to search files ?
- Eclipse IDE – How to locate methods in Java file ?
- Eclipse IDE – How to open editor using CTRL + E ?
- Eclipse IDE – Java compiler compliance level issue
Related Articles:
- Apache Maven – Introduction
- Apache Maven – Install on Windows 7 OS
- Apache Maven – Settings.xml explanation
- Apache Maven – Proxy setting explanation
- Apache Maven – pom.xml explanation
- Apache Maven – Plugins explanation
- Apache Maven – Changing default Maven repository location in Windows 7 OS
- Apache Maven – Local, Central and Remote Repositories
- Apache Maven – Installing custom library into local repository
- Apache Maven – Transitive dependencies explanation
- Apache Maven – Exclusion of Transitive dependencies
- Apache Maven – Dependency Scopes
- Apache Maven – Skipping unit test using surefire plugin
- Apache Maven – Exclusions and Inclusions of unit test
- Apache Maven – offline execution
- Apache Maven – Co-ordinates explained
- Eclipse + Maven – Integration
- Eclipse + Maven – How to import Maven project with pom.xml ?
- Eclipse + Maven – Setting M2_REPO classpath variable in IDE
- Eclipse + Maven – M2_REPO is Non Modifiable
- Eclipse + Maven – Creating and exploring projects using archetypes
- Eclipse + Maven – Converting Web project to Maven project
- Eclipse + Maven – mvn eclipse:eclipse command
- Eclipse + Maven – Plugin execution not covered by lifecycle configuration
References:
- http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html
- https://www.benchresources.net/apache-maven-lifecycle-and-basic-operations
Happy Coding !!
Happy Learning !!