打开项目时在Eclipse 2018-12中找不到自动模块 [英] Automatic modules not found in Eclipse 2018-12 when project is opened

查看:360
本文介绍了打开项目时在Eclipse 2018-12中找不到自动模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Eclipse 2018-12从Oracle JDK 8切换到Open JDK 11.我有以下情况:

I want to switch from Oracle JDK 8 to Open JDK 11 using Eclipse 2018-12. I have the following situation:

在Eclipse工作区中,我有一个名为 example 的主maven项目,其中包含一些maven依赖项 my_dependency_1,my_dependency_2 ,它们也是同一工作区中的maven项目.当前,只有 example 是模块化的(因此包含 module-info.java ).其他项目是非模块化的,并包含在 pom.xml module-info.java required (自动)模块中>的示例.我认识到以下几点:

In my Eclipse workspace I have a main maven project called example with some maven dependencies my_dependency_1, my_dependency_2 that are also maven projects in the same workspace. Currently, only example is modular (and thus contains a module-info.java). The other projects are non-modular and are included in the pom.xml as well as required (automatic) modules in the module-info.java of example. I recognized the following:

当我关闭工作区中除 example 之外的所有Eclipse项目时,所有(非模块化)依赖项.jars都将从模块路径中的.m2存储库中正确地包含为自动模块,而我 example 项目运行正常.我可以通过查看运行配置>参数>显示命令行来验证这一点.

When I close all Eclipse projects in my workspace except example, then all (non-modular) dependency .jars get correctly included from the .m2 repository in the module-path as automatic modules and my example project runs fine. I can verify this by looking at Run Configurations > Arguments > Show Command Line.

相反,当我在工作区中也打开具有依赖项的项目时,依赖项不包含在模块路径中,因此 example 项目不会运行(导致在引导层初始化期间发生错误,java.lang.module.FindException:模块my_dependency_1,...未找到,示例必需.).

In contrast, when I open the projects with my dependencies in my workspace as well, the dependencies get not included into the module-path and thus the example project doesn't run (resulting in Error occurred during initialization of boot layer, java.lang.module.FindException: Module my_dependency_1, ... not found, required by example).

我的问题是:如果我想打开它们并与之一起使用,是否需要将所有 my_dependencies_1,... 从非模块化依赖项转换为模块化依赖项?我在工作区中的模块化 example 项目,或者是否有任何选项可以让我即使在非模块化依赖项目中也可以继续工作?我正在寻找一个简单,干净的解决方案,而不是在手动添加一些时髦的东西到启动参数上的黑客".如果无法做到这一点,我宁愿花一些时间将每个依赖项转换为模块化的依赖项,其缺点是在仍为Java 8编写的其他项目中不再可用.

My question is: Do I need to convert all my_dependencies_1, ... from non-modular to modular dependencies if I want to open and work with them along with my modular example project in my workspace, or is there any option that lets me keep working even with the non-modular dependency projects? I'm looking for a simple and clean solution and not for a "hack" with manually appending funky stuff to the startup arguments. If this is not possible I'd prefer to take some time to convert each dependency into a modular one with the drawback of being not usable anymore in other projects still written for Java 8.

感谢您的澄清:)

这是演示的基本示例:

pom.xml (来自项目 example )

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <artifactId>example</artifactId>
  <version>1.0-SNAPSHOT</version>

  <dependencies>
    <dependency>
      <groupId>com.example</groupId>
      <artifactId>mydependency</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
  </dependencies>
</project>

Example.java (属于项目 example )

package example;
import mydependency.MyDependency;

public class Example {
    public static void main(String[] args) { 
        MyDependency.run();
    }
}

module-info.java (来自项目 example )

open module example {
    requires mydependency;
}

pom.xml (属于项目 mydependency )

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <artifactId>mydependency</artifactId>
  <version>1.0-SNAPSHOT</version>
</project>

MyDependency.java (属于项目 mydependency )

package mydependency;
public class MyDependency {
    public static void run() {
        System.out.println("run");
    }
}

如果项目已关闭,则运行时的命令行(正常工作):

Command line when running if project is closed (works properly):

C:\me\jdk-11.0.1\bin\javaw.exe
-Dfile.encoding=UTF-8
-p "C:\me\workspace\example\target\classes;C:\me\.m2\repository\com\example\mydependency\1.0-SNAPSHOT\mydependency-1.0-SNAPSHOT.jar"
-m example/example.Example

如果打开了项目,则运行时的命令行(缺少独立性的目标/类):

Command line when running if project is opened (target/classes of mydependency is missing):

C:\me\jdk-11.0.1\bin\javaw.exe
-Dfile.encoding=UTF-8
-p "C:\me\workspace\example\target\classes"
-m example/example.Example

推荐答案

源项目不可能是自动模块,因为自动模块的名称是从jar文件名派生的.

A source project cannot possibly be an automatic module, because the name of an automatic module is derived from the jar filename.

您需要的是:

    每个依赖项中的
  • mvn install
  • Maven > Disable Workspace Resolution在依赖自动模块(程序包资源管理器"中的上下文菜单)的每个项目上
  • mvn install in each dependency
  • Maven > Disable Workspace Resolution on each project that depends on an automatic module (context menu in the Package Explorer)

  • 或者,可以禁用所有包含自动模块的项目,而不是禁用工作空间分辨率.
  • 受此影响的人们可能会拥护 Eclipse Bug 543925 ,m2e和JDT之间的合作可以进行超出JPMS规定的范围:如果可以从jar文件中访问给定的项目 ,则m2e可以告诉JDT自动模块名称为.然后,在运行该程序的幕后将需要忽略工作区分辨率,以便JVM找到所需的jar文件.
  • Alternatively, instead of disabling workspace resolution, all projects containing automatic modules can be closed.
  • People affected by this may champion Eclipse Bug 543925, where a cooperation between m2e and JDT could go beyond what is specified by JPMS: m2e could tell JDT what would be the automatic module name, if a given project would be accessed from it's jar file. Behind the scenes running that program would then require to ignore the workspace resolution, so that the JVM finds the needed jar file.

这篇关于打开项目时在Eclipse 2018-12中找不到自动模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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