简体   繁体   English

为什么要在包上使用许多子项目和依赖项?

[英]Why use many sub-projects and dependencies over packages?

I've mostly in my career worked in small or mid-sized java projects. 我的职业生涯主要是在中小型Java项目中工作。 I recently saw a huge project comprising of 30 projects in eclipse. 我最近在eclipse中看到了一个包含30个项目的庞大项目。 I don't really get concept of creating many small projects and then maintain inter-project dependencies. 我真的没有创建许多小项目的概念,然后维护项目间的依赖关系。 When do we prefer this over simply organizing stuff in packages? 我们什么时候更喜欢这个而不是简单地组织包裹?

I guessed it's a maven thing (have mostly been using Ant). 我猜这是一个maven的事情(主要是使用Ant)。 I've been reading up on Maven's concept of modules as well – I saw some links on net recommending creation of different modules for web, dao and service layers under a parent module. 我一直在阅读Maven的模块概念 - 我在网上看到一些链接,建议在父模块下为web,dao和服务层创建不同的模块。 Is it really a common/best practice? 这真的是一种常见/最佳实践吗?

With or without maven – does such division really makes life easier? 有或没有maven - 这种分裂真的让生活更轻松吗? Isn't it more compact to have everything in a single project with well-defined packages structure for different layers? 将所有内容都放在一个具有明确定义的不同层包结构的项目中是不是更紧凑?

It's common to split up projects into API, implementation, web, etc. components–when there's a need to do so. 在需要时,将项目分成API,实现,Web等组件是很常见的。 Large projects are just that: large. 大型项目就是:大型项目。

There are benefits to keeping components separate" 将组件分开是有好处的“

  • Re-use functionality (eg, the web layer uses the service layer 重用功能(例如,Web层使用服务层
  • Package individual components (eg, ship jus the API to a client) 打包单个组件(例如,将API发送给客户端)
  • Version sub-components; 版本子组件; define their version dependencies 定义他们的版本依赖

You can do all the same stuff with one giant project, but it's more difficult to determine what goes where, and why. 你可以用一个巨大的项目完成所有相同的工作,但是确定哪些地方以及原因更加困难。 Life is easier when those lines of demarcation are clearly defined. 当明确界定这些划界线时,生活会更容易。

How much easier depends on the project, but when you're dealing with hundreds of thousands of lines of code, occasionally millions, breaking that stuff up saves huge headaches. 容易取决于项目,但是当你处理数十万行代码时,偶尔会有数百万行代码打破这些代码可以避免巨大的麻烦。

Why choose to create a separate module in maven? 为什么选择在maven中创建一个单独的模块? To help you with your development. 为了帮助您的发展。 There really is no other reason. 真的没有其他原因。

The are a number of different reasons why you may want to create a separate module: 您可能想要创建单独的模块的原因有很多种:

  1. Separation of concerns: yes, you can do this with packages, but if it's in a separate module, then it can be compiled separately, and you can reduce the amount of tangle[*] in your packages. 关注点分离:是的,您可以使用包来执行此操作,但如果它位于单独的模块中,则可以单独编译,并且可以减少包中的纠缠量[*]。
  2. The modules are managed by different teams, with release cycles of their own. 这些模块由不同的团队管理,具有自己的发布周期。
  3. More understandable code: If all of your dao code is in one module, and all of your web in another, you can test them separately. 更易理解的代码:如果您的所有dao代码都在一个模块中,而您的所有Web代码都在另一个模块中,则可以单独测试它们。
  4. A module can be a separate deployable entity. 模块可以是单独的可部署实体。 I have a project which has two web apps, 5 batches and two other core modules (one core for the webapp and one core for the batches). 我有一个项目有两个网络应用程序,5个批次和两个其他核心模块(一个核心用于webapp,一个核心用于批量)。 I can now build and deploy each module separately. 我现在可以单独构建和部署每个模块。
  5. The modules are published and used externally. 这些模块在外部发布和使用。 If this is true, then you want the minimum amount of 'other' code in this module. 如果这是真的,那么您需要此模块中最少量的“其他”代码。

You choose to break up into modules for the same reasons as you would for separating into packages, but at a higher level, at a group of packages. 您选择分解为模块的原因与分离到包中的原因相同,但是在更高级别,在一组包中。

30 does seem excessive. 30看起来似乎过分了。 But there may be good reasons for it. 但可能有充分的理由。 It's up to you and your project to decide what is the right level for the number of modules. 由您和您的项目决定什么是模块数量的正确级别。

Personally, I try not to split excessively, unless there is a very good reason to do so. 就个人而言,我尽量不要过分分裂,除非有充分的理由这样做。

[*] Tangle: mess which describes the links between packages. [*] Tangle:mess描述了包之间的链接。 Package A uses B, which uses C, which uses both A and B. Something which doesn't help understanding. 包A使用B,它使用C,它使用A和B.这些东西无助于理解。

I think excessive modularisation is akin to over engineering. 我认为过度模块化类似于过度工程。 The best route in my opinion is to start with one module/project and keep with that until such point as it becomes obvious to everyone involved that part of this existing module would benefit from being extracted into its own module. 在我看来,最好的路线是从一个模块/项目开始并保持这一点,直到所有参与者都明白这个现有模块的一部分将从提取到自己的模块中受益。 Yes, that means extra work at that point, but for me I'd rather do the work then than endlessly battle with unneeded complexity, in terms of my builds and my development environment, for benefits which are never actually realised. 是的,这意味着在那一点上需要额外的工作,但是对于我来说,我宁愿做完工作,而不是在我的构建和我的开发环境方面与不必要的复杂性无休止地争夺从未实际实现的好处。

Unfortunately there seems to be a tendency at the start of a projects to modularise to the nth degree before even a single line of code is written. 不幸的是,在编写一行代码之前,项目开始时似乎趋向于模块化到n度。

这还有另外一个优点..如果我必须在“选定”组件上部署,我不会浪费时间和资源来部署我不需要的依赖项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 播放2.2.1子项目与子包? - Play 2.2.1 sub-projects vs sub-packages? 将春季子项目包装到一个罐中 - Packaging spring sub-projects into one jar 我想创建一个利用并创建较小子项目(库)的Java项目,Apache Ant是适合此目的的合适软件吗? - I want to create a Java project that utilizes AND creates smaller sub-projects (libraries), is Apache Ant the proper software to use for this? 在一台Play服务器上运行独立的子项目不起作用 - Running independent sub-projects on one Play server doesn't work Gradle多项目构建命令失败,但对子项目可正常使用 - Gradle multi-project build command fails but works correctly for sub-projects 如何使用IntelliJ,maven和spring-data-rest处理多个子项目? - How to work with multiple sub-projects using IntelliJ, maven and spring-data-rest? 如何在Maven中处理子项目依赖项 - How to handle sub projects dependencies in Maven 在子项目中使用重新定位的依赖项 - Use relocated dependencies in child projects 所有项目的Maven依赖关系直方图(使用频率) - Histrogram of maven dependencies over all projects (What is used how often) 为什么不让gradle返回其他项目的依赖关系? - Why doesn't gradle return dependencies for another projects?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM