When you develop an application with different Maven projects, we will often use a Maven parent project to manage these Maven projects. It will help us to centralize the version management of the dependencies used for the application, when we want to upgrade the version of a dependency, we just need to change it in the pom.xml file of the Maven parent project. One need is how we can manage the version of these Maven sub-projects so that when we need to upgrade the application’s version, we will only need to change one place.
For example, I have a Maven parent project with the content of pom.xml file as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.huongdanjava</groupId> <artifactId>maven-parent</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <modules> <module>maven-example1</module> <module>maven-example2</module> </modules> <dependencyManagement> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.18</version> </dependency> </dependencies> </dependencyManagement> </project> |
with maven-example1 sub-project has pom.xml file with the following content:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.huongdanjava</groupId> <artifactId>maven-parent</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath>../pom.xml</relativePath> </parent> <artifactId>maven-example1</artifactId> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependencies> </project> |
and maven-example2 sub-project has pom.xml file with the following content:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.huongdanjava</groupId> <artifactId>maven-parent</artifactId> <version>${global.version}</version> <relativePath>../pom.xml</relativePath> </parent> <artifactId>maven-example2</artifactId> </project> |
Now my need is that when I want to upgrade the version of the application, I just need to change it in the pom.xml file of the Maven parent project, which can be effective on sub-projects, no need to change for each pom.xml of sub-project! 🙂
What is your solution? I suggest the following solution:
Add a property in the pom.xml file of the Maven parent project, for example:
1 2 3 |
<properties> <global.version>1.0-SNAPSHOT</global.version> </properties> |
Then use this property to declare the parent project:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.huongdanjava</groupId> <artifactId>maven-parent</artifactId> <version>${global.version}</version> <packaging>pom</packaging> <properties> <global.version>1.0-SNAPSHOT</global.version> </properties> <modules> <module>maven-example1</module> <module>maven-example2</module> </modules> <dependencyManagement> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.18</version> </dependency> </dependencies> </dependencyManagement> </project> |
and all parent project declarations in sub-projects are as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.huongdanjava</groupId> <artifactId>maven-parent</artifactId> <version>${global.version}</version> <relativePath>../pom.xml</relativePath> </parent> <artifactId>maven-example1</artifactId> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependencies> </project> |
and:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.huongdanjava</groupId> <artifactId>maven-parent</artifactId> <version>${global.version}</version> <relativePath>../pom.xml</relativePath> </parent> <artifactId>maven-example2</artifactId> </project> |
Running build parent project, you will see the following results:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
... [INFO] ------------------------------------------------------------------------ [INFO] Reactor Build Order: [INFO] [INFO] maven-parent [pom] [INFO] maven-example1 [jar] [INFO] maven-example2 [jar] [INFO] [INFO] -------------------< com.huongdanjava:maven-parent >-------------------- [INFO] Building maven-parent 1.0-SNAPSHOT [1/3] [INFO] --------------------------------[ pom ]--------------------------------- ... [INFO] ------------------< com.huongdanjava:maven-example1 >------------------- [INFO] Building maven-example1 1.0-SNAPSHOT [2/3] [INFO] --------------------------------[ jar ]--------------------------------- ... [INFO] ------------------< com.huongdanjava:maven-example2 >------------------- [INFO] Building maven-example2 1.0-SNAPSHOT [3/3] [INFO] --------------------------------[ jar ]--------------------------------- ... [INFO] Reactor Summary for maven-parent 1.0-SNAPSHOT: [INFO] [INFO] maven-parent ....................................... SUCCESS [ 0.366 s] [INFO] maven-example1 ..................................... SUCCESS [ 0.851 s] [INFO] maven-example2 ..................................... SUCCESS [ 0.056 s] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1.396 s [INFO] Finished at: 2020-01-18T09:14:24+07:00 [INFO] ------------------------------------------------------------------------ |
Change the value of property ${global.version} to 1.0 and rebuild, you will see the following results:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
... [INFO] ------------------------------------------------------------------------ [INFO] Reactor Build Order: [INFO] [INFO] maven-parent [pom] [INFO] maven-example1 [jar] [INFO] maven-example2 [jar] [INFO] [INFO] -------------------< com.huongdanjava:maven-parent >-------------------- [INFO] Building maven-parent 1.0 [1/3] [INFO] --------------------------------[ pom ]--------------------------------- ... [INFO] ------------------< com.huongdanjava:maven-example1 >------------------- [INFO] Building maven-example1 1.0 [2/3] [INFO] --------------------------------[ jar ]--------------------------------- ... [INFO] ------------------< com.huongdanjava:maven-example2 >------------------- [INFO] Building maven-example2 1.0 [3/3] [INFO] --------------------------------[ jar ]--------------------------------- ... [INFO] ------------------------------------------------------------------------ [INFO] Reactor Summary for maven-parent 1.0: [INFO] [INFO] maven-parent ....................................... SUCCESS [ 0.520 s] [INFO] maven-example1 ..................................... SUCCESS [ 0.961 s] [INFO] maven-example2 ..................................... SUCCESS [ 0.055 s] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1.713 s [INFO] Finished at: 2020-01-18T09:25:37+07:00 [INFO] ------------------------------------------------------------------------ |
Very convenient, right?