Skip to content
Advertisement

Is there a maven repository to quickstart Java 11 app

There is a quickstart maven archetype for Java 7, as I can see here: https://maven.apache.org/archetypes/maven-archetype-quickstart/ The problem with this is when I fire this command:

mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart

Enter project directory and fire this command:

mvn package

I get this error:

[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

Please note that there is some problem with my JDK install, as update-alternatives tells me that I am running 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

But, when I run java -version, it gives me this:

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

I am not sure why I am not getting OpenJDK 11, despite the fact that I installed JDK 11 only in the first place. Maybe will start a different thread on that.

Advertisement

Answer

tl;dr

The quickstart archetype, sadly, is often out-of-date.

Update your maven-surefire-plugin element of your POM to the latest version such as 3.0.0-M7.

Update your dependencies to current version

Oddly, the maven-archetype-quickstart artifact is never kept up-to-date. It lists dependencies with old versions that may not work well with the latest versions of Java, your IDE, or other libraries.

This is quite frustrating, as you would think the artifact would be kept up-to-date via some automated scripting. After all, the entire point of Maven is to make this kind of configuration chore easy and automated!

Fortunately, you can easily update the version numbers yourself.

How do you know what is the latest version number to enter for each dependency?

  • You can look up manually the latest version number using the web site of a Maven repository. Copy-paste the version number to your POM.
  • You can let your IDE (IntelliJ, NetBeans, etc.) suggest the latest number as you edit the version number XML element value. You may or may not to type some “help-me” keystroke depending on how your IDE works. Before doing this, be sure to update your IDE’s cache of Maven repository data so it knows the current latest version numbers. For example, in IntelliJ go to preferences, and in the search field for settings, type repo, and in the list of known repositories click each Update button.

Steps

First, if using your IDE to work with Maven, be sure to update its cache of Maven repository data so you are getting the latest quickstart artifact. For example, in IntelliJ, Preferences > Build, Execution, Deployment > Build tools > Maven > Repositories > Update button.

enter image description here

The initial POM should expand into a bigger one. You may need a Maven clean and install.

After first use, go through your 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>

On each element in the POM, change the version number to the latest.

Your IDE may assist by showing you a list of version numbers it knows of.

enter image description here

Or use a Maven repository web site to verify the latest version number. Like this:

enter image description here

You may find most of the items are out-of-date. In particular, regarding the surefire problem described in the Question, change the surefire element to v3.

    <plugin>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>3.0.0-M3</version>
    </plugin>

This will fix your surefire specifically, in my experience. Some problems were recently fixed in later versions. I don’t recall the nature of the problems, perhaps related to the Java Platform Module System.

My version of the POM after updating to the latest as of 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>

JUnit

Notice I also updated JUnit from v4 to the vastly improved v5 “Jupiter”. If you do this, you will also need to update the AppTest.java file to simply change the import lines.

Old:

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 );
    }
}

New:

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 );
    }
}

Lastly, do a Maven clean and install to get those latest versions of your dependencies actually installed inside your app.

By the way, for more about JUnit Jupiter, and how to run old JUnit 3 & 4 testes, see my Answer to another Question.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement