简体   繁体   中英

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

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

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

  <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 . After recompiling eclipse tells me:

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.

So the question is, how to fix that?

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

Update 2

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.

Update 3

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

Note 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! 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.

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

Note 2

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

Note 3

Looks to me like it is fixed now in 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). 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 - "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:

  • In module-info.java add the line requires org.apache.logging.log4j.core; which pulls all related JARs on the modulepath
  • 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)

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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