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

(Continued from page 1)

Let's examine the files in the project that were created for us. We can see that the project is essentially a collection of resources that will get placed in the destination project when the archetype is called. First off, notice that all the resources are under src/main/resources, which makes sense since this is the default place for resources.

'maven-archetype-example' structure

The project has a normal pom.xml at its root level, as would be expected.

maven-archetype-example/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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.myarchetype</groupId>
  <artifactId>maven-archetype-example</artifactId>
  <version>1.0-SNAPSHOT</version>
  <name>Archetype - maven-archetype-example</name>
  <url>http://maven.apache.org</url>
</project>

The archetype.xml file is a very important file. It basically specifies the files that will be included in a project created with this archetype. If the files aren't listed here, they won't be included in the generated project even if the files exist. This file can also specify things like <resources>.

src/main/resources/META-INF/maven/archetype.xml

<archetype>
  <id>maven-archetype-example</id>
  <sources>
    <source>src/main/java/App.java</source>
  </sources>
  <testSources>
    <source>src/test/java/AppTest.java</source>
  </testSources>
</archetype>

The archetype-resources directory contains the resources that will get included in the generated project with the help of the archetype.xml file. One file that is essential is the archetype-resources/pom.xml file. This file is shown here.

original src/main/resources/archetype-resources/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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>$com.myarchetype</groupId>
  <artifactId>$maven-archetype-example</artifactId>
  <version>$1.0-SNAPSHOT</version>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

Unfortunately, notice that there appears to be a bug (or I did something wrong). The groupId, artifactId, and version values are messed up. We'd like these to be correct and dynamic when a new project gets created, so I corrected these problems, as shown below.

modified src/main/resources/archetype-resources/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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>${groupId}</groupId>
  <artifactId>${artifactId}</artifactId>
  <version>${version}</version>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

I also needed to fix the groupId value for the package in App.java. The fixed version is shown here.

App.java

package ${groupId};

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
    }
}

The fixed AppTest.java file is shown here.

AppTest.java

package ${groupId};

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

/**
 * Unit test for simple App.
 */
public class AppTest 
    extends TestCase
{
    /**
     * Create the test case
     *
     * @param testName name of the test case
     */
    public AppTest( String testName )
    {
        super( testName );
    }

    /**
     * @return the suite of tests being tested
     */
    public static Test suite()
    {
        return new TestSuite( AppTest.class );
    }

    /**
     * Rigourous Test :-)
     */
    public void testApp()
    {
        assertTrue( true );
    }
}

Now that the "maven-archetype-example" project is ready, I'll install it into my local mavenSW repository so that I can use it. I'll do this via "mvn clean install".

Executing 'mvn clean install' on 'maven-archetype-example'

The console output is shown below.

Console output for 'mvn clean install' on 'maven-archetype-example' project

[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building Archetype - maven-archetype-example
[INFO]    task-segment: [clean, install]
[INFO] ------------------------------------------------------------------------
[INFO] [clean:clean]
[INFO] Deleting directory C:\dev\workspace\maven-archetype-example\target
[INFO] [resources:resources]
[INFO] Using default encoding to copy filtered resources.
[INFO] [compiler:compile]
[INFO] No sources to compile
[INFO] [resources:testResources]
[INFO] Using default encoding to copy filtered resources.
[INFO] [compiler:testCompile]
[INFO] No sources to compile
[INFO] [surefire:test]
[INFO] No tests to run.
[INFO] [jar:jar]
[INFO] Building jar: C:\dev\workspace\maven-archetype-example\target\maven-archetype-example-1.0-SNAPSHOT.jar
[INFO] [install:install]
[INFO] Installing C:\dev\workspace\maven-archetype-example\target\maven-archetype-example-1.0-SNAPSHOT.jar to \dev\m2repo\com\myarchetype\maven-archetype-example\1.0-SNAPSHOT\maven-archetype-example-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2 seconds
[INFO] Finished at: Wed Feb 20 16:19:56 PST 2008
[INFO] Final Memory: 7M/13M
[INFO] ------------------------------------------------------------------------

The "maven-archetype-example" archetype has now been installed in my local maven repository. In another tutorial, I'll show how we can now use this archetype to create a new project.

Page: < 1 2