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

发布于 2024-12-06 01:40:01 字数 301 浏览 2 评论 0原文

在我的职业生涯中,我主要从事中小型 Java 项目。我最近在eclipse中看到一个由30个项目组成的庞大项目。我真的不明白创建许多小项目然后维护项目间依赖关系的概念。我们什么时候更喜欢这样做而不是简单地将东西整理成包?

我猜这是一个专家的事情(主要是使用 Ant)。我也一直在阅读 Maven 的模块概念——我在网上看到了一些链接,建议在父模块下为 Web、dao 和服务层创建不同的模块。这真的是常见/最佳实践吗?

无论有没有 Maven,这样的划分真的会让生活变得更轻松吗?将所有内容都放在一个项目中,并为不同层提供明确定义的包结构,这不是更紧凑吗?

I’ve mostly in my career worked in small or mid-sized java projects. I recently saw a huge project comprising of 30 projects in eclipse. 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). 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. Is it really a common/best practice?

With or without maven – does such division really makes life easier? Isn’t it more compact to have everything in a single project with well-defined packages structure for different layers?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

烂柯人 2024-12-13 01:40:01

当需要时,将项目拆分为 API、实现、Web 等组件是很常见的。大项目就是这样:大。

保持组件独立有好处”

  • 重用功能(例如,Web 层使用服务层
  • 打包各个组件(例如,仅将 API 发送给客户端)
  • 版本子组件;定义它们的版本版本依赖性

做所有相同的事情,但是当这些分界线被明确定义时,确定什么会发生在哪里以及为什么会变得更容易。

您可以在一个巨大的项目中 在项目上,但是当你处理数十万行代码,有时甚至数百万行,将这些内容分解可以节省巨大的麻烦。

It's common to split up projects into API, implementation, web, etc. components–when there's a need to do so. Large projects are just that: large.

There are benefits to keeping components separate"

  • Re-use functionality (e.g., the web layer uses the service layer
  • Package individual components (e.g., ship jus the API to a client)
  • 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.

因为看清所以看轻 2024-12-13 01:40:01

为什么选择在maven中创建单独的模块?为您的发展提供帮助。确实没有其他原因。

您可能想要创建单独的模块的原因有很多:

  1. 关注点分离:是的,您可以使用包来执行此操作,但如果它位于单独的模块中,则可以单独编译它,并且可以减少您的包裹中缠结的数量[*]。
  2. 这些模块由不同的团队管理,有自己的发布周期。
  3. 更易于理解的代码:如果所有 dao 代码都在一个模块中,而所有 Web 代码都在另一个模块中,则可以单独测试它们。
  4. 模块可以是单独的可部署实体。我有一个项目,有两个 Web 应用程序、5 个批次和两个其他核心模块(一个用于 Web 应用程序的核心,一个用于批次的核心)。我现在可以单独构建和部署每个模块。
  5. 这些模块在外部发布和使用。如果这是真的,那么您希望此模块中的“其他”代码数量最少。

您选择分解为模块的原因与分解为包的原因相同,但在更高的级别上,是在一组包中。

30确实显得太多了。但这可能有充分的理由。由您和您的项目决定模块数量的正确级别。

就我个人而言,我尽量不要过度分裂,除非有充分的理由这样做。

[*] Tangle:混乱,描述了包之间的链接。包A使用B,B使用C,C同时使用A和B。这对理解没有帮助。

Why choose to create a separate module in 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.
  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). 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. 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. Package A uses B, which uses C, which uses both A and B. Something which doesn't help understanding.

感性不性感 2024-12-13 01:40:01

我认为过度的模块化类似于过度工程。我认为最好的路线是从一个模块/项目开始,并一直坚持下去,直到每个参与其中的人都清楚地意识到,现有模块的一部分将受益于提取到自己的模块中。是的,这意味着那时需要额外的工作,但对我来说,我宁愿先做这些工作,也不愿在我的构建和开发环境方面无休止地与不必要的复杂性作斗争,以获得从未真正实现的好处。

不幸的是,在项目开始时,甚至在编写一行代码之前,似乎存在一种模块化程度的趋势。

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.

菊凝晚露 2024-12-13 01:40:01

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

there is another advantage to this.. if i have to deploy on 'selected' components i dont waste time and resources deploying dependencies that I dont need.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文