8

Maven Archetypes

 3 years ago
source link: https://sergiuoltean.com/2019/01/23/maven-archetypes/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Maven Archetypes – Learn. Write. Repeat.Skip to content

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.

photo-1532619187608-e5375cab36aa?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1050&q=80

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.

screenshot-2019-01-22-at-20.17.54.png

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.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK