How to check and update newer versions for dependencies in the Maven project?

Mohammad Faisal Khatri
6 min readDec 11, 2022

--

Introduction

Currently, I am working on an open sourced project adding example codes for API Test Automation using rest-assured framework. The Project is available on github.

Checkout End to End testing using Rest Assured blog where I talk about writing end to end tests using rest-assured framework

Since I had created this project long back around a year or so and having been adding example codes as and when I get some free time on weekends.

Checkout the following blog where I have discussed about What is Maven and How to create Maven Project using IntelliJ IDE.

With the passing of time, new versions of the dependencies are released into market and we need to update the respective dependencies versions in the project as these version have new changes as well as fixes for the security vulnerabilities. It is better to update the dependencies frequently in the project. Now, here arises the question, “How do I come to know which dependencies have a new version released and needs to be updated?”

It is a very tedious task to check manually which all dependencies have released a new version and then go to your pom.xml file and update each manually. Now, imagine if you have a big project and are using multiple dependencies, how tedious it would be to search for each dependency and check if there is a version upgrade available?

How to check for availability of newer versions for the dependencies in the Maven project?

Luckily, Maven has provided us with a command which by running you will get to know which all dependencies in your project are needed to be upgraded with a latest version release. Simply we need to open the command prompt or terminal in our local machine and run the following Maven command:

mvn versions:display-dependency-updates

Maven will show you all the dependencies that have newer versions so you could update them in your project! How cool it is!

I got a huge relief when I learned about this command and now I simply run this command and get all the updated about the dependencies in the project and accordingly update them.

Let’s run this command and check what output it delivers. I am running this command on the repository which has API Automation Example Tests and uses multiple dependencies like rest-assured, testng, jackson databind, etc.

We can see towards in the above screenshot, Maven displays that there are newer versions available for the following dependencies with their respective older and newer versions:

  1. jackson-databind — 2.15.2(Old Version) > 2.17.2(New Version)
  2. io.qmeta.allure — 2.27.0 (Old Version) > 2.28.1(New Version)
  3. json-schema-validator — 5.4.0(Old Version) > 5.5.0(New Version)
  4. rest-assured — 5.4.0(Old Version) > 5.5.0(New Version)
  5. data-faker — 2.2.2(Old Version) > 2.3.1(New Version)
  6. commons-lang3–3.14.0(Old Version) > 3.15.0(New Version)
  7. log4j-api — 2.22.1(Old Version) > 3.0.0-beta2(New Version)
  8. log4j-core — 2.22.1(Old Version) > 3.0.0-beta2(New Version)
  9. testng — 7.10.0(Old Version) > 7.10.2(New Version)

It’s so relaxing to see the results in few seconds and find out exactly which of the dependency in your project needs to be updated to latest version.

The other question we get if we need to update the plugins version what do we do?

How to check for availability of newer versions for the plugins in the Maven project?

Like we ran the command for finding the latest versions of the dependencies, similarly we can run another Maven command as mentioned below and find out for the updates for the Maven plugins used in our project.

mvn versions:display-plugin-updates

Let’s run this command using the command prompt and checkout for the updates.

We can see Maven has provided us with all the plugins related information that can be used and updated in pom.xml. It has also listed down the required Maven version for updating the respective plugins.

Updating the Dependency Versions in the project

Now, the next process is to update the dependencies in the project which Maven has provided us with the latest versions for. Now, there are 2 options available to update the dependencies:

  1. Manually update the versions in pom.xml
  2. Use Maven command to update the dependencies automatically

Updating pom.xml with the latest versions of the dependencies using Maven command

There are two cases here in terms of update

  1. Dependency versions are provided in the Dependency itself.
  2. Dependency versions are updated in the properties block in pom.xml.

To update Dependency version in dependency itself

If you have the versions defined in the dependency itself, run the following command:

mvn versions: use-latest-versions

pom.xml before running the command, we can see that the version for testng is 7.10.0 amd for rest-assured it is 5.4.0.

let’s run the mvn versions:use-latest-versions by navigating to the root folder of the repository.

Let’s check pom.xml now and see if the versions got updated to the latest ones

The versions got updated automatically after the command was executed successfully. We can see that testng dependency has the latest version 7.10.2 and rest-assured has the latest version as well that is 5.5.0.

Maven created a backup pom.xml file (Check file named pom.xml.versionsBackup in the below screenshot) in the root folder, just in case, if we want to revert back the changes.

To update Dependency versions that are updated in the properties block:

If you have the versions defined in the properties block in the pom.xml, run the following command:

mvn versions:update-properties

Let’s run the command and check out the automatic update of the versions in the properties block of pom.xml file.

The versions we see in the red color in the above screenshots were updated automatically through the command we just executed.

Maven created a backup pom.xml file(Check file named pom.xml.versionsBackup in the below screenshot) in the root folder, just in case if we want to revert back the changes.

Conclusion

The tedious task of manually finding the newer versions of the respective dependencies used in the project can be easily done using the mvn:versions command. We can sit back and enjoy a hot cup of coffee while Maven updates the dependencies automatically in the project for us.

References:

https://www.mojohaus.org/versions-maven-plugin/

Trainings and Mentoring

Contact me for Paid trainings/Mentoring related to Test Automation and Software Testing, ping me using any of the social media site listed on LinkTree or email me @mohammadfaisalkhatri@gmail.com.

--

--

Mohammad Faisal Khatri

QA with 14+ years of experience in automation as well as manual testing. Freelancer, blogger and open source contributor.