如何在Eclipse Oxygen 1a Release(4.7.1a)中使用maven with Java9.0.1和pom打包?

时间:2017-11-10 17:42:39

标签: maven m2e java-9 eclipse-oxygen

下面给出的maven项目示例显示了Eclipse Oxygen中 module-info.java 中的错误:

log4j.api cannot be resolved to a module. 

如果我删除该行

<packaging>pom<packaging>
从pom.xml

错误消失。但是,我需要使用pom包装。如果我在没有模块定义的情况下使用Java8,那么我现实世界中的maven部分非常有效。尝试迁移到Java9遇到了这个新问题。首先我以为我不会正确引用log4j依赖项。然后我发现这与我在多模块项目中需要的pom包装有关。我创建了一个下面给出的最小示例,以允许您在Eclipse中重现错误消息。

=&gt;这是M2E插件的错误(1.8.2.20171007-0217)吗?

=&gt;如果没有,我如何调整我的pom.xml文件以使用Java9?

相关问题:

最小示例项目(我的真实例子更复杂):

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

    }

}

使用全新安装的maven run配置的输出:

[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] ------------------------------------------------------------------------

整个Eclipse示例项目:

https://github.com/stefaneidelloth/java9MavenEclipse

使用的工具:

Eclipse for RCP和RAP Developers,Oxygen 1a Release(4.7.1a) (包括M2E版本1.8.2.20171007-0217)

Java JDK版本9.0.1

0 个答案:

没有答案