带有Maven的Java 9 - 找不到模块

时间:2018-01-12 15:15:47

标签: maven intellij-idea maven-3 java-9

我正在努力将项目从Java 8迁移到Java 9。

使用:IntelliJ IDEA 2017.2.5

Maven版本:3.5.1

到目前为止,我为我要迁移的所有模块创建了module-info.java。在这里,requires适用于java.base。但是所有其他外部模块/依赖关系都没有,比如我们项目中的log4j和包还没有迁移到Java 9.这些包也作为POM中的依赖项添加。但是,IntelliJ不会在此处抛出错误。 Navigation from module-info.java to the corresponding JAR in External Libraries works.

但是只要我使用clean install运行构建,就会出现编译失败:module not found: log4j。与所有其他外部包相同。

知道为什么会出现这种错误吗?我认为非模块化的依赖关系将被Java 9变成一个自动模块。我做错了什么或者没理解正确的方法?

这是项目的POM:

<groupId>de.project</groupId>
<artifactId>basicProject</artifactId>
<version>1.0</version>
<name>Basic Project</name>

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>9</source>
                    <target>9</target>
                    <compilerVersion>3.7.0</compilerVersion>
                </configuration>
                <version>3.5.1</version>
            </plugin>
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.0.1</version>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <!-- here the phase you need -->
                        <phase>package</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>../output/intern/</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>intern</directory>
                                    <includes>
                                        <include>loggingConfig.xml</include>
                                    </includes>
                                    <!--<filtering>true</filtering>-->
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.build.resourceEncoding>UTF-8</project.build.resourceEncoding>
</properties>

<repositories>
    <repository>
        <id>some_repository</id>
        <name>somerepos</name>
        <url>http://some.rep.de/plugin/repo/everything</url>
    </repository>
</repositories>

<dependencies>
    <!--the first project -->
    <dependency>
        <groupId>de.project</groupId>
        <artifactId>firstProject</artifactId>
        <version>1.2</version>
    </dependency>
    <!--the second project -->
    <dependency>
        <groupId>de.project</groupId>
        <artifactId>secondProject</artifactId>
        <version>1.0</version>
    </dependency>
    <dependency>
        <groupId>de.project</groupId>
        <artifactId>thirdProject</artifactId>
        <version>1.0</version>
    </dependency>
    <dependency>
        <groupId>de.project</groupId>
        <artifactId>fourthProject</artifactId>
        <version>1.4</version>
    </dependency>

    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>

    <dependency>
        <groupId>com.someitextpdf</groupId>
        <artifactId>someitextpdf</artifactId>
        <version>5.5.12</version>
    </dependency>

    <dependency>
        <groupId>io.github.lzf0349</groupId>
        <artifactId>jdatepicker</artifactId>
        <version>2.0.3</version>
    </dependency>

    <dependency>
        <groupId>dom4j</groupId>
        <artifactId>dom4j</artifactId>
        <version>1.6.1</version>
    </dependency>

    <!--libs for testing -->
    <dependency>
        <groupId>org.hamcrest</groupId>
        <artifactId>hamcrest-all</artifactId>
        <version>1.3</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>2.3.0</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-annotations</artifactId>
        <version>3.5.6-Final</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-envers</artifactId>
        <version>5.2.1.Final</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

项目结构:

  • Project SDK:9.0(java版“9.0.1”)
  • 项目语言级别:9 - 接口中的模块,私有方法等。
  • 模块SDK:Project SDK(9.0)
  • 模块 - &gt;来源 - &gt;语言水平:9 - 接口中的模块,私有方法等。
  • 平台设置:SDK:9.0,JDK主路径C:\ Program Files \ Java \ jdk-9.0.1

2 个答案:

答案 0 :(得分:4)

我遇到了同样的问题。我最终做的和工作是从我在pom.xml中的Eclipse创建的pom中插入构建元素:

<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.7.0</version>
            <executions>
                <execution>
                    <id>default-compile</id>
                    <phase>compile</phase>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                    <configuration>
                        <source>9</source>
                        <target>9</target>
                    </configuration>
                </execution>
                <execution>
                    <id>default-testCompile</id>
                    <phase>test-compile</phase>
                    <goals>
                        <goal>testCompile</goal>
                    </goals>
                    <configuration>
                        <source>9</source>
                        <target>9</target>
                    </configuration>
                </execution>
            </executions>
            <configuration>
                <source>9</source>
                <target>9</target>
            </configuration>
        </plugin>
    </plugins>
</build>

答案 1 :(得分:1)

我要再回答这个问题,因为不是每个人都可以阅读评论...

对我来说,maven-compiler-plugin版本是问题所在。似乎自3.8.0起才支持模块。

这是我的pom.xml中的部分:

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

    <maven.compiler.source>9</maven.compiler.source>
    <maven.compiler.target>9</maven.compiler.target>
</properties>

<build>
    <plugins>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
        </plugin>

    </plugins>
</build>