java - 我应该如何命名我的 Java 9 模块?

标签 java java-9 java-module

假设我有一个包含 groupId = org.abcartifactId = myLibrary 的库。模块名称的推荐名称是什么:myLibraryorg.abc.myLibrary?有命名方案的官方指南吗?

最佳答案

有一段时间对你的问题有两种不同的看法,但在模块系统的开发过程中,社区决定采用反向 DNS 方法。

唯一模块名称 - 反向 DNS

这里假设模块名称应该是全局唯一的。鉴于该目标,实现该目标的最实用方法遵循包命名策略并反转维护者关联的域名。 The State of the Module System says this :

Module names, like package names, must not conflict. The recommended way to name a module is to use the reverse-domain-name pattern that has long been recommended for naming packages.

此外,马克·莱因霍尔德 writes :

Strongly recommend that all modules be named according to the reverse Internet domain-name convention. A module's name should correspond to the name of its principal exported API package, which should also follow that convention. If a module does not have such a package, or if for legacy reasons it must have a name that does not correspond to one of its exported packages, then its name should at least start with the reversed form of an Internet domain with which the author is associated.

这很清楚,其他 Java 专家分享了 like Stephen Colebourne .

模块大于工件

有一段时间(2016 年初),有另一个建议进行了轮次。 JDK 团队表示,模块名称毕竟可能不必是唯一的,“因为模块比定义它们的工件更抽象”。 Mark Reinhold writes :

Choose module names that start with the name of your project or product. Module (and package) names that start with reversed domain names are less likely to conflict but they're unnecessarily verbose, they start with the least-important information (e.g., com, org, or net), and they don't read well after exogenous changes such as open-source donations or corporate acquisitions (e.g., com.sun.*).

The reversed domain-name approach was sensible in the early days of Java, before we had development tools sophisticated enough to help us deal with the occasional conflict. We have such tools now, so going forward the superior readability of short module and package names that start with project or product names is preferable to the onerous verbosity of those that start with reversed domain names.

此外,模块名称不包含域将允许将该模块与另一个实现交换,只要它具有相同的名称(并且当然实现相同的公共(public) API)。

在上面列出的邮件中,Reinhold 记录了他的意见改变:

Some people may prefer shorter, project-oriented names, and those can be fine to use in limited projects that will never, ever see the light of day outside of a single organization. If you create a module that has even a small chance of being open-sourced in the future, however, then the safest course is to choose a reverse-DNS name for it at the start.

总结

陪审团在场,所有公开发表意见的人都同意反向 DNS,如包。

关于java - 我应该如何命名我的 Java 9 模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43192741/

相关文章:

java - 如何使用 FTP 协议(protocol)恢复读取输入流

java - 添加到 DateTimeFormatterBuilder 的文字破折号导致解析失败

Java 模块化与依赖注入(inject)

Java 9 + Maven : What happens if two dependencies export the same module?

java - 列出Java9中所有必需的模块?

java - 如何将 JSP 页面内的表单提交给 servlet?

java - StringEscapeUtils.escapeXml 正在转换它不应该转换的 utf8 字符

使用jre 6.0通过代理更新eclipse

Java 9 在禁用调整大小后更改了 JFrame 的默认标题高度

java - 我可以从 Java 模块中排除导出的包吗?