Using Maven Archetype to Create New Applications Based on a Reference Architecture

Bruno Alves
Jan 10 · 5 min read
Image for post
Image for post

Recently, I was involved in a project composed by several Java applications. All of these applications followed a layered architecture pattern specified in an architecture reference, previously discussed and documented by software architects.

However, each time I had to create a new project based on that architecture reference I had a lot of difficulties because these Java applications still use the old web.xml and a long and complex Spring application context xml file with a lot of common beans and properties that had to be changed for each new application.

Therefore, in order to have a standard between all of these applications and to facilitate the start of a new project I decided to build an archetype.

What is an archetype? An archetype is defined as an original pattern or model from which all other things of the same kind are made[1].

Using a Maven Archetype project I found a great way to enforce same code base, quality attributes, packages organization to all projects.

So let’s start! 🚀

Application architecture

For this article, I chose an application layered architecture but of course other patterns can be considered.

Thus, in the next diagram I provide the component view of our architecture reference and on which I will base the Maven Archetype project.

Image for post
Image for post

Building the Archetype

Step 1: First of all we must create a simple Maven project with a pom.xml file on which packaging is maven-archetype.

Under the build section we also must import the archetype-packaging extension:

Step 2: Now we must place under src/main/resources/META-INF/maven directory a XML file named archetype-metadata.xml. This file is the Archetype descriptor and describe archetypes’s metadata that must be taken into account when generating the project.

Within archetype descriptor, we can define required properties that can be passed by argument. For example, in the next block, we define the name and version of the service.

As we are using a multi-module project we have to define which modules we want to be considered. Following the component diagram presented above, we should have the following four modules.

Note that we are already making use of the required properties defined here as a placeholder.

Step 3: We will have to return to the archetype descriptor file later but for now we can already place within src/main/resources/archetype-resource directory the template folders and files we want to be considered.

Image for post
Image for post

Note that if we inspect pom.xml file we can verify that we can use the placeholders previously defined in archetype descriptor.

Image for post
Image for post

Therefore, using the placeholders it’s easy to configure the dependencies between the modules that will be generated but also the common dependencies and properties to the entire project.

So, as you can see now it’s very easy to enforce a package organization, just create the directory structure and put the pretended classes or a .empty file (if the folder is going to be empty).

Image for post
Image for post

Step 4: After creating all directory structure now we have to return to the archetype descriptor file. If you payed attention before I put a comment with “…” between each module tag.

Thus, we have to indicate for each module and from a base directory the files we want to include, make the filtered property true if you want to filter files by extension or name as in the example.

Note that we make packaged property true or false as we want the filtered files to be placed inside a package or not. In the first set of files, this make sense for java classes but in the second, we just want to put the files inside resources folder.

Final step: Now we just have to compile the whole project and after that run on the terminal the following command.

Don’t forget to change the <SERVICE_NAME> placeholder in the command above for the name of the desired service.

After that, if you open the generated project you will have something like this:

Image for post
Image for post

Conclusion

I hope this article will help other developers start new projects more quickly, skipping much of the initial setup and maintaining a standard across the applications.

The project presented can be found on Github.

References

The Startup

Medium's largest active publication, followed by +773K people. Follow to join our community.

Bruno Alves

Written by

I am software engineer at KuantoKusta and student in the master of software engineering. I have experience developing backend applications in Java and Node.

The Startup

Medium's largest active publication, followed by +773K people. Follow to join our community.

Bruno Alves

Written by

I am software engineer at KuantoKusta and student in the master of software engineering. I have experience developing backend applications in Java and Node.

The Startup

Medium's largest active publication, followed by +773K people. Follow to join our community.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store