I have many classes generated by JAXB's xsd2java. I need all these classes to be annotated with specific annotations at compile time (for example with lombok annotations). 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.
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. I assume that when you said xsd2java
you probably meant XJC.
Update
The OP asked about in comments how to configure it with the jaxb2-maven-plugin.
You can use jaxb2-annotate-plugin
with jaxb2-maven-plugin as well. I just have never tried it.
dependencies/depenency
in pom.xml
. arguments
into the configuration.See this answer (and question) for examples:
It is about other plugins but you'll get a clue on how to configure it with Codehaus jaxb2-maven-plugin.
Configuration with my maven-jaxb2-plugin would be as follows:
<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.