Maven继承和聚合示例体系结构 [英] Maven Inheritance and Aggregation Example Architecture

查看:166
本文介绍了Maven继承和聚合示例体系结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,关于如何使用继承和聚合的组合来重新构建一些单独的Maven项目。

I have a question regarding how best to re-structure a number of individual Maven projects using a combination of inheritance and aggregation.

设置场景:


  • 现有3个基于代码的Maven项目均由同一团队开发。

  • 1项目是一个API ,让我们调用project-api。

  • 其他2个项目是使用project-api的web应用程序。让我们称之为web-app1和web-app2。

这三个项目都有一些基本的依赖关系,比如log4j和junit。除此之外,web-app1和web-app2依赖于project-api,并且还在它们之间共享许多其他常见的依赖关系。

All three projects have a couple of basic dependencies like log4j and junit in common. Aside from that, web-app1 and web-app2 depend on project-api and also share a number of additional common dependencies between them.

我一直在阅读 http://www.sonatype.com/ books / mvnref-book / reference / pom-relationships-sect-pom-best-practice.html ,我只是想确认我理解最佳做法。

I have been reading http://www.sonatype.com/books/mvnref-book/reference/pom-relationships-sect-pom-best-practice.html, I'm just wanting to confirm that I understand best practice.

继承:

我是否有必要创建一个组织级父POM,其中包含所有三个项目共有的依赖关系(依赖关系管理)一些环境设置和总体项目信息。意图是所有Maven项目(不一定是直接)从这个POM继承。

Would it make sense for me to create an organisational level parent POM that includes the dependencies (dependency management) that are common to all three projects and some environment settings and overarching project information. The intent being all Maven projects (not necessarily directly) inherit from this POM.

我是否有意义创建另一个包含依赖项的父POM(依赖关系管理)这是web-app1和web-app2常见的,只有web-app1和web-app2继承自此POM。我认为这个POM(让我们称之为web-app父POM)应该是组织级父POM的子POM。

Would it make sense for me to create another parent POM that includes the dependencies (dependency management) that are common to web-app1 and web-app2 and have only web-app1 and web-app2 inherit from this POM. I think this POM (lets call it web-app parent POM) should be a child POM of the organisational level parent POM.

聚合:

在聚合方面,我不想在一天结束时创建一个工件。相反,我想运行一个Maven构建命令(可能在组织POM级别)按此顺序构建三个项目:

In terms of aggregation, I do not want to create a single artifact at the end of the day. Rather, I want to run one Maven build command (likely at the organisational POM level) to build the three projects in this order:


  1. 项目-api

  2. web-app1

  3. web-app2

这是否意味着组织父POM会声明模块:

Does this mean the organisational parent POM would declare modules:


  1. project-api

  2. web-app父POM

网络应用程序父POM将声明模块:

And the web-app parent POM would declare modules:


  1. web-app1

  2. web-app2

请注意,Web-app父POM和组织父POM没有任何相关代码。我相信这是可以注意到的事实上,在Maven世界中,一个项目根本不需要包含任何代码,只需要一个pom.xml。摘自 http://maven.apache.org/pom.html

Note the web-app parent POM and organisational parent POM do not have any related code. I believe this is OK noting "In fact, in the Maven world, a project need not contain any code at all, merely a pom.xml." taken from http://maven.apache.org/pom.html.

最后,我如何确保我需要的构建顺序得到尊重?例如。构建组织父POM会导致构建project-api并且这个最新版本用于构建web-app1和web-app2?

Finally, how do I ensure the build order I need is respected? E.g. Building the organisational parent POM would result in project-api being built and this latest build being used in the building of web-app1 and web-app2?

我希望这不是太混乱了,很高兴澄清是否需要更多信息。如果我完全错了,请随时告诉我!谢谢。

I hope this isn't too confusing, happy to clarify if more info is required. Feel free to tell me if I have this completely wrong! Thanks.

推荐答案

你的方法是明智的。几点:

Your approach is sensible. A couple of points:

是环境设置和总体项目信息,没有依赖项。

Yes to environment settings and overarching project information, no to dependencies.

项目应明确列出所有依赖项,而不是依赖继承它们(恕我直言)。这意味着您必须多次声明您的记录器,但以后会为您节省时间。 (当然,您可以使用单独的POM项目将相关的依赖项组合在一起,因此通常一起指定,例如链接中的hibernate示例)。如果要集中依赖项的版本,可以在父POM中放置 dependencyManagement 部分,这意味着您仍然在子项中声明依赖项项目,但版本来自父项,从而确保一致性。没有声明依赖关系的孩子根本不会最终使用它。

Projects should explicitly list all their dependencies and not rely on inheriting them (IMHO). It means you have to declare your logger a few times but it will save you pain later. (You can of course use a separate POM project to group together dependencies that are related and hence usually specified together, like the hibernate example in your link). If you want to centralize versions of dependencies, you can put a dependencyManagement section in the parent POM, which means you still declare the dependency in the child project, but the version comes from the parent, thus ensuring consistency. Children that don't declare the dependency don't end up with it at all.

如果他们有重复的插件,配置等,有一个webapp-parent是个好主意。如果他们共享代码,你可以添加另一个项目webapp-common,它是一个内置的jar,其他两个可以依赖。它的依赖关系将被传递,因此这是共同依赖的自然场所。

Having a webapp-parent is a good idea if they have duplicated plugins, configuration etc. If they share code, you could add another project webapp-common that's a built as a jar which the other two can depend on. Its dependencies will be transitively included, so that's a natural place for common dependencies to go.

webapp -parent不需要同时是父级和聚合器,除非您需要经常同时构建webapp1和webapp2而不是project-api。您可以将所有项目作为整体父项的模块。您的目录结构可能如下所示

webapp-parent doesn't need to be both parent and aggregator, unless you need to frequently build webapp1 and webapp2 but not project-api at the same time. You can just put all the projects as modules of the overall parent. Your directory structure could look like

overall
  project-api
  webapp-parent
  webapp1
  webapp2

或者如果你更喜欢你的原始建议,这也没关系

or if you prefer your original suggestion, which is also fine

overall
  project-api
  webapp-parent
    webapp1
    webapp2

随着时间的推移,关注项目布局更重要,并在适当的时候进行重构。

It's more important to keep an eye out on the project layout over time, and refactor when appropriate.

Maven足够聪明,可以按正确的顺序构建模块,只要您声明依赖项。

Maven is clever enough to build the modules in the correct order, as long as you declare the dependencies.

这篇关于Maven继承和聚合示例体系结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