Imagine you are building an application with many different projects, for each project you need to declare to use some dependencies with the fixed versions. Some projects need to use the same dependencies and of course the same version. How do we feel when we need to upgrade a version of a dependency, we have to open each project using those dependencies to fix or you want to build all the projects, you have to open command-line for each project and execute “mvn clean install”. To minimize all that, and more importantly, to let us know which projects are relevant to the application, you can:
- Create a Maven project with the pom package,
- Declare all required dependencies for the application in this project,
- Declares all project of the application in this project.
- Then in each project of the application, you declare this project is the parent project,
- Then declare the dependencies to use for that project without the version.
Many stages, don’t you? How is it in details? We will learn together in this tutorial.
First, I will create a Maven parent project with the pom packaging.
I have shown you how to create a new Maven project in Eclipse, then you follow the instructions of this tutorial but there is a note that is in New Maven project window, you choose the type of packaging is pom.
My result is as follows:
The contents of the pom.xml file are as simple as:
1 2 3 4 5 6 7 8 |
<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> </project> |
As you can see, the packing tag is pom.
Next, we will create a Maven project as an example and then declare the parent project pointing to the project we just created.
You can create a Maven project as normal, my project as follows:
Normally, we will leave this project within the parent project of the application.
Now, I will declare the parent project for this project in the pom.xml file as follows:
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> <groupId>com.huongdanjava</groupId> <artifactId>maven-example</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>com.huongdanjava</groupId> <artifactId>maven-parent</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath>../pom.xml</relativePath> </parent> </project> |
Declare <relativePath> in declaring parent project on purpose if our parent project is not in the local or remote repository then our project will automatically get parent project from the relative path with the value declared in this <relativePath> tag.
Here, you can also remove the project’s <groupId> and <version> declaration to use the <groupId> and <version> of the parent project as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<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> <artifactId>maven-example</artifactId> <parent> <groupId>com.huongdanjava</groupId> <artifactId>maven-parent</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath>../pom.xml</relativePath> </parent> </project> |
In the parent project, we will declare this project as a module of the application as follows:
1 2 3 4 5 6 7 8 9 10 11 12 |
<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-example</module> </modules> </project> |
Now, assuming your application needs to use MySQL dependency, you will declare this dependency in the parent project as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<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-example</module> </modules> <dependencyManagement> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.11</version> </dependency> </dependencies> </dependencyManagement> </project> |
then in the maven-example project, I declare the MySQL dependency as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<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> <artifactId>maven-example</artifactId> <parent> <groupId>com.huongdanjava</groupId> <artifactId>maven-parent</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath>../pom.xml</relativePath> </parent> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependencies> </project> |
As you can see, in the maven-example project, I have declared using MySQL dependency but without having to declare its version anymore. Later, if you want to upgrade the version of MySQL dependency you just need to change in the parent project, the project using the parent project will automatically change accordingly.