28 April 2011

Creating a new Maven Archetype

The simplest way to create a new archetype is to first start off with the code you wish the archetype to generate.  In our case, for this example, we will use:

malachi@onyx:~/work/archtest$ tree
.
|-- pom.xml
`-- src
    `-- main
        `-- java
            `-- org
                `-- eoti
                    `-- archtest
                        |-- Test1.java
                        `-- Test2.java

6 directories, 3 files
Nothing fancy here.  Standard standalone app.

Make sure it all compiles (mvn clean install).  Typos are much easier to fix at this stage.  If you want specific repositories or dependencies to be included by default in applications, make sure to include those in your pom now.

Once it has built successfully, let's build our initial archetype (mvn clean archetype:create-from-project).

If all went well, your directory tree should be quite a bit bigger now:
malachi@onyx:~/work/archtest/target/generated-sources/archetype$ tree
.
|-- pom.xml
|-- src
|   |-- main
|   |   `-- resources
|   |       |-- archetype-resources
|   |       |   |-- pom.xml
|   |       |   `-- src
|   |       |       `-- main
|   |       |           `-- java
|   |       |               |-- Test1.java
|   |       |               `-- Test2.java
|   |       `-- META-INF
|   |           `-- maven
|   |               `-- archetype-metadata.xml
|   `-- test
|       `-- resources
|           `-- projects
|               `-- basic
|                   |-- archetype.properties
|                   `-- goal.txt
`-- target
    |-- archtest-archetype-1.0-SNAPSHOT.jar
    |-- classes
    |   |-- archetype-resources
    |   |   |-- pom.xml
    |   |   `-- src
    |   |       `-- main
    |   |           `-- java
    |   |               |-- Test1.java
    |   |               `-- Test2.java
    |   `-- META-INF
    |       `-- maven
    |           `-- archetype-metadata.xml
    `-- test-classes
        `-- projects
            `-- basic
                |-- archetype.properties
                `-- goal.txt

24 directories, 14 files
Your initial project (though modified) is still buried in there:
malachi@onyx:~/work/archtest/target/generated-sources/archetype/src/main/resources/archetype-resources$ tree
.
|-- pom.xml
`-- src
    `-- main
        `-- java
            |-- Test1.java
            `-- Test2.java

3 directories, 3 files
Look through that subset of files.  You'll notice that the pom.xml has changed the groupId, artifactId and version to be variables.  You'll also notice that Test1.java and Test2.java are no longer listed under a specific package directory structure.  You will also notice that both .java files have a variable package declaration.

Let's go ahead and install our new archetype locally. Go into target/generated-sources/archetype and build it (mvn clean install).

Assuming all went well, let's give it a shot:

malachi@onyx:~/work$ mvn archetype:generate -DarchetypeGroupId=org.eoti.archtest -DarchetypeArtifactId=archtest-archetype -DarchetypeVersion=1.0-SNAPSHOT
It will prompt you to fill in some values:
Define value for property 'groupId': : org.eoti.test.archetypetest
Define value for property 'artifactId': : ArchetypeTest
Define value for property 'version':  1.0-SNAPSHOT: :
Define value for property 'package':  org.eoti.test.archetypetest: :
Confirm properties configuration:
groupId: org.eoti.test.archetypetest
artifactId: ArchetypeTest
version: 1.0-SNAPSHOT
package: org.eoti.test.archetypetest
 Y: :
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 23.650s
[INFO] Finished at: Thu Apr 28 12:07:37 PDT 2011
[INFO] Final Memory: 8M/245M
[INFO] ------------------------------------------------------------------------
malachi@onyx:~/work$ tree ArchetypeTest/
ArchetypeTest/
|-- pom.xml
`-- src
    `-- main
        `-- java
            `-- org
                `-- eoti
                    `-- test
                        `-- archetypetest
                            |-- Test1.java
                            `-- Test2.java

7 directories, 3 files

You will see that all your variables have been replaced with the values you were prompted to fill in.  You now have a working archetype =)

No comments:

Post a Comment