As many that are using maven 2 since years, I have many mavenized projects and the most of them have similar structure: are web projects, uses jetty and other plugins, many shares a lot of dependencies. So I decided to create an archetype to quickly generate new projects. I also wanted to use the mvn archetype generate command, so I prepared a archetypes catalog in my repository to allow me to use my personal archetypes with automatic generation.
As a start, I took an existing mvn archetype source code. Yes, maybe there are a good tutorial but what is better than an existing simple projects to modify? I am the lazy dev after all. I choose wicketrad-archetype, for the only reason I already had the code on my computer. There are just two things to do:- Change group and artifact id in the archetype pom (the one in the root folder) with what you like i called mines net.ildella.archetypes.base and net.ildella.archetypes.pheadra.
- Modify the pom.xml in src/main/resources/archetype-resources. This will be the one that will be used as basis for generated projects. Put in there whatever you like: plugins with configurations, dependencies, some comment as tip or reminder should also be useful.
Then a simple "mvn install" will install archetype artifact in local repository.
Let's now go for the Catalogue: create a file called archetype-catalog.xml with a content like this one: <pre><?xml version="1.0" encoding="UTF-8"?>
<archetype-catalog>
<archetypes>
<archetype>
<groupId>net.ildella.archetypes</groupId>
<artifactId>base</artifactId>
<version>1.0</version>
<repository>http://www.mvnsearch.org/maven2</repository>
<description>Basic maven projects with useful stuff configured</description>
</archetype>
<archetype>
<groupId>net.ildella.archetypes</groupId>
<artifactId>phaedra</artifactId>
<version>1.0</version>
<repository>http://www.mvnsearch.org/maven2</repository>
<description>Basic + pheadra dependencies and stuff</description>
</archetype>
</archetypes>
</archetype-catalog>
</pre> and put it in your ~/.m2/repository folder, or wherever you keep your repository.
That's all. Now we can do: <pre>
mvn archetype:generate -DarchetypeCatalog=file:///home/ildella/.m2/repository/
</pre> to get the following result: we choose a number and we got our skeleton project. If like me you started from wicketrad-archetype, have a look at file src/main/resources/META-INF/maven/archetype.xml to see how to include resources to your skeleton project, for example to add some demonstration classes.

0 Responses to “How to create maven archetype and a Catalog for project automatic generation”