Getting started with Maven: What archetype to choose for a simple java project - Maven Tutorial



Normally I use two archetypes. It depends on what kind of application you want to create.

1. If you want a web application, use maven-archetype-webapp, or2. 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 am providing 2 ways to do this:
Method 1: Providing all info in one command:

Example:

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

Executing on CMD:

username $ mvn archetype:generate -DgroupId=com.example -DartifactId=foobar -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false[INFO] Scanning for projects...[INFO] [INFO] ------------------------------------------------------------------------[INFO] Building Maven Stub Project (No POM) 1[INFO] ------------------------------------------------------------------------[INFO] [INFO] >>> maven-archetype-plugin:3.0.1:generate (default-cli) > generate-sources @ standalone-pom >>>[INFO] [INFO] <<< maven-archetype-plugin:3.0.1:generate (default-cli) < generate-sources @ standalone-pom <<<[INFO] [INFO] [INFO] --- maven-archetype-plugin:3.0.1:generate (default-cli) @ standalone-pom ---[INFO] Generating project in Batch modeDownloading: https://repo.maven.apache.org/maven2/org/apache/maven/archetypes/maven-archetype-quickstart/1.0/maven-archetype-quickstart-1.0.pomDownloaded: https://repo.maven.apache.org/maven2/org/apache/maven/archetypes/maven-archetype-quickstart/1.0/maven-archetype-quickstart-1.0.pom (703 B at 1.0 kB/s)Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/archetypes/maven-archetype-bundles/2/maven-archetype-bundles-2.pomDownloaded: https://repo.maven.apache.org/maven2/org/apache/maven/archetypes/maven-archetype-bundles/2/maven-archetype-bundles-2.pom (1.5 kB at 2.7 kB/s)Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/archetype/maven-archetype-parent/1/maven-archetype-parent-1.pomDownloaded: https://repo.maven.apache.org/maven2/org/apache/maven/archetype/maven-archetype-parent/1/maven-archetype-parent-1.pom (1.3 kB at 2.5 kB/s)Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-parent/4/maven-parent-4.pomDownloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven-parent/4/maven-parent-4.pom (10.0 kB at 19 kB/s)Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/archetypes/maven-archetype-quickstart/1.0/maven-archetype-quickstart-1.0.jarDownloaded: https://repo.maven.apache.org/maven2/org/apache/maven/archetypes/maven-archetype-quickstart/1.0/maven-archetype-quickstart-1.0.jar (4.3 kB at 8.0 kB/s)[INFO] ----------------------------------------------------------------------------[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.0[INFO] ----------------------------------------------------------------------------[INFO] Parameter: basedir, Value: /Users/admin/Desktop[INFO] Parameter: package, Value: com.example[INFO] Parameter: groupId, Value: com.example[INFO] Parameter: artifactId, Value: foobar[INFO] Parameter: packageName, Value: com.example[INFO] Parameter: version, Value: 1.0-SNAPSHOT[INFO] project created from Old (1.x) Archetype in dir: /Users/admin/Desktop/foobar[INFO] ------------------------------------------------------------------------[INFO] BUILD SUCCESS[INFO] ------------------------------------------------------------------------[INFO] Total time: 16.427 s[INFO] Finished at: 2018-04-02T23:12:07+05:30[INFO] Final Memory: 16M/54M[INFO] ------------------------------------------------------------------------username $

Method 2: Providing info step by step and Filtering to reduce archetype list
mvn archetype:generate -Dfilter=org.apache.maven.archetypes:

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

Example:

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

Executing on CMD:

username $ mvn archetype:generate -Dfilter=org.apache.maven.archetypes:maven-archetype-quickstart[INFO] Scanning for projects...[INFO] [INFO] ------------------------------------------------------------------------[INFO] Building Maven Stub Project (No POM) 1[INFO] ------------------------------------------------------------------------[INFO] [INFO] >>> maven-archetype-plugin:3.0.1:generate (default-cli) > generate-sources @ standalone-pom >>>[INFO] [INFO] <<< maven-archetype-plugin:3.0.1:generate (default-cli) < generate-sources @ standalone-pom <<<[INFO] [INFO] [INFO] --- maven-archetype-plugin:3.0.1:generate (default-cli) @ standalone-pom ---[INFO] Generating project in Interactive mode[INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0)Choose archetype:1: remote -> org.apache.maven.archetypes:maven-archetype-quickstart (An archetype which contains a sample Maven project.)Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 1: 1Choose org.apache.maven.archetypes:maven-archetype-quickstart version: 1: 1.0-alpha-12: 1.0-alpha-23: 1.0-alpha-34: 1.0-alpha-45: 1.06: 1.1Choose a number: 6: 6Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/archetypes/maven-archetype-bundles/4/maven-archetype-bundles-4.pomDownloaded: https://repo.maven.apache.org/maven2/org/apache/maven/archetypes/maven-archetype-bundles/4/maven-archetype-bundles-4.pom (3.6 kB at 6.5 kB/s)Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/archetype/maven-archetype/2.0-alpha-5/maven-archetype-2.0-alpha-5.pomDownloaded: https://repo.maven.apache.org/maven2/org/apache/maven/archetype/maven-archetype/2.0-alpha-5/maven-archetype-2.0-alpha-5.pom (8.7 kB at 16 kB/s)Define value for property 'groupId': com.interviewbubbleDefine value for property 'artifactId': samplemavenprojectDefine value for property 'version' 1.0-SNAPSHOT: : 1.0 Define value for property 'package' com.interviewbubble: : Confirm properties configuration:groupId: com.interviewbubbleartifactId: samplemavenprojectversion: 1.0package: com.interviewbubble Y: : Y[INFO] ----------------------------------------------------------------------------[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.1[INFO] ----------------------------------------------------------------------------[INFO] Parameter: basedir, Value: /Users/admin/Desktop[INFO] Parameter: package, Value: com.interviewbubble[INFO] Parameter: groupId, Value: com.interviewbubble[INFO] Parameter: artifactId, Value: samplemavenproject[INFO] Parameter: packageName, Value: com.interviewbubble[INFO] Parameter: version, Value: 1.0[INFO] project created from Old (1.x) Archetype in dir: /Users/admin/Desktop/samplemavenproject[INFO] ------------------------------------------------------------------------[INFO] BUILD SUCCESS[INFO] ------------------------------------------------------------------------[INFO] Total time: 01:53 min[INFO] Finished at: 2018-04-02T23:21:44+05:30[INFO] Final Memory: 16M/54M[INFO] ------------------------------------------------------------------------username$


Getting Project Structure created by maven:
username:samplemavenproject$ cd samplemavenproject/username:samplemavenproject$ ls -ltotal 8-rw-r--r-- 1 username staff 768 Apr 2 23:21 pom.xmldrwxr-xr-x 4 username staff 128 Apr 2 23:21 src

Getting generated POM file:

username:samplemavenproject$ vi pom.xml

<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>com.interviewbubble</groupId> <artifactId>samplemavenproject</artifactId> <version>1.0</version> <packaging>jar</packaging><name>samplemavenproject</name> <url>http://maven.apache.org</url><properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties><dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies></project>

Getting Project structure
username:samplemavenproject$ tree.├── pom.xml└── src├── main│     └── java│           └── com│                └── interviewbubble│                             └── App.java└── test│     └── java│           └── com│                └── interviewbubble│                             └── AppTest.java9 directories, 3 filesusername:samplemavenproject$

Part 2: Getting started with Maven: First Maven Project Part 2