101

mvn archetype:generate provides way too many options and I am looking to create a simple java utility with junit test cases. I would like to know what archetype I should be using here?

Jean-Rémy Revy
  • 5,607
  • 3
  • 39
  • 65
Jason
  • 12,229
  • 20
  • 51
  • 66
  • I recommend that you read the [Maven Getting Started Guide](http://maven.apache.org/guides/getting-started/index.html). It's short and covers a lot of ground. – Sahil Muthoo Aug 23 '11 at 08:54

11 Answers11

80

I use two archetypes. It depends on what kind of application you will create.

If you want a web application, use maven-archetype-webapp, or if you want a simple application use maven-archetype-quickstart. They are useful because you will be able to expand them with no problem.

jwaddell
  • 1,892
  • 1
  • 23
  • 32
Oleksandr
  • 2,346
  • 4
  • 22
  • 34
  • 1
    Note that this is in the Eclipse plugin by default (at least it is today). – Ben Dec 03 '13 at 06:45
  • here I am going to create maven-archetype-webapp and download the maven catalog but getting an error. – Mowgli Dec 20 '19 at 06:51
  • what should I do? – Mowgli Dec 20 '19 at 06:51
  • The only thing bothering me about maven-archetype-quickstart is that the dependency version it generates are old, and no new version has been released since 2018. Does anyone know how to contribute to this? – gerrytan Mar 16 '23 at 11:56
24

I'm using command like below:

mvn archetype:generate -Dfilter=org.apache.maven.archetypes:

I will get a short list of achetypes only from org.apache.maven.archetypes groupId. The good ones for starting is maven-archetype-quickstart and maven-archetype-webapp like my predecessors said.

marioosh
  • 27,328
  • 49
  • 143
  • 192
  • Note the final ":" at the end of the filter necessary to filter by groupId (https://maven.apache.org/archetype/maven-archetype-plugin/generate-mojo.html) – Cristiano Costantini Jun 07 '21 at 04:24
18

When you do a mvn archetype:generate, a default selection appears in enclosing curly brackets (), e.g. (1274), if you scroll up to see what #1274 is, it is usually the default Java archetype to try out or start out with, if doing simple Java projects, so is safe to select.

rogerdpack
  • 62,887
  • 36
  • 269
  • 388
Oh Chin Boon
  • 23,028
  • 51
  • 143
  • 215
12

Here's what you need

 mvn archetype:generate -DgroupId=com.example -DartifactId=foobar -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Reference: Build Run Java Maven Project Command Line

Sorter
  • 9,704
  • 6
  • 64
  • 74
9

I think you should use maven-archetype-simple

EDIT

According to the maven documentation:

  • maven-archetype-quickstart An archetype which contains a sample Maven project.
  • maven-archetype-simple An archetype which contains a simple Maven project.
AlexR
  • 114,158
  • 16
  • 130
  • 208
  • 5
    I don't see `maven-archetype-simple` in my Eclipse selection, but I see `maven-archetype-quickstart`. Are they the same? – huahsin68 Jan 14 '14 at 04:15
  • Please take a look on my later edit that refers to maven documentation. Concerning to eclipse plugin: I personally prefer to edit `pom.xml` manually and run command line from command prompt. – AlexR Oct 27 '14 at 10:28
6

I would start with a very simple pom.xml file which has only what you need. Something like

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.app</groupId>
  <artifactId>my-module</artifactId>
  <version>1</version>
</project>

from http://maven.apache.org/guides/introduction/introduction-to-the-pom.html

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 14
    While this advice may have merit, it doesn't answer the question. The whole point of archetypes, as I understand it, is to save the developer from this kind of boilerplate, and make it easy to follow best practices. – Michael Scheper Mar 10 '14 at 23:56
3

You can use any of the basic ones from The Practical Developer:

Java 8 + commonly-used test libraries:

mvn archetype:generate -DgroupId=[your-project-groupId] -DartifactId=[your-project-name] -DarchetypeGroupId=com.thepracticaldeveloper -DarchetypeArtifactId=archetype-java-basic-tpd -DarchetypeVersion=1.0.0

Java 9 + commonly-used test libraries

mvn archetype:generate -DgroupId=[your-project-groupId] -DartifactId=[your-project-name] -DarchetypeGroupId=com.thepracticaldeveloper -DarchetypeArtifactId=archetype-java-basic-tpd -DarchetypeVersion=1.0.0 -Djava-version=9

They include JUnit 4, Mockito and AssertJ, and a default manifest file in case you want your jar file to be executable. More info: https://thepracticaldeveloper.com/archetypes/

Disclaimer: I'm the author of that blog.

Moisés
  • 491
  • 4
  • 12
2

You may want to consider Java Archetype: it has reasonable defaults for quickly starting a Java project following best practices, including JUnit tests. I conceived it as a modern successor of maven-archetype-quickstart.

Example: mvn archetype:generate -DarchetypeGroupId=io.github.oliviercailloux -DarchetypeArtifactId=java-archetype -DgroupId=mygroupid -DartifactId=myartifactid -DinteractiveMode=false.

Disclaimer: I am the author.

For more information about alternatives, here is a part of that project’s README (it provides more links than copied here).

The official quickstart archetype

The official archetype (mentioned by Apache’s Maven Getting Started Guide and by the Apache Maven Cookbook) for simple Java projects is maven-archetype-quickstart, or org.apache.maven.archetypes:maven-archetype-quickstart in full.

It should, IMHO, be considered deprecated, as it suffers several weaknesses.

  • An important problem is that it creates projects that depend on JUnit 4. The current version, JUnit 5, differs significantly, providing among other an improved API.
  • Minor weaknesses: its default version is 1.0-SNAPSHOT, which should be 1.0.0-SNAPSHOT to follow the recommended and usual scheme.
  • Another minor weakness: it creates projects configured for a 1.7 JVM. This may be appropriate if you particularly need to support old installs, but opting for a reasonably recent JVM is a more reasonable default rule for new projects. For example, Java 8 introduced lambda expressions, which you probably do not want to miss out.

My archetype also provides logging by default, which I think is useful, scales better than sysout and does not hurt, and Guava, which I think usefully enriches Java.

Other quickstart archetypes in Maven Central

In order to join efforts if possible and avoid wasteful duplication, I actively searched for other archetypes that would have the same aim as mine: provide a simple archetype with reasonable defaults to easily start a modern Java project. (This was mostly done around June 2020.)

A general search on the internet led me to The Practical Developer. When contacted, he wrote to me (by e-mail) that he does not work on his archetype regularly and therefore preferred to decline collaborating on such a project.

As searches on the net did not reveal other useful results, and as I found no specialised search tool suitable for my needs, I implemented a simple archetype browser. It lists all the archetypes available in Maven Central. There are far too many to review manually, thus, I selected those whose groupId and artifactId existed since at least three years and have been updated during the last year, in hope of finding projects that are maintained on the long run, which I suppose indicates more probably a good quality project. (Of course this filter may have missed good quality archetypes that perfectly match the stated goal; I have no way to know. As a case in point, this very archetype does not pass that filter as I have changed its artifactId over time.)

I then filtered manually the resulting list on the basis of the archetypes descriptions found in their POM, and gave a further look (on the official website, typically) for a few promising archetypes among them. Only com.github.ngeor:archetype-quickstart-jdk8 revealed to be a suitable candidate. But its author wrote to me that he is “not really using/maintaing the archetype much these days”.

Olivier Cailloux
  • 977
  • 9
  • 24
1

maven-archetype-quickstart An archetype which contains a sample Maven project. maven-archetype-simple An archetype which contains a simple Maven project.

if get errors fixed it by adding the maven archetype catalog to eclipse. Steps are provided below:

Open Window > Preferences> Maven > Archetypes

Click Add Remote Catalog and add the following:

Catalog File: http://repo1.maven.org/maven2/archetype-catalog.xml

Description: maven catalog

NOTE: INTERNET CONNECTION IS REQUIRED DURING CREATION OF YOUR FIRST MAVEN PROJECT.

Community
  • 1
  • 1
1

the default archetype number now is 1092 1092: remote -> org.apache.maven.archetypes:maven-archetype-quickstart (An archetype which contains a sample Maven project.)

0

Maven Quickstart Archetype

As others noted, the usual starting point for a simple Java app is the QuickStart archetype.

Produces these folders and files:

diagram of files and folders in a newly created project using QuickStart archetype

From the command line in a console:

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

Note there that 1.4 is the current version, as of 2023-07.

In the IntelliJ IDE, start a new project as seen in this screenshot.

screenshot of New Project dialog box in IntelliJ with QuickStart archetype selected

Change the version from 1.1 to 1.4 (currently the latest). Inexplicably, the IntelliJ dialog never offers the latest version.

enter image description here

Open the Advanced Settings panel. Specify the various fields of Maven info: GroupID, ArtifactID, and Version. See this Answer for guidance.

enter image description here

Click Create button to produce the new project. Wait a moment as Maven does its job, expanding the archetype into a real project.

Unfortunately, we are not ready to do our Java programming. The POM.xml file needs some tweaks. The QuickStart archetype, even in its latest version, is quite out-of-date with the versions of its various parts. In my experience, it is generally best to change all the versions to their respective latest.

First, update the version of Java for your project. You should usually be using either:

  • One of the LTS versions: 8, 11, 17.
  • The latest version: 20.

And when specifying the Java version, in modern tooling we can replace the pair of .source & .target tags with a single .release tag.

<maven.compiler.release>20</maven.compiler.release>

Load that first change to the POM. In IntelliJ, look for the little floating windoid with the blue M logo. Click that logo. Otherwise, open the Maven panel within IntelliJ. Click the first icon for Reload all Maven projects.

Verify that your project runs. Open the App.java file. Click the hollow green icon for a pop-up menu; choose Run App.main(). See that you get successful output in the console panel of the IDE.

Back in the POM.xml, change from using JUnit 4 to using the JUnit 5 Jupiter technology. Replace the entire <dependency> tag for junit to the JUnit Jupiter (Aggregator) dependency. The Aggregator means that a few related dependencies are brought in for your convenience.

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

Modernize the auto-generated App.test file to use JUnit 5 Jupiter. Replace both import lines with these:

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

Click the green hollow icon to choose run shouldAnswerWithTrue(). Look for the green checkmarks in the *Run` panel of IntelliJ.

Next we update the version numbers of the remaining parts.

Update your local cache of Maven dependency data. In the IntelliJ settings, go to Build, Execution, Deployment > Build Tools > Maven > Repositories. To quickly go there, just type repo in the Find field at top of the column in Settings. Once you have reached that panel, select each of the listed Maven repositories, and click the Update button. Performing the updates may take several minutes.

After updating, you may see the latest version number by deleting the content of each <version> tag, and press Control + Spacebar. You may want to verify the actual latest numbers against a Maven repository’s web site.

When done, you should have a POM that looks something like this:

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

    <name>ExampleQS</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.release>20</maven.compiler.release>
    </properties>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>5.10.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.2.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.3.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.11.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>3.0.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>3.3.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>3.1.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>3.1.0</version>
                </plugin>
                <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
                <plugin>
                    <artifactId>maven-site-plugin</artifactId>
                    <version>4.0.0-M3</version>
                </plugin>
                <plugin>
                    <artifactId>maven-project-info-reports-plugin</artifactId>
                    <version>3.4.1</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

Do a Maven clean. Run your app, and run your test. All should be well. You are now ready to start your Java programming.


This Answer uses IntelliJ as an example. You should be able to do something quite similar in other IDEs such as Eclipse or NetBeans. One of the main benefits of Maven (and alternatives such as Gradle) is that it works the same across various IDEs.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154