是否有一个 Maven 存储库来快速启动 Java 11 应用程序

Pra*_*hra 9 java maven

Java 7 有一个快速入门 ma​​ven 原型,正如我在这里看到的:https : //maven.apache.org/archetypes/maven-archetype-quickstart/ 问题是当我触发这个命令时:

mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart
Run Code Online (Sandbox Code Playgroud)

进入项目目录并执行以下命令:

mvn package
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.20.1:test (default-test) on project gfg-stuff: Execution default-test of goal org.apache.maven.plugins:maven-surefire-plugin:2.20.1:test failed. NullPointerException
Run Code Online (Sandbox Code Playgroud)

请注意,我的 JDK 安装存在一些问题,因为它update-alternatives告诉我我正在运行 JDK 11:

$ sudo update-alternatives --config java
There are 2 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                         Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-11-openjdk-amd64/bin/java   1101      auto mode
  1            /usr/lib/jvm/java-11-openjdk-amd64/bin/java   1101      manual mode
  2            /usr/lib/jvm/java-8-oracle/jre/bin/java       1081      manual mode
Run Code Online (Sandbox Code Playgroud)

但是,当我运行时java -version,它给了我这个:

$ java -version
openjdk version "10.0.2" 2018-07-17
OpenJDK Runtime Environment (build 10.0.2+13-Ubuntu-1ubuntu0.18.04.3)
OpenJDK 64-Bit Server VM (build 10.0.2+13-Ubuntu-1ubuntu0.18.04.3, mixed mode)
Run Code Online (Sandbox Code Playgroud)

我不确定为什么我没有获得 OpenJDK 11,尽管我首先安装了 JDK 11。也许会就此开始一个不同的话题。

Bas*_*que 8

tl;博士

quickstart遗憾的是,原型往往已经过时。

maven-surefire-plugin将 POM的元素更新到最新版本,例如3.0.0-M3.

将您的依赖项更新到当前版本

奇怪的是,maven-archetype-quickstart工件从来没有保持最新。它列出了旧版本的依赖项,这些依赖项可能不适用于最新版本的 Java、您的 IDE 或其他库。

这很令人沮丧,因为您会认为工件会通过一些自动脚本保持最新。毕竟,Maven 的全部意义在于让这种配置工作变得简单和自动化!

幸运的是,您可以轻松地自己更新版本号。

您如何知道为每个依赖项输入的最新版本号是多少?

  • 您可以使用 Maven 存储库的网站手动查找最新版本号。将版本号复制粘贴到您的 POM。
  • 您可以让您的 IDE(IntelliJ、NetBeans 等)在您编辑版本号 XML 元素值时建议最新的数字。根据您的 IDE 的工作方式,您可能会也可能不会键入一些“帮助我”键击。在执行此操作之前,请务必更新 IDE 的 Maven 存储库数据缓存,以便它知道当前的最新版本号。例如,在 IntelliJ 中转到首选项,然后在设置的搜索字段中键入repo,然后在已知存储库列表中单击每个Update按钮。

脚步

首先,如果使用您的 IDE 来处理 Maven,请务必更新其 Maven 存储库数据的缓存,以便您获得最新的quickstart工件。例如,在 IntelliJ 中,Preferences> Build, Execution, Deployment> Build tools> Maven> Repositories>Update按钮。

在此处输入图片说明

最初的 POM 应该扩展为更大的 POM。您可能需要一个 Mavenclean和install.

首次使用后,请检查您的 POM。

<?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>work.basil.work.example</groupId>
  <artifactId>quickstart</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>quickstart</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

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

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>
Run Code Online (Sandbox Code Playgroud)

在 POM 中的每个元素上,将版本号更改为最新的。

您的 IDE 可能会通过向您显示它知道的版本号列表来提供帮助。

在此处输入图片说明

或者使用 Maven 存储库网站来验证最新版本号。像这样:

在此处输入图片说明

您可能会发现大多数项目都已过时。特别是对于问题中描述的surefire问题,将surefire元素更改为v3。

    <plugin>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>3.0.0-M3</version>
    </plugin>
Run Code Online (Sandbox Code Playgroud)

surefire根据我的经验,这将专门解决您的问题。最近在更高版本中修复了一些问题。我不记得问题的性质,可能与Java Platform Module System 相关。

我的 POM 版本更新到 2019-05-29 的最新版本。

<?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>work.basil.work.example</groupId>
  <artifactId>quickstart</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>quickstart</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

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

  <dependencies>
    <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter -->
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter</artifactId>
      <version>5.5.0-M1</version>
      <scope>test</scope>
    </dependency>

  </dependencies>

  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>3.0.0-M3</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.1.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>3.0.0-M1</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>3.0.0-M1</version>
        </plugin>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>
Run Code Online (Sandbox Code Playgroud)

JUnit

请注意,我还将JUnit从 v4更新到了大幅改进的 v5“Jupiter”。如果您这样做,您还需要更新AppTest.java文件以简单地更改import行。

老的:

package work.basil.work.example;

import static org.junit.Assert.assertTrue;

import org.junit.Test;

/**
 * Unit test for simple App.
 */
public class AppTest 
{
    /**
     * Rigorous Test :-)
     */
    @Test
    public void shouldAnswerWithTrue()
    {
        assertTrue( true );
    }
}
Run Code Online (Sandbox Code Playgroud)

新的:

package work.basil.work.example;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertTrue;

/**
 * Unit test for simple App.
 */
public class AppTest 
{
    /**
     * Rigorous Test :-)
     */
    @Test
    public void shouldAnswerWithTrue()
    {
        assertTrue( true );
    }
}
Run Code Online (Sandbox Code Playgroud)

最后,执行 Mavenclean并install获取实际安装在应用程序中的最新版本的依赖项。

顺便说一下,有关 JUnit Jupiter 以及如何运行旧的 JUnit 3 和 4 测试的更多信息,请参阅我对另一个问题的回答。