← Back to list

#CodeGuide to keeping JAVA dependencies up-to-date

Introduction

Raja Nagendra Kumar, Code Doctor/Innovator · 2023-05-18 05:27 · 2 claps · 9.2 min read
#maven #gradle #clean-code #ncu
Open on Medium ↗

#CodeGuide to keeping JAVA dependencies up-to-date

Introduction

Maven and Gradle are two of the most popular build automation tools for Java. They both provide a number of features that can help you to automate the build process, including dependency management, task execution, and reporting.

Mavenis a more mature & legacy tool than Gradle, and it is widely used in the Java community. It is based on the concept of a project object model (POM), which is an XML file that describes the project’s dependencies, plugins, and build configuration. Maven uses a convention over configuration approach, which means that it provides a default set of settings that can be used for most projects. This can make it easier to get started with Maven, but it can also make it more difficult to customize the build process.

Gradleis a newer tool than Maven, but it is gaining popularity in the Java community. It is based on the concept of a build script, which is a Groovy or Kotlin file that describes the project’s dependencies, plugins, and build configuration. Gradle uses a more flexible configuration model than Maven, which makes it easier to customize the build process. However, this also makes it more difficult to learn and use Gradle.

In general, Mavenis a good choice for projects that need to be built in a consistent and repeatable way. Gradle(recommended for all new projects i.e. gradle with Kotline DSL) is a good and modern choice for projects that need to be customized or that need to be built quickly.

What is Dependency management?

Dependency management is the process of managing the dependencies of a software project. Dependencies are the libraries, frameworks, and other software components that a project relies on.

Dependency management is a complex and challenging task. There are a number of pain points that developers face when managing dependencies.

Some of the most common dependency management pain points include:

  • Versioning: Dependencies are often released in multiple versions. It can be difficult to track which versions of dependencies are compatible with each other.
  • Security vulnerabilities: Dependencies can contain security vulnerabilities. It can be difficult to keep track of which dependencies have security vulnerabilities and to apply security updates.
  • Complexity: Dependency management can be complex and time-consuming. It can be difficult to manage the dependencies of a large project.
  • Tooling: There are a number of dependency management tools available. However, these tools can be difficult to use and can have different features.
  • Human error: Human error is a common cause of dependency management problems. Developers can accidentally install the wrong version of a dependency or forget to update a dependency.
  • Dependency hell: is a situation where a project cannot be built because it depends on conflicting versions of the same dependency. This can be caused by dependencies being updated independently, or by dependencies being pulled in from different sources.
  • Dependency churn: this is a situation where the dependencies of a project are constantly changing. This can make it difficult to track the dependencies of a project and to ensure that the project is using the latest versions of the dependencies.
  • Dependency sprawl: a situation where a project depends on a large number of dependencies. This can make it difficult to manage the dependencies of a project and to understand the impact of changes to dependencies.
  • Performance issues: Outdated dependencies can be slower than newer versions. This can lead to performance problems in applications.
  • Maintainability: Dependency management can be a complex and time-consuming process. This can make it difficult to maintain projects and keep them up to date.

Timely Dependency management is an important task in#cleancode world that can help to ensure the quality and security of software projects. However, it is a complex and challenging task that can be fraught with the above pain points.

Here are some tips for managing dependencies effectively:

  • Use a dependency management tool: A dependency management tool can help you to track dependencies, manage versions, and apply security updates.
  • Use a consistent versioning strategy: A consistent versioning strategy can help you to avoid conflicts between dependencies.
  • Test your dependencies: Before you use a dependency, test it to make sure that it works with your project.
  • Keep your dependencies up to date: Keep your dependencies up to date to avoid security vulnerabilities.
  • Document your dependencies: Document your dependencies so that you know what you are using and where to find them.

By following these tips, you can help to manage dependencies effectively and avoid the pain points that can plague dependency management.

Bit of learning from NodeJS/NPM world

In the world of *nodejsand `npmncu i.e. *npm-check-updates`, is a command-line tool that helps you to check for updates to your npm packages (which are listed in package.json). It can be used to list all of the packages that have updates available, or to only list packages that have security updates available.

The -u option tells ncuto upgrade all of the packages that have updates available. This can be a useful way to keep your project up to date with the latest security patches and bug fixes.

Here is an example of how to use ncuwith the -u option:

ncu -u

This will list all of the packages that have updates available, and then updates package.jsonfile.

Here is an example of the output that ncuwill produce:

core-js ^2.6.9 → ^3.1.4
tslib ^1.14.1 → ^2.3.0

This output shows that the core-js and tslib packages have updates available. The first column shows the current version of the package, and the second column shows the latest version that is available.

You can also use ncuto list all of the packages that have security updates available. To do this, use the -s option:

ncu -s

This will list all of the packages that have security updates available, but it will not upgrade them.

Here is an example of the output that ncuwill produce:

core-js ^2.6.9 → ^3.1.4

This output shows that the core-js package has a security update available.

ncuis a useful tool for keeping your npm packages up to date. It is easy to use and can be used to list all of the packages that have updates available, or to only list packages that have security updates available.

