[英]Modify java classes to include specific annotations at compile time
I have many classes generated by JAXB's xsd2java. 我有许多由JAXB的xsd2java生成的类。 I need all these classes to be annotated with specific annotations at compile time (for example with lombok annotations). 我需要在编译时使用特定注释对所有这些类进行注释(例如使用lombok注释)。 Is there any way to do this, with some code generation tool for example? 有没有办法做到这一点,例如一些代码生成工具?
Disclaimer: I am the author of JAXB2 Annotate Plugin which allows you adding arbitrary annotations to the schema-derived classes. 免责声明:我是JAXB2 Annotate Plugin的作者,它允许您向模式派生类添加任意注释。
Short example: 简短的例子:
<xsd:complexType name="FooType">
<xsd:annotation>
<xsd:appinfo>
<annox:annotate>@java.lang.SuppressWarnings({"unchecked","rawtypes"})</annox:annotate>
<annox:annotate target="package">@javax.annotation.Generated({"XJC","JAXB2 Annotate Plugin"})</annox:annotate>
</xsd:appinfo>
</xsd:annotation>
<xsd:sequence>
<xsd:element name="bar" type="xsd:string"/>
<xsd:element name="foobar" type="xsd:string">
<xsd:annotation>
<xsd:appinfo>
<annox:annotate>@java.lang.SuppressWarnings({"unchecked","rawtypes"})</annox:annotate>
<annox:annotate target="setter">@java.lang.Deprecated</annox:annotate>
<annox:annotate target="setter-parameter">@java.lang.Deprecated</annox:annotate>
<annox:annotate target="getter">@java.lang.Deprecated</annox:annotate>
<annox:annotate target="field">@java.lang.Deprecated</annox:annotate>
<annox:annotate target="class">@java.lang.Deprecated</annox:annotate>
</xsd:appinfo>
</xsd:annotation>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
Works in external binding files as well. 也适用于外部绑定文件。
Limitations: 限制:
ps. PS。 I assume that when you said xsd2java
you probably meant XJC . 我假设当你说xsd2java
你可能意味着XJC 。
Update 更新
The OP asked about in comments how to configure it with the jaxb2-maven-plugin . OP在评论中询问如何使用jaxb2-maven-plugin配置它。
You can use jaxb2-annotate-plugin
with jaxb2-maven-plugin as well. 您也可以将jaxb2-annotate-plugin
与jaxb2-maven-plugin一起使用 。 I just have never tried it. 我从来没有尝试过。
dependencies/depenency
in pom.xml
. 您可以使用pom.xml
的dependencies/depenency
将其他JAR包含到类路径中。 arguments
into the configuration. 然后,您需要在配置中添加arguments
。 See this answer (and question) for examples: 有关示例,请参阅此答案(和问题):
https://stackoverflow.com/a/12804497/303810 https://stackoverflow.com/a/12804497/303810
It is about other plugins but you'll get a clue on how to configure it with Codehaus jaxb2-maven-plugin . 它是关于其他插件,但你会得到一个如何使用Codehaus jaxb2-maven-plugin配置它的线索。
Configuration with my maven-jaxb2-plugin would be as follows: 使用我的maven-jaxb2-plugin进行配置如下:
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<configuration>
<extension>true</extension>
<args>
<arg>-Xannotate</arg>
</args>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics-annotate</artifactId>
</plugin>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-annotate-plugin-test-annox-annotations</artifactId>
</plugin>
</plugins>
</configuration>
</plugin>
This part: 这部分:
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-annotate-plugin-test-annox-annotations</artifactId>
</plugin>
refers to the artifact containing the annotation classes. 指的是包含注释类的工件。
Here's a sample pom.xml
for maven-jaxb2-plugin / jaxb2-annotate-plugin combo. 这是maven-jaxb2-plugin / jaxb2-annotate-plugin组合的示例pom.xml
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.