Introduction to Maven Build Command
The maven build command is used in a project management tool based on the project object model, and it is used in dependency, documentation, and project build. It is a tool used for managing and building the project, which was java based. This is used to easier the work of java developers working on the building project.
Overview of Maven Build Command
We are using the build command when building the project from the command line. We are executing this command from where our pom file exists. We are passing the goal or phase of the life cycle as a parameter to this command. Maven is reading the pom file and resolving the project dependencies. These are validating dependencies from the local repository; suppose it is available, then we can use the same.
Suppose the dependency is not available, then into the build reactor of our local repo, then maven will be downloading the dependent artifacts from the central repository from the specified to the local repository. Maven executes all the life cycle phases until a specified will exists. Suppose the clean install command of maven is triggered at the time of jar packaging, then this will include the sources compilation executing tests from compiled files from jar files. Then install phase will install the resulting artifact from the local repository.
- The maven will contain the multiple commands which were we are executing. Maven is a build of a life cycle, build goals, and build phases.
- Maven is a widely used project model to manage and build projects based on maven.
Types of Maven Build Command
Below are the types of maven commands which were used at the time of the building project as follows.
1. mvn –version
To check the installed version of maven we are using the below command. This command will be printing the maven as well as the java version which was, which we have installed on the server. In the below example, we can see we are using two commands for checking the version of maven.
2. mvn clean
The maven clean command is used to clean and removes the project and files which were generated from the previous build as follows. The maven clean command will first be downloading all the dependencies which were required for our project.
3. mvn compile
The maven compiles command is used to compile the source code of the project. The maven compiles command will first be downloading all the dependencies which were required for our project.
4. mvn test-compile
This command is used to compile the test source code from our project as follows.
5. mvn test
This Command is used to run the test of our project, we will running test module as follows.
6. mvn package
This command is used to create jar or war file of our project for converting the same into file format which was distributable.
7. mvn install
This command is used to deploy the war or jar files into the local repository as follows.
8. mvn deploy
This command is used to copy the packaged jar or war files from the remote repository after compiling or building a project.
9. mvn pre-clean
This command is used to clean the project before building the project.
Maven Build Command Structure
The maven build command consists the two elements. First is the mvn and the other is the build phase, build goals and build life cycle. Below is the structure example as follows. The below command shows the maven command to execute by using the build cycle as follows.
In the below example, we are installing the clean life cycle with the install build phase which is default as follows.
mvn clean install
Life Cycle of Maven Build Command
Below is the representation of the maven build command life cycle as follows. It will consist the below steps.
- Validate: This phase is used to validate the structure of the project is correct or not, it will check all the dependencies are downloaded or not.
- Compile: This phase is used to compile the source code and convert the java files to class and it will be storing the classes in the folder.
- Test: This phase is used to run the unit test.
- Package: This step packages the compiled code into the format which was distributed like a war or jar file.
- Integration test: In this phase, we are running the integration test on a specified project.
- Verify: This step will run a check to verify that our project is valid and it will meet all the standards.
- Install: This step will install the packaged code from the maven repository which was local.
- Deploy: It will copy the packaged code from the repository which was local.
Maven is following the sequential order for executing the command where we are running all steps from n number of steps. It will be verifying the project along with the test of integration.
Maven Build Command Test
The maven test command is used to run the specified test from the project. The below example shows how we can run the test as follows.
The below example shows how we can compile and test the source code from the maven build command as follows.
Maven Command Line Options
To find the command line options of maven we need to execute the help command with the maven command as follows. At the time executing the help command with maven it will be printing the below result as follows.
The below example shows we are using the –o option with the mvn package command as follows.
mvn –o package
The below example shows we are using the –q option with mvn package command as follows.
mvn –q package
The below example shows we are using the –X option with mvn package command as follows.
mvn –X package
Given below are the FAQs mentioned:
Q1. What is the use of the maven build command in the java project?
Answer: Maven contains multiple commands, by using those commands we are managing the project source code.
Q2. Which phases are involved in the life cycle of maven build command?
Answer: The maven build contains the eight phases into it, i.e. install, deploy, compile, validate, test, verify, integration test, and package.
Q3. How can we find the command line options for the maven build command?
Answer: We are finding all usable options of the maven build command by using a command of maven –help.
At the time of building the maven project from the command line, we are using the maven build command. The maven build command is used in a project management tool based on the project object model and is used in dependency, documentation, and build of the project.
This is a guide to Maven Build Command. Here we discuss the introduction and types of Maven Build Command along with structure, life cycle, test, and options. You may also have a look at the following articles to learn more –