Like in nodejs the world, there are ways to update javadependencies. We all talk about the same for applications that are built using mavenor gradle.

Dependencies Management in Maven

Maven is a build automation tool for Java projects. It provides a number of features that can help you to automate the build process, including dependency management, task execution, and reporting.

One of the most important features of Maven is dependency management. Maven allows you to specify the dependencies that your project needs and it will automatically download and install those dependencies when you build your project.

Maven provides two commands that you can use to update your dependencies: mvn versions:display-dependency-updates and mvn versions:use-latest-releases.

mvn versions:display-dependency-updates

The command will list all of the dependencies in your project that have newer versions available. This command does not update pom.xmlfile. This command can be used to identify dependencies that need to be updated.

The output of this command will look something like this:

[INFO] ------------------------------------------------------------------------
[INFO] Building MyProject
[INFO]    task-segment: [versions:display-dependency-updates]
[INFO] ------------------------------------------------------------------------

[INFO] The following dependencies have newer versions:

[INFO]   org.apache.commons:commons-lang3:3.8.1 => 3.9
[INFO]   org.slf4j:slf4j-api:1.7.25 => 1.8.0-beta2

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1 second
[INFO] Finished at: 2023-05-17T17:32:43-07:00
[INFO] Final Memory: 3M/16M
[INFO] ------------------------------------------------------------------------

If the need is auto update pom.xml files especially used by CI/CD pipelines is

mvn versions:use-latest-releases

The output of this command will look something like this:

[INFO] ------------------------------------------------------------------------
[INFO] Building MyProject
[INFO]    task-segment: [versions:use-latest-releases]
[INFO] ------------------------------------------------------------------------

[INFO] The following dependencies have been updated to the latest release:

[INFO]   org.apache.commons:commons-lang3:3.8.1 => 3.9
[INFO]   org.slf4j:slf4j-api:1.7.25 => 1.8.0-beta2

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1 second
[INFO] Finished at: 2023-05-17T17:33:15-07:00
[INFO] Final Memory: 3M/16M
[INFO] ------------------------------------------------------------------------

In case version numbers are in properties, the to update these properties use

mvn versions:update-properties

would output similar to

INFO] Scanning for projects...
[INFO] ----------------------------------------------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] mutate                                                                                                     [pom]
[INFO] kata                                                                                                       [jar]
[INFO] fix                                                                                                        [jar]
[INFO]
[INFO] --------------------------------------------< mutation.test:mutate >--------------------------------------------
[INFO] Building mutate 1.0-SNAPSHOT                                                                               [1/3]
[INFO]   from pom.xml
[INFO] ----------------------------------------------------[ pom ]-----------------------------------------------------
[INFO]
[INFO] --- versions:2.16.0:update-properties (default-cli) @ mutate ---
[INFO] Property ${junit.version}: Leaving unchanged as 4.13.2
[INFO] Updated ${mockito.version} from 3.12.4 to 5.2.0
[INFO] Updated ${pitest.version} from 1.7.0 to 1.14.1
[INFO] Copying mutation.test:mutate:pom:1.0-SNAPSHOT to project local repository
[INFO] Copying mutation.test:mutate:pom:consumer:1.0-SNAPSHOT to project local repository
[INFO]

Full details of this maven plugin are at https://www.mojohaus.org/versions/versions-maven-plugin/index.html

Dependencies Management in Gradle

Gradle is a build automation tool for Java projects. It provides a number of features that can help you to automate the build process, including dependency management, task execution, and reporting.

One of the most important features of Gradle is dependency management. Gradle allows you to specify the dependencies that your project needs and it will automatically download and install those dependencies when you build your project.

Over time, new versions of dependencies are released. It is important to keep your dependencies up to date in order to protect your project from security vulnerabilities and to take advantage of new features and bug fixes.

