Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of maven dependency of IDEA Multi-module Project

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail the example analysis of IDEA multi-module project maven dependency. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

Whether eclipse was abandoned by the times or not, it was abandoned by me, because IDEA is really easy to use.

Now the company's projects are basically based on maven multi-module projects, controller,service,model,dao and so on have been divided into different module, of course, to decouple.

These module can be configured in pom.xml as needed to form war packages or jar packages.

Jar

Web main project sets the packaging level to war,dao, model these module sets the packaging level to jar.

Module can refer to or depend on each other through module's own pom.xml, such as:

Cn.com.autohome.mall mall-common cn.com.autohome.mall mall-api-model

So under File-> project structure, select the main web project

From the screenshot above, you can see the difference between the dependent third-party jar and the dependent project child module.

When maven executes install,packaging as jar, it will be typed as jar and placed in the target directory. If packaging is war, it will be typed as war and placed in the target directory.

The other two target directories will be a little different. War's target directory will have an extra folder with the same name as module, which is exactly the same as war when unzipped.

All dependent jar (including dependent module, which is called jar) will be placed under lib

In this way, when you deploy, you only need to deploy the corresponding war.

About the elements modules, parent, properties and import in Maven pom.xml

Multiple module do not need to execute mvn commands separately. You can use aggregator to build all modules at once.

Modules

Passed in the parent pom.xml

Mall-common mall-api-model

To reference all the submodules that need to be built

Parent

Inheritance, which is equivalent to inheritance in java, is used to reuse

Scene

If each sub-module uses spring, do we need to configure spring dependencies separately for each sub-module? It's OK to do this, but we have a better approach, which is inheritance, which is implemented in parent.

Realize

Parent (account-aggregator) pom.xml

Mall-common mall-api-model org.springframework spring-core 4.0.2.RELEASE junit junit 4.7 test

Child pom.xml

Xx.xx.xx aggregator 1.0.0-SNAPSHOT.. / pom.xml javax.mail mail 1.4.3 com.icegreen greenmail 1.4.1 test

Dependency management

Is there no problem that inheritance can eliminate duplication? The answer is that there is a problem. Suppose you need to add a new sub-module util in the future, which only provides some simple help tools and does not need to rely on spring or junit. Is there any way to rely on it after inheritance?

Yes, maven has thought of it for us, and that is the dependencyManagement element, which not only allows the child module to inherit the dependency configuration of the parent module, but also ensures the flexibility of the child module dependency. Dependency declaration under the dependencyManagement element does not introduce actual dependencies, but it can constrain dependency use under dependencies.

Configure the dependencyManagement element in the parent pom.xml

Mall-common mall-api-model org.springframework spring-core 4.0.2.RELEASE

Child pom.xml

Org.springframework spring-core org.springframework spring-beans org.springframework spring-context org.springframework spring-context-support junit junit org.springframework spring-jdbc 4.0.2.RELEASE com.alibaba druid 1.0.16

Using this dependency management mechanism does not seem to reduce too much POM configuration, so there is less version (junit is also missing a scope), which feels useless. In fact, it is quite useful. The parent POM uses dependencyManagement to unify the dependent version in the project scope. When the dependent version is declared in the parent POM, the child module does not need to declare the version when using the dependency, and there is no need for multiple child modules to use version inconsistencies, helping to reduce the probability of dependency conflicts. If the child module does not declare the use of the dependency, even if the dependency is declared in the dependencyManagement in the parent POM, it will not have any effect.

This is the end of this article on "sample analysis of IDEA multi-module project maven dependency". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report