Making a Maven-plugin that annotates auto-generated files
Tags: java, maven
Published: 2021-02-04 11:19:45

Recently, I began experimenting with Swagger to adopt an API-first approach. However, I was dissatisfied with the absence of Lombok for the auto-generated model files, and decided to learn how to create Maven plugins to address this issue. In this blog, I will share my experience creating a Maven plugin to add annotations to auto-generated files. This solution provides a more tailored approach to using imported Java classes.

To achieve the goal of annotating the auto-generated target Java files, I opted to tailor imported Java classes to my specific use case. It is essential to note that target files are not meant to be modified post-compilation, as they will differ from those in the jar file. Therefore, we must use the Maven-phase post-process to process the Java files before they are compiled.

[Maven: introduction-to-the-lifecycle](https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#:~:text=There%20are%20three%20built%2Din,of%20your%20project's%20site%20documentation.)

To create the Maven plugin, I set up an archetype for a mojo plugin using the maven-plugin-annotations dependency. By using this approach, defining the parameters to configure the new plugin becomes easy.

Baeldung provides some excellent guides on creating plugins with Maven. These resources helped me gain a good understanding of how to create a plugin and how to use annotations in Java. You can access these resources here:

https://www.baeldung.com/spring-boot-rest-client-swagger-codegen
https://www.baeldung.com/maven-plugin

We defined that this is a plugin with the annotation @Mojo(name = "generate-annotations", defaultPhase = LifecyclePhase.PROCESS_SOURCES), and that we will be modifying files created in the generated-source phase.

To customize the plugin, we have defined three parameters: directory for the folder in which we want to edit the file, imports for the source of Java imports, and annotations to specify which annotations should be used. These parameters are used to determine how the files should be written.

To use this plugin in another project, we can update that project's plugin tag, add the configurations, and metadata for the plugin. After running a Maven clean install, we obtain the desired annotations on the auto-generated files.

Here is the pom.xml file with the plugin definition:
{% gist https://gist.github.com/Dill-Dall/b9bbd165ea4ca70639ef4ffef0c80a45 file=pom.xml %}

Here is the JavaFileAnnotationMojo.java file with the Mojo plugin definition:
https://maven.apache.org/developers/mojo-api-specification.html
By using the maven-plugin-annotations dependency, the definition of the parameters to use for configuring the new plugin are easy to set up.

{% gist https://gist.github.com/Dill-Dall/b9bbd165ea4ca70639ef4ffef0c80a45 file=JavaFileAnnotationMojo.java %}

We change files that were created in the generated-source phase. So we use the next one process-source phase.

These values are used to determine how the files are to be written.

To use this plugin in another project. We can update that project plugins tag, and add the configurations and metadata for the plugin:

{% gist https://gist.github.com/Dill-Dall/b9bbd165ea4ca70639ef4ffef0c80a45 file=usage.xml %}

Maven clean install, and we get the annotations that we wanted on the auto generated files.

![autoJPG](https://dev-to-uploads.s3.amazonaws.com/i/iuk2ca9bwru1qrgov9uf.JPG)

Creating a Maven plugin to add annotations to auto-generated files is an excellent way to provide a more tailored approach to using imported Java classes. Baeldung provides some great resources on creating plugins with Maven, which I found helpful. I hope this article provides a useful starting point for anyone looking to create their Maven plugins.

https://gist.github.com/Dill-Dall/b9bbd165ea4ca70639ef4ffef0c80a45