Update maven dependencies automatically

Summary

The concept is when we have a very old maven project having dependencies with the old version but somehow need to update now then by following these steps we can easily up to date our project dependencies to the latest. We can do it manually too but that will be tedious and time-consuming.

Checking newer versions of a dependency

Go to the root folder of your project and open the terminal then run the below maven command to check the latest available versions of your project dependencies.

In my case, I have the below pom.xml file in my project which has some old dependencies as below:

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <selenium-version>4.8.3</selenium-version>
        <cucumber-version>7.11.2</cucumber-version>
        <cucumber-junit-version>7.11.2</cucumber-junit-version>
        <testng-version>7.7.1</testng-version>
        <extent-report-version>5.0.8</extent-report-version>
        <poi-xml-version>5.2.3</poi-xml-version>
        <log4j-core-version>2.19.0</log4j-core-version>
        <log4j-api-version>2.19.0</log4j-api-version>
        <extent-report-cucumber-adapter-version>1.7.0</extent-report-cucumber-adapter-version>
        <cucumber-testng-verison>7.11.1</cucumber-testng-verison>
        <java-home>C:\Program Files\Java\jdk-16.0.2\bin\javac</java-home>
        <java-version>1.8</java-version>
        <cucumber-pdf-plugin-version>1.12.0</cucumber-pdf-plugin-version>
        <maven-compiler-plugin-version>3.10.1</maven-compiler-plugin-version>
        <lombok-version>1.18.26</lombok-version>
        <maven-surefire-plugin-version>3.0.0-M5</maven-surefire-plugin-version>


    </properties>

    <dependencies>

        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>${cucumber-version}</version>
        </dependency>

        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>${cucumber-junit-version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>${selenium-version}</version>
        </dependency>

        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-testng</artifactId>
            <version>${cucumber-testng-verison}</version>
        </dependency>

        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>${testng-version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.aventstack</groupId>
            <artifactId>extentreports</artifactId>
            <version>${extent-report-version}</version>
        </dependency>

        <dependency>
            <groupId>tech.grasshopper</groupId>
            <artifactId>extentreports-cucumber7-adapter</artifactId>
            <version>${extent-report-cucumber-adapter-version}</version>
        </dependency>


        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>${poi-xml-version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>${log4j-core-version}</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok-version}</version>
            <scope>provided</scope>
        </dependency>

    </dependencies>
mvn versions:display-dependency-updates

it will show all the latest versions of the dependencies the project has.

[INFO] The following dependencies in Dependencies have newer versions:
[INFO]   com.aventstack:extentreports .......................... 5.0.8 -> 5.0.9
[INFO]   io.cucumber:cucumber-java ........................... 7.11.2 -> 7.12.0
[INFO]   io.cucumber:cucumber-junit .......................... 7.11.2 -> 7.12.0
[INFO]   io.cucumber:cucumber-testng ......................... 7.11.1 -> 7.12.0
[INFO]   org.apache.logging.log4j:log4j-core ................. 2.19.0 -> 2.20.0
[INFO]   org.seleniumhq.selenium:selenium-java ................. 4.8.3 -> 4.9.0
[INFO]   tech.grasshopper:extentreports-cucumber7-adapter ..... 1.7.0 -> 1.12.0

Checking newer versions of plugin dependencies

To check the plugin dependencies run the below command the same way.

mvn versions:display-plugin-updates

it will return the list of dependencies that are the latest.

[INFO] The following plugin updates are available:
[INFO]   maven-compiler-plugin ............................ 3.10.1 -> 3.11.0
[INFO]   maven-surefire-plugin ........................... 3.0.0-M5 -> 3.0.0

Update the Dependency version in the dependency itself

Consider the below pom.xml file

<dependencies>
        <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-java -->
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>7.2.3</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-junit -->
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>7.2.3</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>4.1.2</version>
        </dependency>

Now perform the update command to get into the latest version.

mvn versions:use-latest-versions

now check the pom file and found that the below-updated version of dependencies.

[INFO] Updated io.cucumber:cucumber-java:jar:7.2.3 to version 7.12.0
[INFO] Updated io.cucumber:cucumber-junit:jar:7.2.3 to version 7.12.0
[INFO] Updated org.seleniumhq.selenium:selenium-java:jar:4.1.2 to version 4.9.0

and the pom file looks like below

    <dependencies>
        <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-java -->
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>7.12.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-junit -->
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>7.12.0</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>4.9.0</version>
        </dependency>

Update the Dependency version in the properties section

To update the version from the properties section run the below command.

mvn versions:update-properties
[INFO] Updated ${cucumber-testng-verison} from 7.11.1 to 7.12.0
[INFO] Updated ${cucumber-junit-version} from 7.11.2 to 7.12.0
[INFO] Updated ${maven-compiler-plugin-version} from 3.10.1 to 3.11.0
[INFO] Property ${cucumber-pdf-plugin-version}: Leaving unchanged as 1.12.0
[INFO] Updated ${log4j-core-version} from 2.19.0 to 2.20.0
[INFO] Property ${lombok-version}: Leaving unchanged as 1.18.26
[INFO] Updated ${selenium-version} from 4.8.3 to 4.9.0
[INFO] Updated ${extent-report-cucumber-adapter-version} from 1.7.0 to 1.12.0
[INFO] Updated ${cucumber-version} from 7.11.2 to 7.12.0
[INFO] Updated ${extent-report-version} from 5.0.8 to 5.0.9
[INFO] Updated ${maven-surefire-plugin-version} from 3.0.0-M5 to 3.0.0
[INFO] Property ${testng-version}: Leaving unchanged as 7.7.1
[INFO] Property ${poi-xml-version}: Leaving unchanged as 5.2.3

Now check the properties section of the pom file, it will show the latest version for the dependencies.

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <selenium-version>4.9.0</selenium-version>
        <cucumber-version>7.12.0</cucumber-version>
        <cucumber-junit-version>7.12.0</cucumber-junit-version>
        <testng-version>7.7.1</testng-version>
        <extent-report-version>5.0.9</extent-report-version>
        <poi-xml-version>5.2.3</poi-xml-version>
        <log4j-core-version>2.20.0</log4j-core-version>
        <log4j-api-version>2.19.0</log4j-api-version>
        <extent-report-cucumber-adapter-version>1.12.0</extent-report-cucumber-adapter-version>
        <cucumber-testng-verison>7.12.0</cucumber-testng-verison>
        <java-home>C:\Program Files\Java\jdk-16.0.2\bin\javac</java-home>
        <java-version>1.8</java-version>
        <cucumber-pdf-plugin-version>1.12.0</cucumber-pdf-plugin-version>
        <maven-compiler-plugin-version>3.11.0</maven-compiler-plugin-version>
        <lombok-version>1.18.26</lombok-version>
        <maven-surefire-plugin-version>3.0.0</maven-surefire-plugin-version>


    </properties>

The interesting thing is, you will not lose your old pom file maven will save a backup as 'pom.xml.versionsBackup'.