Code generation in Maven

試著忘記壹切 提交于 2019-12-21 04:07:56

问题


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?


回答1:


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.




回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/765060/code-generation-in-maven

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!