关于Java模块和Maven的Eclipse 2019-03 [英] Eclipse 2019-03 regarding Java modules and maven

查看:58
本文介绍了关于Java模块和Maven的Eclipse 2019-03的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于java 11的maven(3.6.0)项目,具有以下结构(在命令行上可以正常工作!):<​​/p>

I am having a maven (3.6.0) project based on java 11 with the following structure (which works fine on the commandline!):

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

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

module-info.java如下所示:

The module-info.java looks as following:

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

  requires org.apache.logging.log4j;
 }

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!");
   }
 }

pom.xml的最后但并非最不重要的部分

Last but not least the important parts of 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>

当我现在(在Eclipse中)说"maven/update项目"时,日食将所有maven依赖项放入 classpath .重新编译后,eclipse告诉我:

When I now say (from within Eclipse) "maven/update project" the eclipse puts all maven dependencies to the classpath. After recompiling eclipse tells me:

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

对于两个org.apache.logging.log4j导入,均为

.

for both org.apache.logging.log4j imports.

问题是,该如何解决?

更新1

我真正想要的是一个基于maven的Java模块项目的清晰项目结构,该项目可在Eclipse中工作,并支持白盒和黑盒测试.有人可以给我这样的项目框架吗?

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. Can somebody give me such a project skeleton?

更新2

还是我在某些文章中阅读时eclipse没有多模块支持的问题? -这还会使我回到Update 1的问题.

Or is my problem that eclipse does not have multi-module support as I read in some articles? - Which also would lead me back to the question of Update 1.

更新3

缩短了整个问题,并添加了完整(缩短的)文件内容.

Shortened the whole question and added complete (shortened) file contents.

注释1

在所有测试中,例如,在test/java/下有一个秒的module-info.java,我发现eclipse 2019-03非常不稳定并且有错误!例如-有时尝试删除被测模块-info.java时-Eclipse无法删除它.另一个效果是,当使用测试路径eclipse编辑module-info.java时,还编辑了主路径下的module-info.java.这意味着在主模块info.java中,我发现该替代已更改为de.test.tp.test-当我修复该问题(在eclipse编辑器中)时,无法保存该文件.当我在外部编辑器中修复该问题并刷新/清理该项目时,eclipse仍然告诉我de.test.tp.test将不存在-因此,我必须从标记"选项卡中手动删除错误.

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! For example - Sometime when trying to delete the module-info.java under test - Eclipse was not able to delete it. 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. 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. 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 2019-03在处理Java模块方面存在一些错误.

So from my point of view eclipse 2019-03 has some bugs regarding the handling of java modules.

注释2

从下面的评论中可以看到,@ howIger将其报告为 Eclipse中的错误.

As you can see from the comments below @howIger has reported this as a bug in Eclipse.

注释3

在我看来,它已在Eclipse 2019-06中修复:)

Looks to me like it is fixed now in eclipse 2019-06 :)

推荐答案

错误表明存在个以上的模块(可能是JAR),其中包含程序包org.apache.logging.log4j (或更确切地说,可以从中访问该软件包). 在Java平台模块系统(JPMS)中是不允许的.但是在这种情况下,只有一个JAR包含该软件包,因此此错误在Eclipse 2019-03(4.11)中被错误显示.参见:

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). This is not allowed in the Java Platform Module System (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). See:

Eclipse bug 546315 -该软件包[ …]可通过多个模块访问:,[…]错误在Java编辑器中错误显示

Eclipse bug 546315 - "The package […] is accessible from more than one module: , […]" error shown in Java editor by mistake

作为此错误的解决方法,请执行以下任一操作:

As workaround for this bug do one of the following:

  • module-info.java中添加行requires org.apache.logging.log4j.core; ,该行将所有相关的JAR提取到模块路径上
  • 忽略错误(因为它不会阻止代码被编译)
  • 降级到Eclipse 2018-12(4.10)或等到Eclipse 2019-06(4.12)

默认情况下,所有Maven依赖项都在类路径上.在module-info.java中,行requires org.apache.logging.log4j;在模块路径中使用org.apache.logging.log4j模块提取JAR.该错误错误地指出,类路径上(因此在 unnamed 模块中)还有另一个JAR,该JAR也包含包org.apache.logging.log4j.请注意,modules-info.test(文件扩展名为.test)既不是Java也不是Maven,因此对于Eclipse来说只是一个文本文件.

By default, all Maven dependencies are on the classpath. In module-info.java the line requires org.apache.logging.log4j; pulls the JAR with the org.apache.logging.log4j module on the modulepath. 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. 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.

多模块支持:在您的情况下,您只有一个module-info.java,这意味着您只有一个Java模块.在Eclipse中,每个Java模块都需要一个Java项目(由于每个模块都有自己的依赖项).

Multi-module support: in your case you have only one module-info.java which means you only have one Java module. In Eclipse for each Java module a Java project is required (due each module has its own dependencies).

这篇关于关于Java模块和Maven的Eclipse 2019-03的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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