Manage projects with Apache Maven

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.

Manage projects with Apache Maven

My result is as follows:

Manage projects with Apache Maven

The contents of the pom.xml file are as simple as:

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:

Manage projects with Apache Maven

Normally, we will leave this project within the parent project of the application.

Manage projects with Apache Maven

Now, I will declare the parent project for this project in the pom.xml file as follows:

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:

In the parent project, we will declare this project as a module of the application as follows:

Now, assuming your application needs to use MySQL dependency, you will declare this dependency in the parent project as follows:

then in the maven-example project, I declare the MySQL dependency as follows:

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.

Chia sẽ bài viết này ...Share on Facebook
Facebook
0Tweet about this on Twitter
Twitter
Share on LinkedIn
Linkedin

Add Comment