限制Java包之间的依赖关系

发布于 2024-07-05 15:18:23 字数 307 浏览 10 评论 0原文

在 Java 构建系统中对包依赖项实施限制的可能性有哪些? 例如,不应允许 myapp.server.bl.Customer 类引用 myapp.client.ui.customlayout 包。 我对基于 Ant 或特定于 IDE 的解决方案感兴趣。

我想在构建中收到一条错误消息,指示违反了(自定义)包依赖关系规则并且构建中止。 我还想在 Ant 脚本或 IDE 项目文件之外的列表中维护依赖项,最好是在文本文件中。

(我不知道 Maven,但我在这里读过它,它对模块依赖管理有更好的支持)

What are the possibilities to enforce restrictions on the package dependencies in a Java build system? For example, the myapp.server.bl.Customer class should not be allowed to refer to the myapp.client.ui.customlayout package.
I'm interested in either Ant-based or IDE-specific solutions.

I'd like to get an error message in the build indicating that a (custom) package dependency rule has been violated and the build aborted. I also would like to maintain the dependencies in a list, preferably in a text file, outside of the Ant scripts or IDE project files.

(I don't know Maven but I've read it here it has better support for module dependency management)

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

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

发布评论

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

评论(7

千笙结 2024-07-12 15:18:23

我相信 Checkstyle 对此有一个检查。
它称为导入控件

I believe Checkstyle has a check for that.
It's called Import Control

我的鱼塘能养鲲 2024-07-12 15:18:23

您可以配置 Eclipse 项目来指定访问规则。 访问规则可以指定“禁止”、“不鼓励”和“可访问”级别,所有级别均使用通配符规则。 然后,您可以将不鼓励或禁止的违规配置为在构建期间标记为警告或错误。

关于这个想法的旧文章(详细信息可能已过时):

http:// www.eclipsezone.com/eclipse/forums/t53736.html

如果您使用 Eclipse(或 OSGi)插件,那么插件/模块的“公共”部分是明确定义的,这是模型的一部分。

You can configure Eclipse projects to specify Access Rules. Access rules can specify "Forbidden", "Discouraged", and "Accessible" levels all with wildcard rules. You can then configure violations of either Discouraged or Forbidden to be flagged as either warnings or errors during builds.

Kind of an old article on the idea (details may be out of date):

http://www.eclipsezone.com/eclipse/forums/t53736.html

If you're using Eclipse (or OSGi) plugins, then the "public" parts of the plugin/module are explicitly defined and this is part of the model.

魔法少女 2024-07-12 15:18:23

ivy 似乎是解决您问题的一个很好的解决方案(如果您使用的是 ant)。 Ivy 是 Ant 的官方依赖管理组件,因此与 ant 集成得很好。 它能够解决依赖关系、处理冲突、创建排除等。

它使用简单的 xml 结构来描述依赖关系,并且比 Maven 更易于使用,因为它只尝试解决依赖关系解析问题。

来自ivy主页:

ivy是一个用于管理(记录、跟踪、解决和报告)项目依赖关系的工具。 它具有以下特点:

  1. 灵活性和可配置性 - Ivy 本质上与流程无关,并且不依赖于任何方法或结构。 相反,它提供了必要的灵活性和可配置性,以适应广泛的依赖关系管理和构建过程。
  2. 与 Apache Ant 紧密集成 - 虽然可以作为独立工具使用,但 Ivy 与 Apache Ant 配合得特别好,提供了许多强大的 Ant 任务,从依赖关系解析到依赖关系报告和发布。

ivy seems like a good solution for your problem (if you are using ant). Ivy is the offical dependency management component of Ant and thus integrates nicely with ant. It is capable of resolving dependencies, handle conflicts, create exclusions and so on.

It uses a simple xml structure to describe the dependencies and is easier to use than Maven, because it only tries to address dependency resolution problems.

From the Ivy homepage:

Ivy is a tool for managing (recording, tracking, resolving and reporting) project dependencies. It is characterized by the following:

  1. flexibility and configurability - Ivy is essentially process agnostic and is not tied to any methodology or structure. Instead it provides the necessary flexibility and configurability to be adapted to a broad range of dependency management and build processes.
  2. tight integration with Apache Ant - while available as a standalone tool, Ivy works particularly well with Apache Ant providing a number of powerful Ant tasks ranging from dependency resolution to dependency reporting and publication.
嘴硬脾气大 2024-07-12 15:18:23

Eclipse 通过构建路径属性/jar 属性对此提供支持。 我认为它可能只能跨 jar/项目边界工作。

Eclipse has support for this via Build Path properties / jar properties. I think it may only work across jar / project boundaries.

偏爱自由 2024-07-12 15:18:23

对于 IDE 特定的解决方案,IntelliJ IDEA 有一个依赖性分析工具,它也允许定义无效的依赖性。
http://www.jetbrains.com/idea/webhelp2/dependency- validation-dialog.html

在编译和实时编辑依赖类时,都会显示依赖项冲突(作为右侧错误栏中的错误/警告条纹)。

使用 JetBrains 的 TeamCity 构建服务器可以获得更多自动化,该服务器可以运行检查构建并报告上述配置的检查。

对于另一个独立于 IDE 的解决方案,AspectJ 可用于声明无效的依赖项(并将该步骤集成到构建过程中,以便获取问题的警告/错误信息)。

For the IDE specific solutions, IntelliJ IDEA has a dependency analysis tool that allows one to define invalid dependencies as well.
http://www.jetbrains.com/idea/webhelp2/dependency-validation-dialog.html

The dependency violation will be shown both when compiling and live, while editing the dependent class (as error/warning stripes in the right side error bar).

Even more automation can be obtained with JetBrains' TeamCity build server, that can run inspection builds and report the above configured checks.

For another IDE independent solution, AspectJ can be used to declare invalid dependencies (and integrate the step in the build process, in order to obtain warning/error info for the issues).

国际总奸 2024-07-12 15:18:23

您可以在 IDEA 或 Maven 中使用多个模块,或者在 Eclipse 和 Gradle 中使用多个项目。 这个概念在所有情况下都是相同的。

一个简单的解释是 myapp.server.bl 的一个模块和 myapp.client.ui.customlayout 的另一个模块,它们之间没有编译时依赖性。 现在,任何针对相反模块/项目编译代码或代码完成的尝试都将根据需要失败。

要审核问题的严重程度,IntelliJ IDEA 的一个有用起点是分析依赖项

http://www.jetbrains.com/idea/webhelp/analyzing-dependency.html

从该文章中,您可以了解如何运行项目的依赖关系分析并对其进行操作。

You can use multiple modules in IDEA or Maven or multiple projects in Eclipse and Gradle. The concept is the same in all cases.

A trivial interpretation would be a module for myapp.server.bl and another for myapp.client.ui.customlayout with no compile time dependencies between either of them. Now any attempt to compile code or code-complete against the opposite module/project will fail as desired.

To audit how extensive the problem already is, a useful starting point for IntelliJ IDEA is Analyzing Dependencies:

http://www.jetbrains.com/idea/webhelp/analyzing-dependencies.html

From that article you can see how to run and act on dependency analysis for your project.

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