java - 如何在 Maven 中表达对 java ee 特性的依赖以过渡到 Java 9?

标签 java maven jaxb java-9 java-platform-module-system

我们使用 maven 并拥有依赖于其他内部 Artifact 的 Artifact 。我正在迁移到 ,并打算首先将所有内容迁移到 Java 9 而不模块化代码(即在未命名的模块中)。

我遇到的问题是我们依赖于 java.xml.bind,它现在不包含在默认模块中。是否有一种“正确”的方式来表达对 java.xml.bind 的这种依赖关系?

最佳答案

Module System谈到从类路径加载应用程序的情况下的未命名模块构建模块图的方式。此外,从文档本身来看:-

When the compiler compiles code in the unnamed module, or the java launcher is invoked and the main class of the application is loaded from the class path into the unnamed module of the application class loader, then the default set of root modules for the unnamed module is computed as follows:

  • The java.se module is a root, if it exists. If it does not exist then every java.* module on the upgrade module path or among the system modules that exports at least one package, without qualification, is a root.

  • Every non-java.* module on the upgrade module path or among the system modules that exports at least one package, without qualification, is also a root.

Otherwise, the default set of root modules depends upon the phase:

  • At compile time it is usually the set of modules being compiled (more on this below);

  • At link time it is empty; and

  • At run time it is the application's main module, as specified via the --module (or -m for short) launcher option.

It is occasionally necessary to add modules to the default root set in order to ensure that specific platform, library, or service-provider modules will be present in the module graph. In any phase the option

--add-modules <module>(,<module>)* where <module> is a module name, adds the named modules to the default set of root modules.

jetty.project 中遇到了类似的问题在哪里 thread来自 jdk 邮件列表中讨论的相同内容,修复方法是使用:

--add-modules java.se.ee

这为他们提供了对所有 Java SE 模块的访问权限,在您的情况下应该是:

--add-modules java.xml.bind

要在 maven 中使用它,您可以将其嵌入到 maven-compiler-plugin 使用

<compilerArgs>
    <arg>--add-modules</arg>
    <arg>java.xml.bind</arg>
</compilerArgs>

根据 ZhekaKozlov here 的建议.


需要注意的重要一点是,将 API 标记为弃用也意味着您可能不想使用它。要适应这种方式,您可能可以开始使用对 jaxb-api:2.3.0 的依赖。现在可以作为模块加载,也可以从类路径中执行。您需要进行的更改是将以下内容添加到您的依赖项列表中:

<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.3.0</version>
</dependency>

更新:- 最终,Java-10 已经推出,JDK/11 即将推出,理想情况下应该点击 JEP 320: Remove the Java EE and CORBA Modules 的链接并进一步用它们的独立库替换这些依赖项。

关于java - 如何在 Maven 中表达对 java ee 特性的依赖以过渡到 Java 9?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46084751/

相关文章:

java - Hibernate 延迟加载抛出 JsonMappingException

java - Android Studio 中的某些 Java 文件未显示在 GitHub Desktop 上

java - App Engine Cloud Endpoints Frameworks (Java) - 在本地运行时出现 NullPointerException

java - 文件类型 Vaadin Paper Input 中的文件浏览窗口打开操作会触发哪个事件

maven - 使用 maven scm 插件

java - 通过 apache poi 读取 excel 文件(位于类路径中)

java - 如何使用 JAXB 附加外部生成的 XML 而不是 POJO 的变量值?

java - 如何使用 archetype 创建 Maven 插件

javax注解问题

java - session 树 xml 表示为 java 对象