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;
@Configuration
@ComponentScan("${groupId}")
@Import(DataConfig.class)
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.

Advertisements

Leave a Reply

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

WordPress.com Logo

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

Google+ photo

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

Twitter picture

You are commenting using your Twitter 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.