How to create an API generator
Tags: api, automation, maven, microservices
Published: 2020-11-20 19:48:36

Development teams are creating dozens of APIs per project since the arrival of microservices and we should try to make as easy as possible for them to create new ones, reduce repetitive tasks and standardise different patterns and configurations so they are applied the same way everywhere. 

Do you want to learn how to create a tool that allows to generate new APIs within seconds and achieve the above goals? If so we are going to show you how to do it with [Maven archetypes](https://maven.apache.org/archetype/maven-archetype-plugin/usage.html), a tool that allows to generate any type of project, even non-Java ones.


<u>**What can be automated?**</u> 
I guess your pipeline, Docker and configuration files are quite similar, they just change some Git urls and project name. 

_**What about the actual code?**_
I guess in most projects you do the same type of actions (create, read, modify and delete) and the main difference is the type (loan, mortgage, trade...) and they follow a similar structure (endpoints on the top layer, business logic in the services, queries on the repositories, and so on). 

_**What about the validations and tests?**_
I guess you have special ones focused on specific scenarios but there are also some generic ones that feel repetitive to implement them again. All of them can be automated so developers don't spend hours writing them again or copying and pasting from previous projects. They can spend their valuable time focused on writing specific business rules (such as ensuring shops cannot sell more products than what they have on stock) or building some amazing features none of your competitors has yet. 

**<u>How to do it?</u>**

There are a few options:
_**1) Create an API generation pipeline:**_ this is a great idea and can be done using e.g. Ansible scripts. The problem is that these scripts are usually managed by a centralised team like SRE that may not have capacity to generate projects that suit the needs of every team, not to mention that each team may use completely different technologies (Java vs Kotlin, Angular vs React, ... ).
_**2) Create your own Spring Initializr:**_ Pivotal have created a [tool](https://start.spring.io/) to create APIs and we can customise it, but it is focused on Spring Boot projects and we may want to provide generators for also other technologies.
_**3) Create your own archetypes:**_ Maven allows to create projects from a template providing some input parameters. We will show how to create one and use it.

<u>**Steps to create your first archetype**</u>:
**_1) Choose a good project to use as a reference._** You can modify it later but it adds extra work to re-implement one or change its structure, rename package paths, etc.
**_2) Create an archetype:_** you just have to run the command *mvn archetype:generate* and choose the base template you prefer.
**_3) Create a configuration file_** in which you specify which folders should be picked and which files inside them. You can find an example [here](https://github.com/rafaborrego/Microservices-example-Spring-Boot/blob/master/service-archetype/src/main/resources/META-INF/maven/archetype-metadata.xml).
**_4) Copy the code_** of your reference project inside the *archetype-resources* folder.
**_5) Replace the variable parts_** (like the entity names) by placeholders. The syntax for the folder names is *\_\_text-to-replace\_\_* and inside the code it is *${domain}*. You may want to do some smarter replacements what can be done using [Velocity templates](https://velocity.apache.org/).
**_6) Add it to your Maven repository_**, either to your local one (using *mvn install*) or to your remote one (using *mvn deploy*, ideally in a pipeline).

That's it. Now you will want to **create your own API using the archetype**. You just have to run a command in which you provide its basic details (Maven groupId, artifactId and version) and the values to replace on the placeholders, and it will be generated within seconds. 

**_An example would be:_**
*mvn archetype:generate -DarchetypeGroupId=com.mycompany -DarchetypeArtifactId=my-first-archetype -DarchetypeVersion=1.0.0-SNAPSHOT -entity=customer*

Do you want to see the full code of an archetype? You can find one [here](https://github.com/rafaborrego/Microservices-example-Spring-Boot/tree/master/service-archetype). 
Do you want to share other ways to automate your project creation or want to ask how to automate other tasks? If so we would love to read your comments!