Maven, Microservices

Maven Archetypes

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

#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
package ${package}.app.config;
import ${package}.data.config.DataConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
public class AppConfig {

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.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.