How to use maven with Java9.0.1 and pom packaging in Eclipse Oxygen 1a Release (4.7.1a)?
Asked Answered
I

0

0

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:

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

enter image description here

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

Ignazio answered 10/11, 2017 at 17:42 Comment(12)
Well, the question also is why do you want to package pom and still use dependencies and not <dependencyManagement>? Are those dependencies used in the parent level as well? All this assuming we are talking about the same pom packaging.Impudicity
And a weird thing in the screenshot, what is the other src folder doing there? And another note, the sample as shared by you in question. Works fine for me.Impudicity
And what command are you executing such that you get the stated error?Impudicity
a)Yes, in my real example the dependencies are used in another maven project, that references a sub project using <modules> tag. b) The src folder is the same one. It is shown twice in the Eclipse view. c) The error can be seen after updating the Maven project in Eclipse (Alt+F5)Ignazio
a. please edit the question with appropriate details... b. doesn't seem to be just a view, looks like a symlink....c. what happens if you execute mvn clean install on the project?Impudicity
I added the output of maven and committed the example project to github.Ignazio
Okay, seems specific to Eclipse you mean? Please update the complete details of the libraries and tool versions used.Impudicity
First why do you manually bind the plugins to the life cycle ? Does not make sense. Use the usual jar packaging. Correctly define the compiler versionPesky
Comments like "why are you doing this" do not really help me and I would prefer if you could concentrate on the "how". As I already stated using the jar packaging would avoid the error in this minimal example but my question clearly targets the pom packaging. I did not ask "When to use pom packaging?". Nevertheless thank you for your comments. The issue might be specific to the M2E plugin of Eclipse. But I am not sure about it since I just started with Java9. If you want to help please check out the example form the linked github project.Ignazio
@Pesky For my real world example: If I want to use modules I have to use pom packaging. The pom packaging does not include compile phase. In order to compile I bind the compile plugin. For the "min example" the reason is that I wanted to kind of simulate the jar packaging to better understand what the difference between using pom packaging or not might be. I thought the pom packaging might miss a phase that is required for the Java9 modules to work correctly.Ignazio
@Ignazio First let make something clear. If I'm asking why it is based the need to understand the problem first before making a suggestion for a solution. If you this will not help Ok than I can't help you. Apart from that your assumption is simply wrong. To use modules you can simply use jar packaging as usual. The problem you have is simply that the log4j dependencies do not contain a module-info.class file. They are called automatic modules which means their module name is derived from the artifact name. And that's the reason for the WARNING of the maven-compiler-plugin. (Part I)Pesky
Part II: The problems in Eclipse is related to issues related to the support of JDK 9 in Eclipse related to autonamed modules...or to m2e... (BTW: Made a simple example project: github.com/khmarbaise/simple-jdk9-maven).Pesky

© 2022 - 2024 — McMap. All rights reserved.