Task 6: Create a Project using Archetypes
Java projects come in a number of common flavors: a simple command-line application, a web application for Tomcat, AppFuse applications and countless more. These applications often have the same folder structure, dependencies and build process. For a company with several of these types of applications the process of setting up the structure, dependencies and build process for these kinds of projects can become quite tedious. It also allows for inconsistency if this setup relies on a manual process by error-prone humans.
Maven removes the tedium and inconsistencies with a concept called Archetypes. Archetypes can be thought of as templates for common application types. They define where source code and resources are stored, the process to compile and test code, dependencies, plugins and any additional information and tasks you may need to perform. There is even an archetype to create your own archetype!
You can use the generate
goal in Maven's archetype
plugin to generate a new project from an archetype. You provide the archetype with your groupId, artifactId and version and it generates a POM file for you, along with its defined folder structure.
Creating a Maven Project with Archetype Interactively
prompt> mvn archetype:generate
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'archetype'.
...
374: remote -> circumflex-archetype (-)
375: remote -> javg-minimal-archetype (-)
Choose a number: 101: 101
Choose version:
...
5: 1.0
6: 1.1
Choose a number: 6: 6
Define value for property 'groupId': : com.example
Define value for property 'artifactId': : hello2
Define value for property 'version': 1.0-SNAPSHOT: 1.0-SNAPSHOT
Define value for property 'package': com.example:
Confirm properties configuration:
groupId: com.example
artifactId: hello2
version: 1.0-SNAPSHOT
package: com.example
Creating a Project using an Archetype Automatically
If you already know the archetype you want to use, you can provide all the information on the command-line:
prompt> $ mvn archetype:generate -DgroupId=com.example \
-DartifactId=hello3 -Dversion=1.0-SNAPSHOT \
-DarchetypeArtifactId=maven-archetype-quickstart \
-DinteractiveMode=false
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'archetype'.
...
[INFO] project created from Old (1.x) Archetype in dir: /Users/brian/work/projects/hello3