Blog‎ > ‎

Deploying a Maven archetype

I've just wasted many hours trying to figure out how to deploy a Maven archetype to our Maven repository, so perhaps this can save people some time.

The documentation for creating archetypes is not great, but it's OK. I highly recommend starting from a project and converting it to an archetype, rather than creating one from scratch.

I found this helpful article to walk me through the steps. There are many more if you Google around.

Once you can successfully create your archetype, you'll install it (mvn install) to your local repository (~/.m2), and you can use it to generate your app. Great. Now how do you deploy this bad boy to your Maven repository?

This is where the documentation leaves you completely up the creek. First for the actual deployment. We use ant for our builds, so here is the target to create and deploy the archetype:

<target name="uploadArchetypeToMaven" depends="checkoutSource">

<path id="maven-ant-tasks.classpath" path="${ThirdPartyHome}/apache-maven/maven-ant-tasks-2.1.3.jar" />
<typedef resource="org/apache/maven/artifact/ant/antlib.xml"
uri="antlib:org.apache.maven.artifact.ant"
classpathref="maven-ant-tasks.classpath" />
<artifact:install-provider artifactId="wagon-ftp" version="1.0-beta-2"/>
<artifact:pom id="mypom" file="${demo.archetype.build.root}/pom.xml" />

<artifact:mvn pom="${demo.archetype.build.root}/pom.xml">
   <arg value="package"/>
</artifact:mvn>

<artifact:deploy file="${demo.archetype.build.root}/target/demo-archetype-${build.version}.jar">
   <remoteRepository url="ftp://1.2.3.4/">
     <authentication username="john.doe" password="secret"/>
   </remoteRepository>
   <pom refid="mypom"/>
</artifact:deploy>

<myftp server="${upload_server}" userid="john.doe" password="secret" 
remotedir="/">
<fileset dir="${demo.archetype.build.root}/build">
<include name="archetype-catalog.xml" />
</fileset>
</myftp>
</target>

A few things to notice:
  1. the package command will create your archetype in the target directory (no surprise here)
  2. the deploy command will then FTP it to your Maven repository
  3. the last step is critical and poorly undocumented: you need to have a file called archetype-catalog.xml that lists the archetypes in your repository. This file looks like this for us:

<?xml version="1.0" encoding="UTF-8"?>
<archetype-catalog xsi:schemaLocation="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-catalog/1.0.0 http://maven.apache.org/xsd/archetype-catalog-1.0.0.xsd"
    xmlns="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-catalog/1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <archetypes>
    <archetype>
      <groupId>com.autobizlogic.abl</groupId>
      <artifactId>demo-archetype</artifactId>
      <version>2.1.4</version>
      <repository>http://resources.automatedbusinesslogic.com/maven2</repository>
      <description>demo</description>
    </archetype>
  </archetypes>
</archetype-catalog>

It's supposed to be more or less automagically created for you, but I've found that taking control of it is the way to go (at least for us).

Once that's in place, you can instantiate your archetype with a command like this:

mvn archetype:generate \
    -DarchetypeGroupId=com.autobizlogic.abl \
    -DarchetypeArtifactId=demo-archetype \
    -DarchetypeVersion=2.1.4 \
    -DgroupId=com.acme \
    -DartifactId=demo \
    -Dversion=1.0-SNAPSHOT \
    -DarchetypeCatalog=http://resources.automatedbusinesslogic.com/maven2

I'd say that's all there is to it, but it took me an embarrassingly long time to figure all this out, so I hope this saves you from the same frustration.

Comments