Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
February 4, 2021 11:19 am GMT

Making a Maven-plugin that annotates auto-generated files

I have recently begun experimenting with swagger, going for an API-first approach. I was annoyed that there was no Lombok for the auto generated model files. So I reckoned that was as good time as any for learning how to make Maven plugins.

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

The goal, Annotate auto generated target java files. Why? So
imported Java classes are more tailored to my use.

Target files are not meant to be edited post compilation, because then they would not be equal to those in the jar file. Therefore, we need to use the Maven-phase post-process, to process the java files, before they are compiled.

First I set up an archetype for a mojo plugin.
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.

We define that this is a plugin with the annotation @Mojo(name = "generate-annotations", defaultPhase = LifecyclePhase.PROCESS_SOURCES)

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

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

Here we have defined three parameters,

  • directory for which folder we want to edit the file in.
  • Imports, from where Java will import the annotations
  • Annotations, which annotations that are to be used.

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.

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

autoJPG

And that's how I did the things.


Original Link: https://dev.to/thomasdahll/making-a-maven-plugin-that-annotates-auto-generated-files-51bo

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To