Code generation in Maven
Asked Answered
A

3

16

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?

Ancier answered 19/4, 2009 at 8:15 Comment(0)
M
8

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.

Maladroit answered 19/4, 2009 at 20:55 Comment(3)
It's normal that they go into a subdirectory of generated-sources. I found that the NetBeans support assumes this.Isiahisiahi
More completely, they should go into {project.build.directory}/generated-sources/<plugin-id>Inapt
Thanks @Maladroit & @Inapt ! Can you please help share a link to the official maven documentation that mentions this aspect, i.e. that target/generated-sources is the standard mechanism not just for annotation processors but for any generated source code? I couldn't see that mentioned both here & here.Shylashylock
W
3

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.

Weissman answered 19/4, 2009 at 12:44 Comment(0)
F
1

I have used APT-Jelly to successfully generate java source code from annotated java. You may want to check it out.

Forgotten answered 15/9, 2011 at 7:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.