Thursday, August 19, 2021

Difference between mvn install, release and deploy commands in Maven - Examples

Even though there are a couple of powerful build and deployment tools that exist for Java applications like Gradle or ANT, It seems Maven is the king of them. I have used it in several Java projects over the years and it was initially ANT, but now they all use Maven with few Scala projects using Gradle. When you work with Maven you know that there are lots of commands to remember, especially if you are working on the command line. The thee Maven build commands which often confuses Java developers are mvn install, mvn release, and mvn deploy.

Many Java developers are never sure which one put the artifact on the remote repository, local repository, and tag the source code in SCM like SVN. In this article, I'll explain the purpose of mvn install, mvn release, and mvn deploy command and some key differences between them.

Btw, if you are just starting with Maven, I suggest you first go through a comprehensive Maven course like Apache Maven: Beginner to Guru course to learn some fundamentals. It will not only help you to build and deploy your Java and Spring Boot projects using Maven but also learn Maven itself in depth.


Maven Install Command: mvn install

The mvn install command runs the install plugin used in the install phase to add artifact(s) to the local repository. The Install Plugin uses the information in the POM (groupId, artifactId, version) to determine the proper location of the artifact within the local repository.

The local repository is the local cache where all artifacts needed for the build are stored. By default, it is located in the user's home directory (~/.m2/repository) but the location can be configured in ~/.m2/settings.xml using the <localRepository> element

The mvn deploy runs the deploy plugin which deploys an artifact to the remote repository. A project may include the main jar and associated sources and Javadoc jars.

abc-1.0.jar
abc-1.0-sources.jar
abc-1.0-javadoc.jar

The sources jar contains the Java sources, and the Javadoc jar contains the generated Javadoc. To include these files in your deployment, set the sources and Javadoc parameters to the paths to the sources and Javadoc jar files. If you want to learn more about this plugin, please read Maven: The Definitive Guide, one of the must-read Maven books for Java developers.




Maven Release Command: mvn release

The mvn release command runs the release plugin which is responsible for tagging the current code in the source control. In order to use the maven release plugin, you need to add the SCM section with a developerConnection which contains the URL of the source control management system pointing to the folder containing the pom.xml.

The URL should be prefixed with scm:scm-provider e.g. if you are using SVN as source control the prefix would be scm:svn as shown below:

<project>
...
<scm>
<developerConnection>scm:svn:
https://svn.mycompany.com/repos/myapplication/trunk/mycomponent/<
/developerConnection>
</scm>
</project>

The maven release plugin has several goals, you can run each of them depending upon your need e.g.
  • release:clean : performs clean-up after a release preparation.
  • release:prepare  : prepare for a release in SCM.
  • release:rollback : rollback a previous release.
  • release:perform :  perform a release from SCM.


You can even do a dry run before making a release by using mvn release:prepare -DdryRun=true command



This will ask all the same questions, run the same tests, and output a copy of how the POMs will look after transformation. You can check the output and review the POMs, then run:

mvn release:clean

This will remove all of the files created above, and the project will be ready to execute the proper release


mvn release:prepare

This command prepares for a release in SCM. It goes through several phases to ensure the POM is ready to be released and then creates a tag in SVN which can be used by release:perform to make a release.


mvn release:perform

This is the command which actually does the release by downloading the tagged version from SCM e.g. SVN or CVS or Git. We usually call this command after release:prepare, which creates the tag in SCM but you can also release any specified tag created previously.


mvn release:rollback

This command rollbacks the changes made by a previous release. This command needs the previous release descriptor release.properties to be available in the local working copy. So, if you think the release has not gone well, you can use this command to roll it back.

Here is a nice diagram that explains the whole release process using Maven in a Java application, btw, if you are not familiar with maven fundamentals like a local repository, the remote repository, goals, phases, convention over configuration, etc then you should read Maven Essentials Course to learn more about key maven concepts.

Difference between mvn install, release and deploy in Maven



In short,

The mvn install command will put the JAR or WAR file of your project into the local repository. Since this repository is only accessible to any application running on the same machine, they can use your project as a dependency.

On the other hand, the mvn release command will tag your current code in configured SCM (Source Control Management) like SVN, and change the version number in your project's pom.xml.

While the last mvn deploy command will put the JAR or WAR file of your project into a remote repository e.g. maven central or your company's nexus repository for sharing with other projects.

 If you want to learn more details like which properties file is created by the release plugin and how these steps communicate with each other, you can further join these free Maven online courses to learn more about the build and release process in Maven.


Other Maven tutorials and articles you may like
  • Top 10 Maven Plugin Every Java developer should know (list)
  • What is the difference between Maven, ANT, and Jenkins? (answer)
  • How to install Maven in Windows 10? (steps)
  • How to fix the Maven Eclipse Dependency search not working issue? (solution)
  • How to increase the heap size of Maven? (steps)
  • How to create or modify build.xml in ANT? (tutorial)
  • How to build a Java project using ANT? (article)
  • Top 5 Apache Maven Free Books for Java developers (books)
  • 9 Maven Concepts Every Java developer should learn (maven concepts)
  • 3 Maven Eclipse Tips for Java developers (maven tips)

References

Maven Release Plugin

P. S. - If you have any other important maven command example, feel free to share with us in the comment section. If you have any doubt about mvn clean install, or mvn install, or the mvn release, or mvn deploy command, feel free to ask in the comments. 


No comments :

Post a Comment