gpt4 book ai didi

maven - Maven 如何与 Java 9 模块一起工作?

转载 作者:行者123 更新时间:2023-12-04 20:29:38 51 4
gpt4 key购买 nike

如果我的项目中有所有类型的模块(应用程序、自动和未命名的),Maven 将如何与它们一起工作?
我可以强制 Maven 将一些 jar 视为自动模块,而其他模块则保留在类路径中。 Maven如何逐步迁移到模块系统?

最佳答案

Maven 只是管理你的依赖项(jars)。它不关心依赖项是否是 java 模块。
Maven 可以提供帮助的唯一方法是,如果您通过 Maven 启动应用程序(例如 mvn spring-boot:run),那么您可以添加一些 JVM 参数,例如 --add-modules .
如果您想知道自动和未命名的模块,这完全取决于您如何启动应用程序,are two ways how you can do that :

  • 从模块代码启动您的应用程序:

  • Since a module must require all of its dependencies and those can only be fulfilled by other named modules (i.e. not JARs on the class path) all dependencies of a modular JAR must be placed on the module path. Yes, even non-modular JARs, which will then get turned into automatic modules.

    The interesting thing is that automatic modules can read the unnamed module, so their dependencies can go on the class path.


  • 从非模块代码启动您的应用程序:

  • and because non-modular code does not express any dependencies, it will not resolve modules from the module path.

    So if non-modular code depends on artifacts on the module path, you need to add them manually with the --add-modules option.

    For example if you want to use ServiceLoader.load(Foo.class); and you compile your application from non-modular code you'll have to add provider module of the Foo class explicity to module graph with --add-modules.


    来自 The State of the Module System 的注释:

    If a package is defined in both a named module and the unnamed module then the package in the unnamed module is ignored.

    关于maven - Maven 如何与 Java 9 模块一起工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49771784/

    51 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com