无法解析Java 9自动模块依赖性/找不到模块 [英] Java 9 automatic module dependencies cannot be resolved / module not found

查看:356
本文介绍了无法解析Java 9自动模块依赖性/找不到模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一些旧应用程序迁移到新的Java 9模块系统中,以增强其封装性.

I'm trying to migrate some legacy applications to the new Java 9 module system, to strengthen its encapsulation.

我从外而内开始,假设外围的类具有最小的外部依赖性.

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;
}

这突然中断了整个项目(在所有类内部),突然,外部依赖项的每次导入都不再解决(导致1k多个Java问题):

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):

导入com.otherexample无法解析

The import com.otherexample cannot be resolved

导入的org.springframework无法解析

The import org.springframework cannot be resolved

同一项目com.example.price中的本地软件包仍然有效-java.util等也是如此

Local packages in the same project com.example.price still work - as do java.util etc.

所有外部依赖项都通过Maven进行管理.在(Eclipse项目)构建路径中,我仍然可以将它们视为"Classpath"依赖项-但只有"Modulepath"中的JRE系统库.

All of the external dependencies are (were) managed with Maven. In the (Eclipse project) build path, I can still see them as "Classpath" dependencies - but only the JRE system libraries in the "Modulepath".

这两个概念可以共存吗?当前看来,通过在项目中的任何地方只有一个module-info.java,所有类路径依赖项都将停止工作?

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?

我确实阅读过有关使用自动模块的信息,这似乎暗示您可以通过将旧版/非模块化jar包含在模块路径中,然后通过其文件名来引用它们来使用它们.他们使用示例:

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. They use the example:

module com.foo.myapp {
  requires guava;  // guava not yet modularised, so use the filename
}

我找不到很多其他信息,但这似乎与Eclipse自动生成module-info.java时使用的约定相符,例如:

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:

spring-core-4.3.8.RELEASE.jar

成为:

requires spring.core;

但是,这仍然会导致Eclipse报告Java错误:

However, this still results in a Java error reported by Eclipse:

spring.core无法解析为模块

spring.core cannot be resolved to a module

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.

推荐答案

感谢 Robert Scholte 指出更新的maven-compiler-plugin 3.7.0(我一直在使用3.6.1),这确实清除了编译目标命令行输出(具有Java 9规范),以帮助我找到问题的根源.这样可以缩小每个requires中报告的错误的范围,使我的错误范围为:

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. This narrowed down the reported errors from every requires giving me the error to:

[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

匹配Eclipse:

foo.bar cannot be resolved to a module

仅六个自动模块/库(jar)出现错误,而不是全部(24个)出现.很好.

The errors appearing for just six automatic modules / libraries (jar) - rather than all (24) of them. Great.

在我的POM中,我将源目录的输出分成了自己的输出目录(target/classes).但是,由于module-info.java引用了该文件夹中的代码(类)未使用/未引用的依赖项(例如requires spring.core;),因此无法解析它们.

In my POM, I'd split the output of source directories, to their own output directories (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.

为什么?基本的Maven依赖关系管理-我将那些库的作用域设置为默认目标之外(以匹配拆分的输出目录).

Why? Basic Maven dependency management - I'd scoped those libraries outside of the default goal (to match the output directories split).

一个相当基本的结果-但是我想我不会成为唯一遇到这种情况的人,因为Java开始侵犯与传统Maven重叠的某些依赖管理方面.

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 9自动模块依赖性/找不到模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