5

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?

1 Answer 1

7

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:

  • Annotations are provided in Java syntax, but you have to use fully qualified names.
  • You have to include annotations classes (lombok, for instance) into the XJC classpath as these classes must be available during the schema compilation time.

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 .

You can use jaxb2-annotate-plugin with as well. I just have never tried it.

  • You can include additional JARs into classpath by using the dependencies/depenency in pom.xml.
  • Then you'll need to add arguments into the configuration.

See this answer (and question) for examples:

https://stackoverflow.com/a/12804497/303810

It is about other plugins but you'll get a clue on how to configure it with Codehaus .

Configuration with my 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 / combo.

Sign up to request clarification or add additional context in comments.

5 Comments

Indeed I meant XJC, sorry about that. I saw this plugin before but I thought it could only be used for JDK annotations. I am using jaxb2-maven-plugin to generate classes can you explain what "include into the XJC classpath" means in this case? Also, I am having trouble understanding how this works. How will I annotate all classes of a package? Does target="package" does that? Thank you in advance!
also, it seems that i will need maven-jaxb2-plugin instead of jaxb2-maven-plugin is that right?
@alex I happen to be the author of maven-jaxb2-plugin as well. So my advise to move to use maven-jaxb2-plugin would be very biased.
@alex You can use jaxb2-annotate-plugin with jaxb2-maven-plugin as well. I just have never tried it. You can include additional JARs into classpath by using the dependencies/depenency in pom.xml. Then you'll need to add arguments into the configuration. See this answer (and question) for examples: 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.
I decided to go with the fluent-api plugin which I found in the example you mentioned. That is what I wanted anyway, a builder-like way to create instances

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.