We found a structure that works for our service.(eg clean architecture). That means we have a domain layer, a data access and application layer and of course our presentation layer which can be a rest endpoints layer. Now we want all our services to respect this structure and also we don’t want to manually copy each time we create a service an existing service structure and remove what we don’t need.
Maven archetypes can help us in achieving this more elegantly. Basically we create our first service and we can generate and archetype from it.
mvn archetype:create-from-project
With that archetype we can spawn services fast and easy. Without further ado let’s get to it.
There is a certain structure that each maven archetype must respect.
Archetype resources contains basically the structure that we want to use as a template. Here we can have classes, yml files, any resource we need. Though we need to respect the conventions. For example, if we take this file
we notice some placeholders: ${package},${groupId}. This allows us to parameterise things like packages and maven group ids.
The second folder that we are interested in is META-INF/maven where the archetype-metadata.xml is. Its purpose is to copy resources and setup required parameters. Here we filter all the java and yml files we need from the archetype-resources. These will eventually be copied in the final module and all the parameters will be filled with values.
For detailed instructions on usage please check out this repo.