为一个简单的 java 项目选择什么样的原型

生成提供了太多的选项,我希望用 junit 测试用例创建一个简单的 java 实用程序。我想知道我应该在这里使用什么原型?

75529 次浏览

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.

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

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.

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.

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.

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

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.

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

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.

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