We live in a micro-services world, lately, does not matter where you go, big, medium or small companies or start-ups, everyone is trying to implement microservices or migrating to them.
Maybe not initially, but when companies achieve a certain level of maturity, they start having a set of common practices, libraries or dependencies they apply or use in all the micro-services they build. Let’s say, for example, authentication or authorization libraries, metrics libraries, … or any other component they use.
When this level of maturity is achieved, usually, to start a project basically we take the “How-To” article in our wiki and start copying and pasting common code, configurations and creating a concrete structure in the new project. After that, it is all set to start implementing business logic.
This copy and paste process is not something that it usually takes a long time but, it is a bit tedious and prone to human errors. To make our lives easier and to try to avoid unnecessary mistakes we can use maven archetypes.
Taken from the maven website, an archetype is:
In short, Archetype is a Maven project templating toolkit. An archetype is defined as an original pattern or model from which all other things of the same kind are made. The name fits as we are trying to provide a system that provides a consistent means of generating Maven projects. Archetype will help authors create Maven project templates for users, and provides users with the means to generate parameterized versions of those project templates.
In the next two sections, we are going to learn how to build some basic archetypes and how to build a more complex one.
Creating a basic archetype
Following the maven documentation page we can see there are a few ways to create our archetype:
From scratch
I am not going to go into details here because the maven documentation is good enough and because it is the method we are going to use in the “Creating a complex archetype” section below. You just need to follow the four steps the documentation is showing:
- Create a new project and pom.xml for the archetype artefact.
- Create the archetype descriptor.
- Create the prototype files and the prototype pom.xml.
- Install the archetype and run the archetype plugin
Generating our archetype
This is a very simple one also described in the maven documentation. Basically, you use maven to generate the archetype structure for you
mvn archetype:generate \
-DgroupId=[your project's group id] \
-DartifactId=[your project's artifact id] \
-DarchetypeGroupId=org.apache.maven.archetypes \
-DarchetypeArtifactId=maven-archetype-archetype
As simple as that. After executing the command, we can add our personalisations to the project and proceed to install it as seen before.
From an existing project
This option allows us to create a project and when we are happy with how it is, to transform the project into an archetype. Basically we need to follow the next steps:
- Build the project layout by scratch and add files as need.
- Run the Maven archetype plugin on an existing project and configure from there.
mvn archetype:create-from-project
This will generate an “archetype” folder into the “target” folder:
target/generated-sources/archetype
We just need to copy this folder structure to the desired location and we will have our archetype ready to go. It needs to be installed as usual to be able to use it.
Using our archetype
Once we have install our archetype, we can start using it:
mvn archetype:generate \
-DarchetypeGroupId=dev.binarycoders \
-DarchetypeArtifactId=simple-archetype \
-DarchetypeVersion=1.0-SNAPSHOT \
-DgroupId=org.example \
-DartifactId=project1
This will create a new project using the archetype. The information we need to modify in the previous command is:
- archetypeGroupId: It is the archetype group id we have defined when we created the archetype.
- archetypeArtifactId: It is basically the name of our archetype.
- archetypeVersion: It is the version of the archetype we want to use in case the archetype has been evolving over time and we have different versions.
- groupId: It is the group id our new project is going to have.
- artifactId: It is the name of our new project.
Deleting our archetype
Right now, after installing our archetype, it is only available in our local repository. This fact allows us to delete the archetype in a very simple way. We just need to take a look at the archetype catalogue in our repository and manually remove the archetype. We can find this file at:
~/.m2/repository/archetype-catalog.xml
Creating a complex archetype
For most cases, the already reviewed ways to create archetypes should be enough but, not for all of them. What happens if we need to define some modules we want to define the name when creating the project? Or classes? Or some other customisations?
Luckily, Maven gives us some level of flexibility allowing us to define some variables and use some concrete patterns to define folders and files in our archetypes in a way they will be replaced when the projects using the archetype are created.
As a general rule we will be using two kinds of notation for our dynamically elements:
- Defined in files: ${varName}
- Defined in file system: __varName__ (two underscores)
This will help us to achieve our goals.
As an example, I am going to create a small complex archetype to be able to see this in action. The projects created with the archetype are going to have:
- A parent project with <artifactId> name.
- Two modules called <artifactId>-one and artifactId-two.
- A main class called <classPrefix>OneApp and <classPrefix>TwoApp respectibely.
- The classes will be located in the package <package>.one and <package>.two respectibely.
- The module One will have a properties class stored in the resources folder.
The code of the archetype can be found at the GitHub repository.
The first file we can check is archetype-metadata-xml located in META-INF/maven.
We can see here the definition of the variable classPrefix and groupId with a default value assigned.
<requiredProperties>
<requiredProperty key="classPrefix" />
<requiredProperty key="groupId">
<defaultValue>dev.binarycoders</defaultValue>
</requiredProperty>
</requiredProperties>
After that, we can see the definition of the project structure we want to achieve. In this case, we have the fileSets node with the files on the parent project and, after that, the definition of the modules we want to include. Here we should pay special attention to the way the module attributes are defined:
<module id="${rootArtifactId}-one"
dir="__rootArtifactId__-one"
name="${rootArtifactId}-one">
As we can see they use the notation described before, using the “${}” notation for variables in files and the notation “__” (two underscores) for file system elements. The rest of the file is pretty simple.
If we explore the folder structure, we can see a few elements defined with these two underscores notation like the module names and the class names. This will be dynamic elements that will take the name from the variable defined when the project is created.
We can define different filesets for the files we want to be copied to our generated project. For example, we can copy all the .java files we can find inside the path src/main/java:
<fileSet filtered="true" packaged="true" encoding="UTF-8">
<directory>src/main/java</directory>
<includes>
<include>**/*.java</include>
</includes>
</fileSet>
Finally, if we explore one of the classes, we can see the next content:
#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $smbol_escape = '\' )
package ${package}.one;
public class ${classPrefix}OneApp {
}
The first three lines are just alias to be able to use the symbols that have a specific meaning not just as literals.
After that, we can see the package definition that it is going to be built with one part dynamically added and one part statically defined. We can see the class name follows the same pattern.
Deserves special attention to the fact that, despite we are defining packages into the classes, we are not replicating this structure in the project structure, Maven will take care of that for us. This is because when we defined the fileset we defined the attribute package equals true. If this attribute is set to false, we will be in charge of defining the desired structure.
It is worth it to mention that because of the files in the maven archetype act as velocity templates, we can introduce some logic and some dynamic content in our files. For example, print something or not in a determinate file:
<requiredProperty key="greeting">
<defaultValue>y</defaultValue>
</requiredProperty>
#if (${greeting == 'y'})
// Hello, welcome here!
#end
This variable can be set using the command line when we generate our new project:
-Dgreeting=n
Finally, there is one more interesting thing we can do. We can use a post-generation script write in groovy to execute some actions after the project has been generated. One interesting use, it is to remove not desired files based on some variables defined when generating the project. This script will be located in the folder src/main/resources/META-INF with the name archetype-post-generate.groovy.
import groovy.io.FileType
def rootDir = new File(request.getOutputDirectory() + "/"
+ request.getArtifactId())
def oneBundle = new File(rootDir, request.getArtifactId()
+ "-one")
def projectPackage = request.getProperties().get("package")
assert new File(oneBundle, "src/main/java/"
+ projectPackage.split("\\.").join('/')
+ "/toDelete.txt").delete()
With this, every time that we use the archetype to create a new project we will obtain the desired results.
We can use our recently created archetype with:
mvn archetype:generate \
-DarchetypeGroupId=dev.binarycoders \
-DarchetypeArtifactId=simple-archetype \
-DarchetypeVersion=1.0-SNAPSHOT \
-DgroupId=org.example \
-DartifactId=project

And the result:

And, one of the classes:

This is all. I hope is useful.
See you.