Maven archetypes are project templates that allow users create project structure with a simple Maven command. In my company we are using archetypes because provide a way to standardize projects structure. All our projects are built using the same directory structure, all of us use the same version of common libraries like JUnit, Hamcrest, Spring Framework, Mockito, or in case of web applications bundling them with company's approved CSS and Javascript libraries. Also PMD, checkstyle or findbugs coding rules can be stored in distributed archetype.
If each time you start a new project you are of those who copy files from existing projects to the new one, apply DRY principle and create a Maven archetype from existing project.
First thing to do is create your template project with all files to be bundled into archetype. In this example, simple Spring MVC project will be transformed to be a Maven archetype.
After template project is created and all desired files are added, you should have a directory layout like:
My personal advice is that if you are thinking about distributing this archetype with community (not only for your company), remove all IDE specific files.
Now you have your project created and ready to be packaged as archetype. Execute next command on root of your project.
mvn archetype:create-from-project
And Maven console output should be:
And now your archetype is created in target/generated-sources/archetype directory with next hierarchy:
Now project is inside
archetype-resources directory. This directory contains all files that will be added in generated project.
At first sight, not much differences between original project and "template" project, it seems that only three files has been added
archetype-metadata.xml,
archetype.properties and
goal.txt, but shortly you will see that original project content has been modified too.
Before continuing see that in project exists two
poms, one
pom that is in root directory, that will be called
archetype pom, because it contains all
archetype configuration, and another one into
archetype-resources, called
template pom, because it will be the
pom used in generated project.
Next step is isolate
archetype project into separate folder, so can be dealt as alone project.
mv target/generated-sources/archetype ../spring-mvc-archetype
Following step is adding a name to generated
archetype, so open
archetype pom and change
<name> tag value to your
archetype name, for example
spring-mvc-archetype, and if you want
artifactId and
groupId too.
After this modification, open archetype-resources'
pom, and see how
<artifactId> or
<groupId> values are surrounded with
${artifactId} or
${groupId}. When you are creating a new
archetype, by default
Maven will ask you to enter four parameters,
groupId,
artifactId,
version and
package. Entered values will be used to fill placeholders.
With default four parameters should be enough, but imagine you want that user provides more information, for example,
war name. To get this, open
archetype-metadata.xml file (
src/main/resources/META-INF/maven) and add one required property, using
<requiredProperties> tag.
In previous file we are adding a new required property named warName. And last thing to do is update
archetype.properties located on test/resources/projects/basic with default value of new property.
And that's all, if you open any
Java class or any
Xml file, you will see that has been modified with
${package} variable. This information is filled when you generate the project
.
Now you can install
archetype into your local catalog and start generating standardized projects.
mvn clean install
And your artifact is ready to be used. Try next
command or if you have installed
m2Eclipse plugin open
Eclipse and try your new
archetype:
mvn archetype:generate -DarchetypeCatalog=local
A list of all installed
archetypes is shown. Choose previously created and fill up all required properties, and your new project is built and configured. You can start coding with same libraries that your workmates use and same style rules.
In this post a simple example has been provided, but think about all kind of elements that you copy and paste from one project to another like
SCM connection,
surefire plugin configuration,
release plugin tag name, to cite a few, and how you can integrate them into your
archetype.
I wish you have found this post interesting.
Music:
http://www.youtube.com/watch?v=AhzhiQA6-Aw&ob=av3e