How to Check and Update Newer Versions for Dependencies in Maven Projects
This tutorial demonstrates how to use different Maven commands for checking and updating the dependency and plugin versions in Maven projects.
Join the DZone community and get the full member experience.
Join For FreeWith the passing of time, new versions of the dependencies are released into the market. We need to update the respective dependencies versions in the project as these versions have new changes and fixes for the security vulnerabilities. It is better to update the dependencies frequently in the project.
Now arises the question:
“How do I come to know which dependencies have a new version released and need to be updated?”
It is a very tedious task to manually check which dependencies have released a new version and then go to your pom.xml file and update each manually. If you have a big project and are using multiple dependencies, it is very tedious 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 that by running you will get to know which dependencies in your project need to be upgraded with the latest version release. We need to simply open the command prompt or terminal in our local machine, run the following Maven command, and it will show you all the dependencies that have newer versions so you can update them in your project! How cool it is!
mvn versions:display-dependency-updates
I got a huge relief when I learned about this, and now I simply run the command and get all the updates about the dependencies in the project.
Let’s run this command and check the 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.
It can be seen in the above screenshot that Maven displays that there are newer versions available for the following dependencies with their respective older and newer versions:
Dependency Name | Old Version | New Version |
---|---|---|
jackson-databind | 2.15.2 | 2.17.2 |
io.qmeta.allure | 2.27.0 | 2.28.1 |
json-schema-validator | 5.4.0 | 5.5.0 |
rest-assured | 5.4.0 | 5.5.0 |
data-faker | 2.2.2 | 2.3.1 |
commons-lang3 | 3.14.0 | 3.15.0 |
log4j-api | 2.22.1 | 3.0.0-beta2 |
log4j-core | 2.22.1 | 3.0.0-beta2 |
testng | 7.10.0 | 7.10.2 |
It’s so relaxing to see the results in a few seconds and find out exactly what dependencies in your project need to be updated to the latest version.
Now arises another question:
"Is there any Maven command to check and update the plugin versions in pom.xml?"
The answer to this question will be provided in the following section of this article.
How to Check for Availability of Newer Versions for the Plugins in the Maven Project
Similarly to how we ran the command for finding the latest versions of the dependencies, we can run another Maven command and find out about the updates related to the Maven plugins used in the project.
mvn versions:display-plugin-updates
On executing the above command on the root folder of the project, the following output is printed.
It can be seen that Maven has provided us with all the plugin-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:
- Manually update the versions in pom.xml.
- Use the 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:
- Dependency versions are provided in the Dependency itself.
- Dependency versions are updated in the properties block in pom.xml.
To Update the Dependency Version in the Dependency Itself
If you have the versions defined in the dependency itself, run the following command:
mvn versions: use-latest-versions
In the pom.xml before running the command, we can see that the version for testng
is 7.10.0
and 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.
Now let’s check the pom.xml file and see if the versions were updated to the latest one.
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, which is 5.5.0
.
Maven created a backup pom.xml file (check the file named pom.xml.versionsBackup in the screenshot below) in the root folder, just in case 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 the file named pom.xml.versionsBackup in the screenshot below) in the root folder just in case we want to revert back the changes.
Summary
The tedious task of manually finding the newer versions of the respective dependencies and updating them 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.
Published at DZone with permission of Faisal Khatri. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments