Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code generation in Maven

I want to autogenerate some java classes from interfaces. My first thought was to write a code generator, and integrate it as a maven plugin.

I was thinking of creating a maven plugin with a codegen goal that is called during the build process.

So if I choose this route, how do I provide the plugin with the interfaces to be processed? And where should the generated files go?

Are there any existing plugins that can be configured to generate default class implementations?

like image 553
parkr Avatar asked Apr 19 '09 08:04

parkr


People also ask

What is Maven in coding?

Maven is a build automation tool used primarily for Java projects. Maven can also be used to build and manage projects written in C#, Ruby, Scala, and other languages. The Maven project is hosted by the Apache Software Foundation, where it was formerly part of the Jakarta Project.

What is Maven generate sources?

The “maven-source” plugin is used to pack your source code and deploy along with your project. This is extremely useful, for developers who use your deployed project and also want to attach your source code for debugging.

How does jOOQ generate code?

jOOQ's code generator takes your database schema and reverse-engineers it into a set of Java classes modelling tables, records, sequences, POJOs, DAOs, stored procedures, user-defined types and many more.

How do you generate a site in Maven?

To generate a site, just run mvn site:site or mvn site. To view the generated site on a local machine, run mvn site:run. This command will deploy the site to a Jetty web server at the address localhost:8080.


2 Answers

Sources should go in {project.build.directory}/generated-sources/[plugin-id]/

Most plugins take configuration passed through the plugin configuration section in the pom. You can use default values as well, or an annotation and classpath scanning.

A plugin like the maven-jspc-plugin generates code, which you could take a look at. The "Better Builds With Maven" e-book also contains a reasonably comprehensive chapter on writing plugins.

like image 98
krosenvold Avatar answered Nov 13 '22 13:11

krosenvold


Maybe have a look at the XDoclet Maven plugin- XDoclet is often used for generating sources from doclet-style markup in classes (e.g. autogenerating MBean interfaces from implementations) and that sounds similar to what you're doing.

like image 30
araqnid Avatar answered Nov 13 '22 12:11

araqnid