How to use maven with Java9.0.1 and pom packaging

2019-02-16 03:50发布

问题:

The maven project example that is given below shows an error in module-info.java in Eclipse Oxygen:

log4j.api cannot be resolved to a module. 

If I remove the line

<packaging>pom<packaging>

from pom.xml the error disappears. However, I need to use pom packaging. If I use Java8 without module definitions, the maven part in my real world example works very well. Trying to migrate to Java9 confronted me with this new issue. First I thought I would not correctly reference the log4j dependency. Then I found out that is has something to do with the pom packaging that I need in my multi-module project. I created a minimal example that is given below to allow you to reproduce the error messages in Eclipse.

=>Is this a bug of the M2E plugin (1.8.2.20171007-0217)?

=>If not, how do I have to adapt my pom.xml file to work with Java9?

Related issues:

  • How to use log4j with maven and java9?

  • Import of a Java-9-Jigsaw-Maven-Project in Eclipse Oxygen 4.7 does not work

  • Java 9 Modules and JUnit 4

  • Can Maven generate the module declaration

  • Where should I put unit tests when migrating a Java 8 project to Jigsaw

  • Which module should I require in Java 9 to use JPA?

  • List of java modules: http://download.java.net/java/jigsaw/docs/api/overview-summary.html

Min example project (my real example is more complex):

pom.xml:

<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>Log4JWithJava9</groupId>
    <artifactId>Log4JWithJava9</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

    <properties>
        <!-- encoding -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <build>

        <plugins>

            <!-- plugin for resource phase -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.0.2</version>
                <executions>
                    <execution>
                        <id>resource-execution</id>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>resources</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <!-- plugin for compile phase (and test-compile phase) -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <!-- specify current java version here: -->
                    <source>9</source>
                    <target>9</target>
                </configuration>
                <executions>
                    <execution>
                        <id>compile-execution</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>test-compile-execution</id>
                        <phase>test-compile</phase>
                        <goals>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <!-- ### PACKAGE ### phase -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.0.2</version>
                <executions>
                    <execution>
                        <id>package-execution</id>
                        <phase>package</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                        <configuration>

                        </configuration>
                    </execution>
                </executions>
            </plugin>

        <!-- plugin for install phase -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-install-plugin</artifactId>
            <version>2.5.2</version>
            <executions>
                <execution>
                    <id>install-execution</id>
                    <phase>install</phase>
                    <goals>
                        <goal>install</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

        </plugins>
    </build>

    <dependencies>

        <!-- log4j -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.9.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.9.1</version>
        </dependency>

    </dependencies>

</project>

module-info.java

module Log4JWithJava9 {
    exports isi.share;      
    requires javafx.base;
    requires log4j.api; 
}

Main.java

package isi.share;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;


public class Main {

    private static Logger sysLog = LogManager.getLogger(Main.class);

    public static void main(String[] args) {

    }

}

Output for maven run configuration with clean install:

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building Log4JWithJava9 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ Log4JWithJava9 ---
[INFO] Deleting D:\EclipseJava\workspace\Log4JWithJava9\target
[INFO] 
[INFO] --- maven-resources-plugin:3.0.2:resources (resource-execution) @ Log4JWithJava9 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\EclipseJava\workspace\Log4JWithJava9\src\main\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.7.0:compile (compile-execution) @ Log4JWithJava9 ---
[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 2 source files to D:\EclipseJava\workspace\Log4JWithJava9\target\classes
[INFO] 
[INFO] --- maven-compiler-plugin:3.7.0:testCompile (test-compile-execution) @ Log4JWithJava9 ---
[INFO] No sources to compile
[INFO] 
[INFO] --- maven-jar-plugin:3.0.2:jar (package-execution) @ Log4JWithJava9 ---
[INFO] Building jar: D:\EclipseJava\workspace\Log4JWithJava9\target\Log4JWithJava9-0.0.1-SNAPSHOT.jar
[INFO] 
[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ Log4JWithJava9 ---
[INFO] Installing D:\EclipseJava\workspace\Log4JWithJava9\pom.xml to C:\Users\eis\.m2\repository\Log4JWithJava9\Log4JWithJava9\0.0.1-SNAPSHOT\Log4JWithJava9-0.0.1-SNAPSHOT.pom
[INFO] 
[INFO] --- maven-install-plugin:2.5.2:install (install-execution) @ Log4JWithJava9 ---
[INFO] Installing D:\EclipseJava\workspace\Log4JWithJava9\pom.xml to C:\Users\eis\.m2\repository\Log4JWithJava9\Log4JWithJava9\0.0.1-SNAPSHOT\Log4JWithJava9-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.520 s
[INFO] Finished at: 2017-11-10T20:41:10+01:00
[INFO] Final Memory: 15M/52M
[INFO] ------------------------------------------------------------------------

Whole Eclipse example project:

https://github.com/stefaneidelloth/java9MavenEclipse

Tools used:

Eclipse for RCP and RAP Developers, Oxygen 1a Release (4.7.1a) (Including M2E version 1.8.2.20171007-0217)

Java JDK version 9.0.1