运行 Java 10 中的 Maven 原型 "quickstart"?

Run Maven archetype "quickstart" in Java 10?

当我在 macOS Sierra 上使用 Oracle 10.0.1 从 Maven 原型 maven-archetype-quickstart 版本 1.3 创建新的 IntelliJ 2018.1 项目时,我在执行 Maven install.[=32 时遇到此错误=]

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.20.1:test (default-test) on project h2e2: Execution default-test of goal org.apache.maven.plugins:maven-surefire-plugin:2.20.1:test failed.: NullPointerException -> [Help 1]

我在创建新项目后直接运行宁此。我正在编写 no 代码。我可以运行默认行:

    public static void main( String[] args ) { System.out.println( "Hello World!" ); }

…通过在代码编辑器中单击的上下文菜单中选择 Run 'App.main()'。这行得通,但是 Maven install 失败了。

在 IntelliJ 的 Maven 面板中执行 cleaninstall 没有解决问题。

如果我切换 IntelliJ File > Project Structure > Project > Project SDK > 从 10.0.1 到 1.8.0_171(并设置 Project language level8 - Lambdas, type annotations etc.),然后 Maven install 成功。

[INFO] ------------------------------------------------------------------------

[INFO] BUILD SUCCESS

[INFO] ------------------------------------------------------------------------

如果我 return 设置为 Java 10,Maven install 再次失败,出现同样的错误。

我 运行 正在使用外部 Maven 3.5.3 而不是 IntelliJ 捆绑的旧版本,没有特别的原因。

我现有的基于 Java8 的旧项目没有问题。我唯一的问题是尝试使用 Java 10.

创建新的基于 Maven 的 quickstart 项目

maven-surefire-plugin

的更高版本

Java10 的原型似乎还没有更新。

根据此博客 post:http://joshlong.com/jl/blogPost/java-10.html,您可以执行以下操作,然后 运行...

.... I want to use Java 10, though, so I opened the pom.xml file and changed the java.version property value to be 10. The Maven surefire plugin broke the build next; it was complaining about not being able to parse the version of the JDK. I overrode the version by redefining the property for the plugin's version: 2.21.0.

这里的重要部分是我认为更新到更高版本的 surefire,如评论中所述。截至 2018-10 年,current version2.22.1

<plugin>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.22.1</version>
</plugin>

是正确和关键的。

将编译器 source/target 设置为 10

除了将版本 maven-surefire-plugin 更新为 运行 Java 10 上基于 Maven "Quickstart" 原型的项目之外,您还需要更改POM 文件 表明 Java 10 将被编译器使用 .

改变这个:

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
</properties>

1.7 对替换为 10:

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>10</maven.compiler.source>
    <maven.compiler.target>10</maven.compiler.target>
</properties>