限制Java包之间的依赖关系
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我相信 Checkstyle 对此有一个检查。
它称为导入控件
I believe Checkstyle has a check for that.
It's called Import Control
您可以配置 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.
ivy 似乎是解决您问题的一个很好的解决方案(如果您使用的是 ant)。 Ivy 是 Ant 的官方依赖管理组件,因此与 ant 集成得很好。 它能够解决依赖关系、处理冲突、创建排除等。
它使用简单的 xml 结构来描述依赖关系,并且比 Maven 更易于使用,因为它只尝试解决依赖关系解析问题。
来自ivy主页:
ivy是一个用于管理(记录、跟踪、解决和报告)项目依赖关系的工具。 它具有以下特点:
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:
Eclipse 通过构建路径属性/jar 属性对此提供支持。 我认为它可能只能跨 jar/项目边界工作。
Eclipse has support for this via Build Path properties / jar properties. I think it may only work across jar / project boundaries.
对于 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).
也许可以使用Classsycle:
http://classycle.sourceforge.net/ddf.html
Maybe Classsycle can be used:
http://classycle.sourceforge.net/ddf.html
您可以在 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.