Difference between mvn install, release and deploy in Maven

Even though there are a couple of powerful build and deployment tools exists for Java applications e.g. Gradle or ANT, It seems Maven is the king of them. I have used 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 difference between them.


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 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 book 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 them 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 create 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 release has not gone well, you can use this command to roll it back.

Here is a nice diagram which explains the whole release process using Maven in a Java application, btw, if you are not familiar with maven fundamentals like local repository, the remote repository, goals, phases, convention over configuration etc then you should read Maven Essentials 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 this your project as a dependency.

On the other hand, mvn release command will tag your current code in configured SCM (Source Control Management) e.g. 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 remote repository e.g. maven central or your company's nexus repository for sharing with other projects. If you want to learn more details e.g. which properties file is created by release plugin and how these steps communicate with each other, you can further read Maven: The Definitive Guide to learn more about 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 Maven Eclipse Dependency search not working issue? (solution)
  • How to increase heap size of Maven? (steps)
  • How to create or modify build.xm in ANT? (tutorial)
  • How to build a Java project using ANT? (article)
  • Top 5 Apache Maven Free Books for Java developers (books)

References
Maven Release Plugin


No comments :

Post a Comment