Tuesday, March 13, 2012

Maven - Archetype

Archetypes are at the heart of Maven projects.

So what exactly is an archetype?

An archetype is a convention which specifies how the project directory structure should be, what dependencies the project will have, how the configuration files will be setup, what the build will generate, etc.

The creator of the archetype thinks these things through to create an optimum project configuration. The user of the archetype understands the high level goals and usage, and usually doesn't need to worry about the details.

Something like the manufacturer of a car vs the owner of a car.

Let us create a Maven project using the standard "JAR" archetype. This archetype creates a project for building jars. It comes nicely integrated with junit test framework, so you can write and run junit tests with no additional configuration.

Open a command prompt and cd to your workspace-confucius directory.

Run the command:
> mvn archetype:generate

 C:\Users\mlavannis\workspace-confucius>mvn archetype:generate  
[INFO] Scanning for projects...
[INFO]

The first time you run this command, Maven will scan its central repository to find all available archetypes and download their specifications. It may take a few minutes (be patient) the first time, but later it will be very fast.

At the end of the process, Maven will list several hundred(!) available archetypes, and ask you to select one. By default, it should point to the following archetype:

remote -> org.apache.maven.archetypes:maven-archetype-quickstart (An archetype which contains a sample Maven project.)

If so, hit enter.
Otherwise, specify the number of the above archetype and hit enter.

Next, it will ask you a series of options.

If it asks you to select a version of the archetype, select the latest one.

For the groupId, specify: org.confucius

For the artifactId, specify: HelloWorldJARArch

For the version, use the default recommended by Maven.

For the package, specify: org.confucius (which should be the default recommended by Maven).

Maven will now ask you to confirm your options, and will then create a new project.

 Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 171:  
Choose org.apache.maven.archetypes:maven-archetype-quickstart version:
1: 1.0-alpha-1
2: 1.0-alpha-2
3: 1.0-alpha-3
4: 1.0-alpha-4
5: 1.0
6: 1.1
Choose a number: 6:
Define value for property 'groupId': : org.confucius
Define value for property 'artifactId': : HelloWorldJARArch
Define value for property 'version': 1.0-SNAPSHOT: :
Define value for property 'package': org.confucius: :
Confirm properties configuration:
groupId: org.confucius
artifactId: HelloWorldJARArch
version: 1.0-SNAPSHOT
package: org.confucius
Y: :
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.1
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: org.confucius
[INFO] Parameter: packageName, Value: org.confucius
[INFO] Parameter: package, Value: org.confucius
[INFO] Parameter: artifactId, Value: HelloWorldJARArch
[INFO] Parameter: basedir, Value: C:\Users\mlavannis\workspace-confucius
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: C:\Users\mlavannis\workspace-confucius\HelloWorldJARArch
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 24.833s
[INFO] Finished at: Tue Mar 13 20:40:29 EDT 2012
[INFO] Final Memory: 7M/13M
[INFO] ------------------------------------------------------------------------


The project is created in its own directory: HelloWorldJARArch

cd to this directory.

You will see a /src folder and a pom.xml

The pom.xml is the master configuration for this project (Project Object Model).
The /src folder contains all the java source classes that will get built into a jar.
The /src folder also contains all the junit tests.

Congratulations - you have created your first Maven project.
We will learn more about this project in the coming posts.