How to use Maven to refactor project easily


How to use Maven to refactor project easily

Now is the era of micro service. It’s hard to say which day leaders will let you reconstruct a big project. Pain points of big projects: slow compilation, cumbersome release, etc. It’s like this picture:

How to use Maven to refactor project easily

I didn’t dare to move. It collapsed accidentally.

For example, our user system can be reconstructed in this way (here is just an example, the purpose of splitting each project may be different).

The user system is divided into:

  • user-web
  • user-service
  • user-dao
  • user-common

After we split it, it is very likely that multiple subprojects depend on a jar package at the same time. If it is not used properly, it may lead to different versions of each module. So, think about how to solve this kind of problem?

Answer: Yes. But first you need to understand two concepts: aggregation and inheritance.


The so-called aggregation is that if we want to build multiple project modules at one time, we need to aggregate these project modules.

Configuration template

    < module > project module 1
    < module > project module 2
    < module > project module 3
    < module > project module... < / module >


For example, we aggregate four project modules: user web, user service, user Dao, and user common.


Note: the path of module is relative.

This solves the aggregation of our project modules. Let’s see how Maven solves the problem of different versions due to repeated dependencies.


Inheritance in order to eliminate duplication, we extract many of the same configurations, such as grouptid, version, etc.

Inherited configuration


Defining properties in inheritance code

In the process of inheriting code, you can define attributes, such as:


The way to access properties is${ junit.version }, for example:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns=""
            <! -- using parent junit.version  -->

So we can use the properties defined above junit.version=4 .13.1。

The parent module is managed by dependency management

Because there are many project modules, we abstract another layer and extract a parent to manage the common dependencies of subprojects. In order to run the project correctly, it is necessary to make all subprojects use the unified version of dependencies. It is necessary to ensure that the dependencies and versions of each application project are consistent, so as to ensure that the test and release results are the same.


The advantage is that submodules can be selectively inherited without having to inherit all of them.

How to use Maven to refactor project easily

The advantage of doing this: unified management of the project version number, to ensure that the application of each project’s dependency and version are consistent, in order to ensure that the test and release of the same results, therefore, in the top-level POM to define a common dependency. At the same time, you can avoid declaring a version number in each used subproject. When you want to upgrade or switch to another version, you only need to update in the parent class container, and you do not need to modify any subproject. If a subproject needs another version number, you only need to declare a version number in dependencies. The subclass uses the version number declared by the subclass and does not inherit from the parent version number.

The relationship between aggregation and inheritance

In our previous multi module project, the relationship is as follows

Mavendemo is the parent project of user web, user service and other modules.

The parent project is mainly used to configure some common configurations. The other three projects own the configurations in the parent project by inheritance. First, configure the configuration of the parent project pom.xml , add theuser-webuser-serviceuser-daouser-commonThese four modules are used for aggregation and jar package dependency.

Compared with dependency management, all life dependencies in dependencies are automatically introduced and inherited by all subprojects by default.

Interview questions: the difference between dependency management and dependencies

  • Dependency management only declares dependencies, but does not implement import. Therefore, subprojects need to declare dependencies that need to be used. If the dependency is not declared in the subproject, it will not be inherited from the parent project; only if the dependency is written in the subproject and no specific version is specified, it will be inherited from the parent project, and the version and scope are read from the parent POM; in addition, if the version number is specified in the subproject, the jar version specified in the subproject will be used.
  • Dependencies even if the dependency is not written in the child project, the child project will inherit the dependency from the parent project (inherit all).

Configuring Maven in idea

When developing with idea, how to configure Maven?

Open idea and click file — > settings. Input search: maven

How to use Maven to refactor project easily

Configure the Maven directory we installed before, the setting.xml And local warehouse directory. Then apply — > OK.

How to use Maven to refactor project easily

When we clean a project, we can double-click the.

So we can integrate Maven into our idea.

Compile run project

We can compile and package all subprojects in the parent project. Let’s package all the sub modules.

Then you can find the target directory and the corresponding jar package in the corresponding subproject.

How to use Maven to refactor project easily

You can also package a subproject separately. For example, we clean user common.

How to use Maven to refactor project easily

Therefore, the target under user common has been deleted, and other subprojects have not been affected.

How to create multi project module in idea

First, create a user parent Maven project

Then delete the SRC directory and create a subproject

Select the user parent project, right-click and select new — > module

Go to the new module interface, select maven, and select your own locally installed JDK.

How to use Maven to refactor project easily

next。 Fill in the name of the subproject. Here we create a user web subproject:

How to use Maven to refactor project easily

Note that user web is the default. We need to manually add a horizontal line between the two words

How to use Maven to refactor project easily

Click Finish:

Then look at the POM of user parent

Modules have been added and our user web has been added.

In the same way, we can create more word projects:

How to use Maven to refactor project easily

Let’s look at user parent pom.xml Contents of the document:

How to use Maven to refactor project easily

Here, we have easily built our big project user parent, which includes user web, user service, user Dao and – user commmon subprojects.

If you create a new multi module project, you can install the above steps.

If the large project is split, we can use the above method to create our own multi module project first, and then copy the content of the large project to the corresponding module (it should be noted that the sub modules will depend on each other directly).


Through inheritance, we can define the parent project of the project, which is used to inherit the dependency, plug-in, attribute and other information of the parent project. Aggregation means that if we want to build multiple project modules at one time, we need to aggregate these project modules.

To build a multi module project, it’s easy to create a module in idea. Used in the parent project to manage the dependencies of child modules.

When refactoring a large project, you only need to copy the code separated from each module to the corresponding sub module.

“There is no innate confidence, only confidence that is constantly cultivated. 」

Recommended reading

To solve these 24 JVM interview questions, you have the strength to ask 30K~

Jingdong interview: talking about the architecture of MySQL

6000 words | second kill system design considerations