带有JDK10 SDK的IntelliJ无法编译具有1.8目标的Maven项目

时间:2018-07-26 06:09:49

标签: java intellij-idea compilation java-10 jigsaw

在maven项目中考虑以下代码:

import org.w3c.dom.css.CSSStyleDeclaration;

public class Test {
    public static void main(String[] args) {
        CSSStyleDeclaration styleDeclaration = null;
    }
}

pom.xml包含maven-compiler-plugin插件的地方

<?xml version="1.0" encoding="UTF-8"?>
<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>me.serce.jdk10mvntest</groupId>
    <artifactId>jdk10mvntest</artifactId>
    <version>1.0-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

可以使用maven正确编译该项目。但是,当使用IntelliJ和JDK10 SDK进行编译时,会出现Error:(1, 27) java: package org.w3c.dom.css does not exist错误。如果将源更改为<source>10</source>,该错误将消失。我希望问题可能与jigsaw有关,但是我不能使用--add-modules ...,因为javac出现了option --add-modules not allowed with target 1.8错误。

是否有任何变通办法使我可以将IntelliJ与JDK10 SDK以及1.8源和目标一起使用来编译此代码?


IntelliJ:2018.2
Java:Java HotSpot(TM)64位服务器VM 18.3(内部版本10.0.1 + 10,混合模式)

1 个答案:

答案 0 :(得分:6)

禁用“使用--release选项进行交叉编译”:

--release

有关更多详细信息,请参见this issuemy another answer about this option