Updated March 8, 2023
Introduction to Maven Commands
Maven is a software project management and comprehension tool which was developed by Apache. It was initially released in July 2004. It is basically used to build projects written in C#, Ruby, Scala and other languages. Maven is very stable and provides different plugins which can help in generating PDF versions and also generate a list of any recent changes that have been made. It is easy to use as each project has a Project Object Model which is an XML file that contains all details of the project. Also, it is easy to migrate any new features through Maven. Let us have a look at those commands that are used.
Basic Maven Commands
The following are the basic commands of which you should be aware.
- Mvn –version: This command helps us in knowing the current version of Maven that is installed
- Creating a project: To create a project using MVN architecture below maven command should be used.
mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false
The templates that maven uses are called archetypes. By using the above command Maven will get all archetypes. It will do all configurations and will generate a working project. This architecture can be simplified by providing an archetypeArtifactId property which helps in creating apps easily.
- MVN package: This command is used to execute all Maven phases until the package phase. It does the job of compiling, verifying and building the project. It builds the jar file and places it in the specified folder under the specified project.
- mvn clean install: This maven command helps in executing a clean build life cycle and installs build phase in the default build cycle. This build life cycles may have its build phases and inside each build, there are different build goals. Also, this ensures that the build target is being removed for a new build and adds the clean target.
- mvn compile: This command is used to compile the source code. It also compiles the classes that are stored at a particular target or class.
- mvn test: Maven also provides the facility of unit testing particular codes. It runs the tests using suitable testing frameworks.
- mvn deploy
Maven also has the facility of deploying the code for the project. This deployment is done in an integration or release environment. It copies all final package to the remote repository and it becomes available for sharing with other developers.
- mvn site
This command builds a site which is based upon the information on the project’s pom.
The document that is generated can be seen under the target/site.
- Run a maven build job
Once the maven project is complete, the build activities like compiling code, running tests and packaging are done here. Running the below command will take care of all these activities.
- maven.javadoc.skip
This command is used to skip the generation and packaging of any specified Javadoc jar file.
Intermediate Commands
Let us discuss the intermediate commands.
- mvn help: describe – This command helps in getting the attributes of the plugin. All information regarding the plugin can be found by this maven command.
- mvn help:effective-pom – By making use of this command a user can get an effective POM as an XML. This XML can be used for current build and for active profiles which are present in the project.
- mvn dependency: analyze – To analyze the dependencies that are present in the project in respect to prints that are unused, outdated dependencies and so on this command can be used.
- mvn dependency: tree – This command prints the complete tree of dependencies of the complete project. This is useful in getting all the transitive dependencies and gets all conflicts if there are any due to version. This command brings all the different dependencies.
- source. skip: This maven command skips the complete packaging of source jars in the project.
- maven.test. skip – If a user wants to skip a particular test script from compilation and the execution of all tests then this command can be used. It can skip both unit and integration for the test for any application
- groups={TestNG Group Name(s)} – This command specifies the TestNG group of unit tests that are present and which will be executed in the current build. It runs the entire group of the test. If there is no group specified then all the unit tests will be executed.
- prepare-package – Using this maven command all activities are performed for any operation which is required to prepare any package.
- pre-integration-test – It performs all activities which are necessary for the integration tests which will be executed. It may also clean up the environment.
- mvn help:effective-pom – Run this command to get the most minimal Maven project pom.xml.
Advanced Commands
Below is the list of advanced commands.
- Resuming builds: To get this option working and resume the builds the -rf or -resume option can be used. It can be used if a user is working with a large multi-module project and the user wants to restart the build any project.
- Making a subset of projects: When a user specifies the -am option Maven builds all the different projects that a specified project has. It can have projects that are dependent on the main project directly or indirectly.
- Creating a Maven Wrapper: There are two way to create a maven Wrapper
- cd {your-project}
- mvn -N io.takari:maven:wrapper
This command helps in creating a Maven wrapper for a particular project with the latest available Maven versions
Tips and Tricks to Use Command
- Maven pl option: This command is used to build specific reactor projects. There is no need to run the entire project.
- Debugging Unit tests: The maven-surefire-plugin helps in running unit tests. This plugin invokes the test phase and also helps in building the life cycle.
Conclusion
It is one of the best tools to project creation, unit testing, and its deployment. It offers all these steps in one tool. It provides better debugging, better collaboration and more powerful builds. It also helps in reducing duplication with more consistent project structure. In the end, it acts to be better dependency management with better hierarchical dependency trees which includes all transitive dependencies. It is one of the best options in all the available tools.
Recommended Articles
This has been a guide to Maven Commands. Here we have discussed basic, intermediate as well as advanced Maven Commands along with tips and tricks to use. You may also look at the following article to learn more –