简体   繁体   English

Eclipse 2019-03 关于 Java 模块和 maven

[英]Eclipse 2019-03 regarding Java modules and maven

I am having a maven (3.6.0) project based on java 11 with the following structure (which works fine on the commandline!):我有一个基于 java 11 的 maven (3.6.0) 项目,其结构如下(在命令行上运行良好!):

src/main/java/
  module-info.java
  /de/test/tp/TP.java

src/test/java/
  /de/test/tp/test/TPTests.java

The module-info.java looks as following: module-info.java 如下所示:

module de.test.tp
 {
  exports de.test.tp;

  requires org.apache.logging.log4j;
 }

TP.java: TP.java:

package de.test.tp;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class TP
 {
  private static final Logger LOGGER = LogManager.getLogger(TP.class);

  public TP()
   {
    super();
    LOGGER.info("test");
   }
 }

TPTests.java: TPTests.java:

package de.test.tp.test;

import static org.junit.jupiter.api.Assertions.assertNotNull;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.jupiter.api.Test;

import de.test.tp.TP;

public class TPTests
 {
  private static final Logger LOGGER = LogManager.getLogger(TP.class);

  public TPTests()
   {
    super();
   }

  @Test
  public void defaultConstructor()
   {
    final TP tp = new TP();
    assertNotNull(tp, "Default constructor failed!");
   }
 }

Last but not least the important parts of pom.xml最后但并非最不重要的 pom.xml 的重要部分

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.0</version>
        <configuration>
          <fork>true</fork>
          <showDeprecation>true</showDeprecation>
          <showWarnings>true</showWarnings>
          <optimize>false</optimize>
          <debug>true</debug>
          <release>11</release>
        </configuration>
      </plugin>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.22.1</version>
        <dependencies>
          <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.4.1</version>
          </dependency>

        </dependencies>
      </plugin>
    </plugins>
  </build>

  <dependencies>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-engine</artifactId>
      <version>5.4.1</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-api</artifactId>
      <version>2.11.2</version>
    </dependency>

    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-core</artifactId>
      <version>2.11.2</version>
    </dependency>

   </dependencies>

When I now say (from within Eclipse) "maven/update project" the eclipse puts all maven dependencies to the classpath .当我现在说(在 Eclipse 中)“maven/update project”时,eclipse 将所有 maven 依赖项放到classpath 中 After recompiling eclipse tells me:重新编译 eclipse 后告诉我:

The package org.apache.logging.log4j is accessible from more than one module: <unnamed>, org.apache.logging.log4j

for both org.apache.logging.log4j imports.对于org.apache.logging.log4j导入。

So the question is, how to fix that?所以问题是,如何解决这个问题?

Update 1更新 1

What I really want is a clear project structure for a java modules project that is based on maven, works in eclipse and supports white- and blackbox testing.我真正想要的是一个基于 maven、在 eclipse 中工作并支持白盒和黑盒测试的 java 模块项目的清晰项目结构。 Can somebody give me such a project skeleton?有人可以给我这样的项目骨架吗?

Update 2更新 2

Or is my problem that eclipse does not have multi-module support as I read in some articles?还是我在某些文章中读到的 eclipse 没有多模块支持的问题? - Which also would lead me back to the question of Update 1. - 这也会让我回到更新 1 的问题。

Update 3更新 3

Shortened the whole question and added complete (shortened) file contents.缩短整个问题并添加完整(缩短)的文件内容。

Note 1注 1

