Getting started with the Maven archetype

Creating new Java projects from scratch can be a bit tortuous as you have to
provide the right component, some initial code and some way of building
it. Using Maven these problems can all be resolved so that starting a new
project is exceptionally easy process. Specifically it will create a new
set of directories and files, add in the build definitions and ensure all
the dependent resources are available. This is the work of the Maven
Archetype plugin.

Naked Objects uses the feature of Maven to create new Naked Object
application in a couple of commands. What I am going to describe now is
the same as for other Maven archetype, the only additional task is to
specify where the archetype is to be found. We maintain that catalog on
our website so to build a application from scratch type in the following
(assuming that you have Maven 2.1 or 2.2 installed, see
http://maven.apache.org/download.html).

$ mvn archetype:generate -DarchetypeCatalog=http://nakedobjects.org

You will then be prompted with a list of archetypes (at time of
writing there is only one).

Choose archetype:
1: http://nakedobjects.org -> application-archetype (Naked Objects Application)
Choose a number:  (1):

Enter ‘1′ and press return. Once prompted enter a group ID, an
artifact ID, a version number and a suitable package name. The group and
artifact IDs combine to form a unique name for you project and typically
the group ID is used across all you different projects – a suitable choice
here is your domain name reversed, e.g., com.mycompany. The version and
packages prompts provide suitable defaults and can be left as such unless
you have good reason to change them.

After entering these details you are asked to confirm, so ‘ y’ must
be entered. The following example shows me setting up a test project to
explore an idea. I have used org.test as the group ID so I do not clutter
up my project space, and I can more simply and safely remove all my test
stuff from the Maven repsository by deleting the org/test directory.

Define value for groupId: : org.test
Define value for artifactId: : expenses
Define value for version:  1.0-SNAPSHOT: :
Define value for package:  org.test: :
Confirm properties configuration:
groupId: org.test
artifactId: expenses
version: 1.0-SNAPSHOT
package: org.test
 Y: : y

I now have directory called expenses and that contains the
following:-

expenses/
|-- commandline
|   |-- config
|   |-- ide
|   |-- pom.xml
|   `-- src
|-- dom
|   |-- pom.xml
|   `-- src
|-- fixture
|   |-- pom.xml
|   `-- src
|-- pom.xml
|-- service
|   |-- pom.xml
|   `-- src
`-- webapp
    |-- ide
    |-- pom.xml
    `-- srcconfig

This is our recommended directory structure for projects so that
they separate out the different parts of the application. Its probably too
complex for simple projects, but it is the right thing for larger ones.

With the project created the first thing to do is build it and
confirm it runs.

$ cd expenses
$ mvn package

This compile the source code, runs any tests and packages up the
results in the typical Maven fashion. If you go to the target directory in
the commandline directory you will find a distribution file (ending in
.zip) and a directory of the same name with the contents expanded into it.
If you go into that directory you can run the application, so following on
this example:

$ cd commandline/target/expenses-commandline-1.0-SNAPSHOT-commandline.dir/expenses-commandline-1.0-SNAPSHOT/

you will see the following config

expenses.jar
images
lib
nakedobjects.bat
nakedobjects.sh

Run the application using the script

$ ./nakedobjects.sh

Login with sven/pass and ensure that you have a single class
available to you. That’s all there is to it, all the work is done by
Maven!

No Comment

No comments yet

Leave a reply