How do I create an archetype?
Author: Deron Eriksson
Description: This maven tutorial describes how to create an archetype to create projects using maven-archetype-archetype.
Tutorial created using: Windows Vista || JDK 1.6.0_04 || Eclipse Web Tools Platform 2.0.1 (Eclipse 3.3.1)

Page:    1 2 >

A mavenSW archetype allows us to create a basic type of maven project that contains a certain structure with certain files. It is possible to create our own archetype. A quick way to start creating an archetype is to use the maven-archetype-archetype.

Here is an EclipseSW external tool configuration to create an archetype project. The maven_exec variable is a string substitution variable that points to my maven mvn.bat file.

Name:mvn archetype~create archetype
Working Directory:${workspace_loc}
Arguments:archetype:create -DgroupId=${string_prompt:groupId} -DartifactId=${string_prompt:artifactId} -DarchetypeArtifactId=maven-archetype-archetype

The external tool configuration is shown here.

External Tool Configuration

When running the external tool configuration, first we get prompted for the groupId. I enter "com.myarchetype".

Entering groupId

Next we're prompted for the artifactId. I enter "maven-archetype-example".

Entering artifactId

The console output from the external tool configuration to create the archetype project is shown here.

Console output for 'mvn archetype:create -DgroupId=com.myarchetype -DartifactId=maven-archetype-example -DarchetypeArtifactId=maven-archetype-archetype'

[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'archetype'.
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Default Project
[INFO]    task-segment: [archetype:create] (aggregator-style)
[INFO] ------------------------------------------------------------------------
[INFO] Setting property: classpath.resource.loader.class => 'org.codehaus.plexus.velocity.ContextClassLoaderResourceLoader'.
[INFO] Setting property: velocimacro.messages.on => 'false'.
[INFO] Setting property: resource.loader => 'classpath'.
[INFO] Setting property: resource.manager.logwhenfound => 'false'.
[INFO] ************************************************************** 
[INFO] Starting Jakarta Velocity v1.4
[INFO] RuntimeInstance initializing.
[INFO] Default Properties File: org\apache\velocity\runtime\defaults\
[INFO] Default ResourceManager initializing. (class org.apache.velocity.runtime.resource.ResourceManagerImpl)
[INFO] Resource Loader Instantiated: org.codehaus.plexus.velocity.ContextClassLoaderResourceLoader
[INFO] ClasspathResourceLoader : initialization starting.
[INFO] ClasspathResourceLoader : initialization complete.
[INFO] ResourceCache : initialized. (class org.apache.velocity.runtime.resource.ResourceCacheImpl)
[INFO] Default ResourceManager initialization complete.
[INFO] Loaded System Directive: org.apache.velocity.runtime.directive.Literal
[INFO] Loaded System Directive: org.apache.velocity.runtime.directive.Macro
[INFO] Loaded System Directive: org.apache.velocity.runtime.directive.Parse
[INFO] Loaded System Directive: org.apache.velocity.runtime.directive.Include
[INFO] Loaded System Directive: org.apache.velocity.runtime.directive.Foreach
[INFO] Created: 20 parsers.
[INFO] Velocimacro : initialization starting.
[INFO] Velocimacro : adding VMs from VM library template : VM_global_library.vm
[ERROR] ResourceManager : unable to find resource 'VM_global_library.vm' in any resource loader.
[INFO] Velocimacro : error using  VM library template VM_global_library.vm : org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource 'VM_global_library.vm'
[INFO] Velocimacro :  VM library template macro registration complete.
[INFO] Velocimacro : allowInline = true : VMs can be defined inline in templates
[INFO] Velocimacro : allowInlineToOverride = false : VMs defined inline may NOT replace previous VM definitions
[INFO] Velocimacro : allowInlineLocal = false : VMs defined inline will be  global in scope if allowed.
[INFO] Velocimacro : initialization complete.
[INFO] Velocity successfully started.
[INFO] [archetype:create]
[INFO] Defaulting package to group ID: com.myarchetype
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating Archetype: maven-archetype-archetype:RELEASE
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.myarchetype
[INFO] Parameter: packageName, Value: com.myarchetype
[INFO] Parameter: package, Value: com.myarchetype
[INFO] Parameter: artifactId, Value: maven-archetype-example
[INFO] Parameter: basedir, Value: C:\dev\workspace
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] ********************* End of debug info from resources from generated POM ***********************
[INFO] Archetype created in dir: C:\dev\workspace\maven-archetype-example
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1 second
[INFO] Finished at: Wed Feb 20 15:27:38 PST 2008
[INFO] Final Memory: 4M/9M
[INFO] ------------------------------------------------------------------------

The archetype project has been created in my Eclipse workspace directory. Now it needs to be imported into Eclipse. To do this, in my Navigator view I select New → Project.

New Project

I select JavaSW Project and click Next.

Java Project

I locate my "maven-archetype-example" project. I click Finish.

'maven-archetype-example' project

The "maven-archetype-example" project now shows up in Eclipse.

'maven-archetype-example' in Eclipse Navigator View

I perform a "mvn eclipse:eclipse" on the project so that the project will work correctly in Eclipse and maven. This does things such as sets the .classpath to contain the correct information.

Executing 'mvn eclipse:eclipse' on 'maven-archetype-example'

The console output of this command is shown here:

Console output for 'mvn eclipse:eclipse' on 'maven-archetype-example' project

[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'eclipse'.
[INFO] ------------------------------------------------------------------------
[INFO] Building Archetype - maven-archetype-example
[INFO]    task-segment: [eclipse:eclipse]
[INFO] ------------------------------------------------------------------------
[INFO] Preparing eclipse:eclipse
[INFO] No goals needed for project - skipping
[INFO] [eclipse:eclipse]
[INFO] Using source status cache: C:\dev\workspace\maven-archetype-example\target\
[INFO] Not writing settings - defaults suffice
[INFO] File C:\dev\workspace\maven-archetype-example\.project already exists.
       Additional settings will be preserved, run mvn eclipse:clean if you want old settings to be removed.
[INFO] Wrote Eclipse project for "maven-archetype-example" to C:\dev\workspace\maven-archetype-example.
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1 second
[INFO] Finished at: Wed Feb 20 15:36:57 PST 2008
[INFO] Final Memory: 3M/8M
[INFO] ------------------------------------------------------------------------

The target/classes directory has been created for the class build contents. I deleted the bin directory since it is not needed.

'maven-archetype-example' project after housekeeping

(Continued on page 2)

Page:    1 2 >