Gradle provides two plugins that you can use to update your dependencies: [com.github.ben-manes.versions](https://github.com/ben-manes/gradle-versions-plugin) and [se.patrikerdes.use-latest-versions](https://plugins.gradle.org/plugin/se.patrikerdes.use-latest-versions).

Add these plugins in build.gradle.kts script file in the plugins block

plugins {
  id("com.github.ben-manes.versions") version "0.46.0"
  id("se.patrikerdes.use-latest-versions") version "0.2.18"
}

Once these plugins are added, it would add the following tasks to manage your dependencies:

  • dependencyUpdates: Displays a list of dependencies that have newer versions available
  • useLatestVersions: Updates all dependencies to the latest version in build.gradle.kts file

Here is an example of how to use the dependencyUpdates task to display a list of dependencies that have newer versions available.

gradle dependencyUpdates

This would output a list of all dependencies that have newer versions available.

------------------------------------------------------------
: Project Dependency Updates (report to plain text file)
------------------------------------------------------------

The following dependencies are using the latest milestone version:
 - ch.qos.logback:logback-classic:1.4.7

The following dependencies have newer versions:

  - org.junit.jupiter:junit-jupiter-api:5.8.2 => 5.8.3
  - org.junit.jupiter:junit-jupiter-engine:5.8.2 => 5.8.3

Gradle release-candidate updates:
 - Gradle: [8.1.1: UP-TO-DATE]

Here is an example of how to use the useLatestVersionstask to display a list of dependencies that have newer versions available.

gradle useLatestVersions

This would output a list of all dependencies that have newer versions available and also updates build.gradle.kts.

------------------------------------------------------------
: Project Dependency Updates (report to plain text file)
------------------------------------------------------------

The following dependencies are using the latest milestone version:
 - ch.qos.logback:logback-classic:1.4.7

The following dependencies have later milestone versions:
 - org.junit:junit-bom [5.9.2 -> 5.10.0-M1]
     https://junit.org/junit5/
 - org.junit:junit-bom [5.9.3 -> 5.10.0-M1]
     https://junit.org/junit5/
 - org.junit.jupiter:junit-jupiter [5.9.2 -> 5.10.0-M1]
     https://junit.org/junit5/

Gradle release-candidate updates:
 - Gradle: [8.1.1: UP-TO-DATE]

Generated report file edu\build\dependencyUpdates\report.json

Generated report file edu\build\dependencyUpdates\report.xml

Generated report file edu\build\dependencyUpdates\report.txt

Conclusion

In conclusion, effective dependency management is crucial in Java development to ensure the stability, efficiency, and security of software projects. Managing dependencies involves keeping track of external libraries, frameworks, and modules used in a project and ensuring they are up to date.

These tools provide the functionality to automate the detection of outdated dependencies, suggest the latest versions available, and even perform updates automatically. This not only saves time and effort but also reduces the risk of using vulnerable or outdated components that may impact the security or performance of the software.

Maintaining up-to-date dependencies brings several benefits, including access to new features, bug fixes, performance improvements, and security patches. It also ensures compatibility with the latest versions of other libraries or frameworks used in the project, minimizing conflicts and compatibility issues.

In summary, dependency management tools provide valuable assistance to Java developers and CI/CD automation in keeping their projects up to date and minimizing potential issues caused by outdated or incompatible dependencies. By embracing these plugins and practices, developers can enhance their productivity, maintain a robust codebase, and deliver high-quality #cleancode software solutions.

The knowledge of effective dependency management in Java is a cross-project skill that can be applied to every product and project across the Java ecosystem. Regardless of the size or complexity of the software, managing dependencies efficiently is vital for maintaining a stable, scalable, and secure codebase.

The principles and best practices of dependency management discussed in the article are universally applicable. Understanding how to track, update, and resolve dependencies ensures that developers can leverage the latest features, bug fixes, and security patches offered by external libraries and frameworks.

References:

  1. npm-check-updates:

Link: [https://www.npmjs.com/package/npm-check-updates](https://www.npmjs.com/package/npm-check-updates)

Description: npm-check-updates is an npm package that allows you to check for and upgrade your package.json dependencies to their latest versions. It provides a command-line interface and supports different upgrade strategies, such as upgrading all dependencies or only specific ones.

  1. versions-maven-plugin:

Link: [https://www.mojohaus.org/versions/versions-maven-plugin/index.html](https://www.mojohaus.org/versions/versions-maven-plugin/index.html)

Description: The versions-maven-plugin is a plugin for Apache Maven, a popular build automation tool for Java projects. This plugin helps in managing version numbers of artifacts (e.g., libraries, dependencies) within your Maven projects. It provides various goals to update, validate, and display version information, allowing you to efficiently handle versioning in your project.

  1. gradle-versions-plugin:

Link: [https://github.com/ben-manes/gradle-versions-plugin](https://github.com/ben-manes/gradle-versions-plugin)

Description: The gradle-versions-plugin is a Gradle plugin developed by Ben Manes. It helps you identify and update outdated dependencies in your Gradle build scripts. The plugin provides tasks and reports to display information about outdated dependencies, making it easier for you to keep your project up-to-date with the latest versions of libraries.

4. use-latest-versions Gradle Plugin:

Link: [https://plugins.gradle.org/plugin/se.patrikerdes.use-latest-versions](https://plugins.gradle.org/plugin/se.patrikerdes.use-latest-versions)

Description: The use-latest-versions Gradle plugin is a plugin available on the Gradle Plugin Portal. It allows you to automatically update the dependencies in your Gradle build files to their latest versions. By using this plugin, you can ensure that your project is always using the most recent versions of the specified dependencies without manually updating the version numbers.


메타데이터
post_id
da2ad26b1f3f
slug
a-guide-to-keeping-your-java-dependencies-up-to-date-da2ad26b1f3f
url
https://medium.com/@nagendra.raja/a-guide-to-keeping-your-java-dependencies-up-to-date-da2ad26b1f3f
canonical_url
https://medium.com/@nagendra.raja/a-guide-to-keeping-your-java-dependencies-up-to-date-da2ad26b1f3f
author_url
https://medium.com/@nagendra.raja
status
ok
fetched_at
2026-06-16 19:09:56