📜 ⬆️ ⬇️

Managing dependency versions in the Maven project

One of the advantages of organizing a Java project using Maven is a convenient description of all libraries and their versions that are required for it. Further, as the project develops, we probably want to be able to update versions of these libraries. This functionality provides an extension Versions Maven .

Versioning dependencies can be divided into three parts:
  1. parent project version (if available);
  2. dependency version;
  3. version of Maven extensions used.

The easiest task is to update the version of the parent project. It is enough to run the following command in the root of your project:

mvn versions:update-parent 

In order to be able to update versions of other dependencies, their versions must be explicitly described as variables in the properties section of the Maven project. Dependencies, versions of which are described without reference to a variable, cannot be updated in this way.

The same applies to the versions of all extensions you use. Thus, it is necessary to explicitly register in the project all the used extensions with the release of their versions into variables, after which their versions will also be updated.
')
As a result, all our dependencies and extensions will look like this:

 <?xml version="1.0" encoding="UTF-8"?> <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <properties> <spring.version>4.2.0.RELEASE</spring.version> <maven-clean.version>2.6.1</maven-clean.version> </properties> <dependencies> <!-- Spring Framework --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-clean-plugin</artifactId> <executions> <execution> <id>auto-clean</id> <phase>initialize</phase> <goals> <goal>clean</goal> </goals> </execution> </executions> </plugin> </plugins> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-clean-plugin</artifactId> <version>${maven-clean.version}</version> </plugin> </plugins> </pluginManagement> </build> </project> 

To figure out what other extensions we have not explicitly indicated, we need to use the command :

 mvn versions:display-plugin-updates 

the results of which you can find out what other extensions are not registered in your project and the versions of which you do not manage. It will also indicate what minimum version of Maven you need to specify in your project for all extensions to work correctly.

It is important to note that the dependencies that are included in your project through the parent project will not update the versions until you explicitly include them in the list of dependencies of your project, transferring from the parent. This can be useful if in your project you want to have a different set of versions than the ready parent project provides.

After performing all the operations described above, we can directly update all the versions described by us as variables by running the command :

 mvn versions:update-properties 

Then you can view a list of the changes made by the extension to the Maven project and decide on their appropriateness.

Looking through the list of proposed new versions, you will very quickly find that not the final versions of dependencies begin to appear there, which you most likely do not need. To describe which versions you would like to skip during the update, a special file is used where you can describe exceptions as specific versions or regex expressions for specific dependencies or their groups.

Create a file maven-version-rules.xml in the project root with the following contents:
 <?xml version="1.0" encoding="UTF-8"?> <ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" comparisonMethod="maven" xmlns="http://mojo.codehaus.org/versions-maven-plugin/rule/2.0.0" xsi:schemaLocation="http://mojo.codehaus.org/versions-maven-plugin/rule/2.0.0 http://www.mojohaus.org/versions-maven-plugin/xsd/rule-2.0.0.xsd"> <rules> <rule groupId="org.hibernate"> <ignoreVersions> <ignoreVersion type="regex">.*Alpha.*</ignoreVersion> <ignoreVersion type="regex">.*Beta.*</ignoreVersion> <ignoreVersion type="regex">.*[.]CR.*</ignoreVersion> </ignoreVersions> </rule> </rules> </ruleset> 

And connect it:
 <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>versions-maven-plugin</artifactId> <configuration> <generateBackupPoms>false</generateBackupPoms> <rulesUri>file://${project.basedir}/maven-version-rules.xml</rulesUri> </configuration> </plugin> 

As a result, you may want to create such update.bat file (alternatively, update.sh ) to update versions in the future:

 call mvn versions:update-parent versions:update-properties 

or only:

 call mvn versions:update-properties 

It should be noted that projects such as the Spring IO platform and Spring Boot in their parent projects already provide current and compatibility tested versions of a very large number of libraries, which allows them to be used in their projects without specifying a specific version.

All sources are available on GitHub .

Source: https://habr.com/ru/post/264505/


All Articles