[英]Java 9 automatic module dependencies cannot be resolved / module not found
I'm trying to migrate some legacy applications to the new Java 9 module system, to strengthen its encapsulation. 我正在尝试将一些遗留应用程序迁移到新的Java 9模块系统,以加强其封装。
I'm starting from the outside-in, with the assumption that classes on the periphery will have the least external dependencies. 我从外向内开始,假设外围的类具有最少的外部依赖性。
As you'd expect, I've declared a very open module to start with: 正如您所期望的那样,我已经宣布了一个非常开放的模块:
module com.example.user {
exports com.example.user;
}
This instantly breaks the entire project (inside all classes), when suddenly every import for an external dependency no longer resolves (causing over 1k Java problems): 这会立即打破整个项目(在所有类中),突然每个外部依赖项的导入都不再解析(导致超过1k的Java问题):
The import com.otherexample cannot be resolved
导入com.atherexample无法解析
The import org.springframework cannot be resolved
无法解析导入org.springframework
etc.
等等
Local packages in the same project com.example.price
still work - as do java.util
etc. 同一个项目
com.example.price
中的本地包仍然有效 - 就像java.util
等一样。
All of the external dependencies are (were) managed with Maven. 使用Maven管理所有外部依赖项。 In the (Eclipse project) build path, I can still see them as "Classpath" dependencies - but only the JRE system libraries in the "Modulepath".
在(Eclipse项目)构建路径中,我仍然可以将它们视为“Classpath”依赖项 - 但只能看到“Modulepath”中的JRE系统库。
Can the two concepts co-exist? 这两个概念可以共存吗? Currently it seems by having a single
module-info.java
anywhere in the project, all classpath dependencies stop working? 目前看来项目中的任何地方都有一个
module-info.java
,所有类路径依赖都停止了吗?
I did read about using automatic modules , which seemed to imply you could use legacy / non-modular jars by including them in your modulepath, then referring to them by their filename. 我读过关于使用自动模块的内容 ,这似乎意味着您可以通过将它们包含在模块路径中来使用传统/非模块化jar,然后通过文件名引用它们。 They use the example:
他们使用的例子:
module com.foo.myapp {
requires guava; // guava not yet modularised, so use the filename
}
I couldn't find much other info, but this appears to match the convention Eclipse uses when it auto-generates a module-info.java for example: 我找不到太多其他信息,但这似乎与Eclipse在自动生成module-info.java时使用的约定相符:
spring-core-4.3.8.RELEASE.jar
becomes: 变为:
requires spring.core;
However, this still results in a Java error reported by Eclipse: 但是,这仍然导致Eclipse报告的Java错误:
spring.core cannot be resolved to a module
spring.core无法解析为模块
Maven reports: Maven报道:
[ERROR] module-info.java:[39,16] error: module not found: spring.core
...and every class in the project with an external dependency is still broken. ...并且项目中具有外部依赖性的每个类仍然被破坏。
Thanks to Robert Scholte for pointing out the updated maven-compiler-plugin 3.7.0 (I had been using 3.6.1), this really cleaned up the compile goal command-line output (with Java 9 specifics), to help me get to the route of the problem. 感谢Robert Scholte指出更新的maven-compiler-plugin 3.7.0(我一直在使用3.6.1),这确实清理了编译目标命令行输出(使用Java 9细节),以帮助我到达问题的路线。 This narrowed down the reported errors from every
requires
giving me the error to: 这缩小了每个
requires
的报告错误,给出了错误:
[WARNING] ********************************************************************************************************************
[WARNING] * Required filename-based automodules detected. Please don't publish this project to a public artifact repository! *
[WARNING] ********************************************************************************************************************
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 56 source files to ./target/classes
~~~ snip ~~~
[ERROR] module-info.java error: module not found: foo.bar
Matching Eclipse: 匹配Eclipse:
foo.bar cannot be resolved to a module
The errors appearing for just six automatic modules / libraries (jar) - rather than all (24) of them. 只出现六个自动模块/库(jar)的错误 - 而不是全部(24个)错误。 Great.
大。
In my POM, I'd split the output of source directories, to their own output directories ( target/classes
). 在我的POM中,我将源目录的输出拆分为它们自己的输出目录(
target/classes
)。 However, as the module-info.java
referred to dependencies (such as requires spring.core;
) that are not used / referenced by the code (classes) in that folder - it couldn't resolve them. 但是,由于
module-info.java
引用了该文件夹中的代码(类)未使用/引用的依赖项(例如requires spring.core;
),因此无法解析它们。
Why? 为什么? Basic Maven dependency management - I'd scoped those libraries outside of the default goal (to match the output directories split).
基本Maven依赖关系管理 - 我将这些库限定在默认目标之外(以匹配输出目录split)。
A fairly basic outcome - but I'd imagine I will not be the only person to encounter this as Java begins to encroach on some aspects of dependency management that overlap with the traditional use of Maven. 一个相当基本的结果 - 但我想我不会是唯一遇到这个问题的人,因为Java开始侵犯依赖管理的某些方面,这些方面与传统的Maven使用重叠。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.