In this tutorial, you will learn about the main difference between the “mvn package” and “mvn install” commands. As well as how to use both commands on your computer.
“mvn package” vs “mvn install”
The main difference between the “mvn package” and “mvn install” commands is that mvn package command will compile the source and will package it in its distributable formats, such as a JAR or WAR. The mvn install command, however, additionally to compiling the source code and packaging it into a JAR or a WAR, it will also install the package into the local repository, for use as a dependency in other projects locally.
For video lessons on how to secure your Spring Boot application with OAuth 2.0. and Spring Security 5, please checkout my complete video course OAuth 2.0. in Spring Boot applications.
Below is what these two commands do according to maven documentation:
- mvn package – take the compiled code and package it in its distributable format, such as a JAR,
- mvn install – install the package into the local repository, for use as a dependency in other projects locally.
Both of these maven commands will compile your code, clean the /target folder, and will place a new packaged JAR or WAR into that /target folder. It is just the “mvn install” command will also install the package into the local maven repository, for use as a dependency in other projects locally.
How to Use “mvn package” Command
The “mvn package
” command is used in Maven to build the project and create a distributable package, such as a JAR, WAR or EAR file, that can be deployed to a server. Below is a step-by-step guide on how to use the “mvn package” command:
- Open a command prompt or terminal window and navigate to the root directory of your Maven project.
- Ensure that you have a valid Maven installation by typing “
mvn -version
” and checking the output. If Maven is not installed, download and install it from the official Apache Maven website. - Type “mvn package” in the command prompt or terminal window and press enter. Maven will then start the build process, which involves compiling the source code, running tests, and creating a distributable package.
- Once the build process is complete, Maven will create a package in the “target” directory of your project. The name of the package will depend on the type of project you are building. For example, if you are building a JAR file, the package will be named “<project-name>-<version>.jar”, where <project-name> and <version> are the name and version of your project, respectively.
- You can then deploy the package to a server by copying it to the appropriate directory. For example, if you are building a web application and have created a WAR file, you can deploy it to a Tomcat server by copying it to the “webapps” directory of your Tomcat installation.
- You can also use the “
mvn clean
” command to clean up the target directory and remove any generated files. This is useful if you want to start the build process from scratch.
How to Use “mvn install” Command
The “mvn install
” command is used to install the project artifact into the local Maven repository. This allows other projects on your local machine to use the artifact as a dependency. Here is a step-by-step guide on how to use the “mvn install” command:
- Open a command prompt or terminal window and navigate to the root directory of your Maven project.
- Ensure that you have a valid Maven installation by typing “mvn -version” and checking the output. If Maven is not installed, download and install it from the official Apache Maven website.
- Type “
mvn install
” in the command prompt or terminal window and press enter. Maven will then start the build process, which involves compiling the source code, running tests, and creating a distributable package. - Once the build process is complete, Maven will install the package to the local Maven repository. This repository is typically located in the “.m2” directory of your user directory, and the artifact will be stored in the appropriate subdirectory based on the group ID, artifact ID, and version of your project.
- You can then use the installed artifact as a dependency in other Maven projects on your local machine. To do this, simply add the dependency information to the “pom.xml” file of the project that requires the artifact. The dependency information should include the group ID, artifact ID, and version of the installed artifact.
- If you make changes to your project and want to install a new version of the artifact, simply run the “mvn install” command again. Maven will overwrite the previous version of the artifact in the local repository.
- You can also use the “
mvn clean
” command to clean up the target directory and remove any generated files before running the “mvn install” command. This is useful if you want to start the build process from scratch.
Frequently Asked Questions
- What is the use of “mvn clean” command in Maven?
The “mvn clean
” command is used to clean up the target directory and remove any generated files before building the project. This is useful if you want to start the build process from scratch. - How do I specify the output directory for the “mvn package” command in Maven?
By default, the “mvn package
” command will create the package in the “target” directory of your project. However, you can specify a different output directory using the “-Dmaven.build.directory=<directory>
” option. - How do I skip tests while running the “
mvn package
” command in Maven?
You can skip running tests while running the “mvn package” command by using the “-DskipTests” option. For example, “mvn package -DskipTests
“. - How do I specify a different Maven installation while running the “mvn package” or “mvn install” command?
You can specify a different Maven installation by using the “-Dmaven.home=<path>” option. For example, “mvn package -Dmaven.home=/usr/local/maven
“.