During all my testing for example with having a seconds module-info.java under test/java/ I found that eclipse 2019-03 is very instable and has bugs!在我所有的测试中,例如在test/java/下有几秒钟的 module-info.java 我发现 eclipse 2019-03 非常不稳定并且有错误! For example - Sometime when trying to delete the module-info.java under test - Eclipse was not able to delete it.例如 - 有时在尝试删除测试中的 module-info.java 时 - Eclipse 无法删除它。 Another effect was that when editing the module-info.java with the test path eclipse also edit's the module-info.java under the main path.另一个效果是,当使用测试路径 eclipse 编辑 module-info.java 时,也会编辑主路径下的 module-info.java。 This means that within the main module-info.java I found that the exort has changed to de.test.tp.test - when I fixed that (in eclipse editor) I could not save the file.这意味着在主 module-info.java 中,我发现 exort 已更改为 de.test.tp.test - 当我修复(在 eclipse 编辑器中)时,我无法保存文件。 When I fixed that in an external editor and refresh/clean the project eclipse still tells me that the de.test.tp.test would not exist - so I have to delete the errors manually from the marker tab.当我在外部编辑器中修复并刷新/清理项目时,eclipse 仍然告诉我 de.test.tp.test 不存在 - 所以我必须从标记选项卡中手动删除错误。

So from my point of view eclipse 2019-03 has some bugs regarding the handling of java modules.因此,从我的角度来看,eclipse 2019-03 在处理 java 模块方面存在一些错误。

Note 2笔记2

As you can see from the comments below @howIger has reported this as a bug in Eclipse .正如您从下面的评论中看到的,@howIger 已将此报告为Eclipse 中错误

Note 3注 3

Looks to me like it is fixed now in eclipse 2019-06 :)在我看来,它现在已经在 eclipse 2019-06 中修复了:)

The error says that there is more than one module (probably a JAR) that contains the package org.apache.logging.log4j (or to be more precise, from which the package is accessible).该错误表示有多个模块(可能是 JAR)包含包org.apache.logging.log4j (或者更准确地说,可以从中访问该包)。 This is not allowed in the Java Platform Module System (JPMS).在 Java 平台模块系统(JPMS) 中是不允许的 But in this case, there is only one JAR that contains the package, so this error is shown by mistake in Eclipse 2019-03 (4.11) .但是在这种情况下,只有一个 JAR 包含该包,因此在 Eclipse 2019-03 (4.11) 中错误地显示了错误 See:看:

Eclipse bug 546315 - "The package […] is accessible from more than one module: , […]" error shown in Java editor by mistakeEclipse 错误 546315 - “包 [...] 可从多个模块访问:,[...]”错误显示在 Java 编辑器中

As workaround for this bug do one of the following:作为此错误的解决方法,请执行以下操作之一:

  • In module-info.java add the line requires org.apache.logging.log4j.core;module-info.java添加行requires org.apache.logging.log4j.core; which pulls all related JARs on the modulepath它在模块路径上拉取所有相关的 JAR
  • Ignore the error (as it does not prevent the code to be compiled)忽略错误(因为它不会阻止编译代码)
  • Downgrade to Eclipse 2018-12 (4.10) or wait until Eclipse 2019-06 (4.12)降级到 Eclipse 2018-12 (4.10) 或等到 Eclipse 2019-06 (4.12)

By default, all Maven dependencies are on the classpath.默认情况下,所有 Maven 依赖项都在类路径上。 In module-info.java the line requires org.apache.logging.log4j;module-info.java该行requires org.apache.logging.log4j; pulls the JAR with the org.apache.logging.log4j module on the modulepath.使用模块路径上的org.apache.logging.log4j模块拉取 JAR。 The error erroneously states that there is another JAR on the classpath (and therefore in the unnamed module) that also contains the package org.apache.logging.log4j .该错误错误地指出类路径上(因此在未命名模块中)还有另一个 JAR,它也包含包org.apache.logging.log4j Please note, modules-info.test (with the file extension .test ) is neither a Java nor a Maven thing and therefore for Eclipse only a text file.请注意, modules-info.test (文件扩展名为.test )既不是 Java 也不是 Maven 的东西,因此对于 Eclipse 来说只是一个文本文件。

Multi-module support : in your case you have only one module-info.java which means you only have one Java module.多模块支持:在您的情况下,您只有一个module-info.java ,这意味着您只有一个 Java 模块。 In Eclipse for each Java module a Java project is required (due each module has its own dependencies).在 Eclipse 中,每个 Java 模块都需要一个 Java 项目(因为每个模块都有自己的依赖项)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM