XJC - Add Lombok Annotation on top of every Generated XSD class and remove Setters Getters

601 views Asked by At

I am working on jaxb2-maven-plugin, I am generating Java POJOs from XSD.

I need to add @Data annotation on top of the class without setters getters.

The output file should look like this.

@Data
public class Employee {

    @JsonProperty("name")
    private Name name;
    @JsonProperty("joindate")
    private String joindate;
    @JsonProperty("payload")
    private String payload;
}

My XSD is like this

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="employee">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="name">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="lastname" type="xs:string"  />
              <xs:element name="firstname" type="xs:string" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
        <xs:element name="hiredate" type="xs:string" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

I need to add annotation on Name.java as well. I have around 100 XSDs. I need to do this for all XSDs

1

There are 1 answers

0
SebastianX On

Yes you can do that, by adding 3rd party plugins for jaxb generating, for example, https://github.com/svzdvd/jaxb-lombok-plugin

<plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>jaxb2-maven-plugin</artifactId>
            <version>2.2</version>
            <dependencies>
                <dependency>
                    <groupId>it.yobibit</groupId>
                    <artifactId>jaxb-lombok-plugin</artifactId>
                    <version>1.0.0</version>
                </dependency>
            </dependencies>
            <executions>
                <execution>
                    <id>xjc</id>
                    <goals>
                        <goal>xjc</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <packageName>com.company.model</packageName>
                <arguments>
                    <argument>-Xlombok</argument>
                    <argument>-Xlombok:Setter</argument>
                    <argument>-Xlombok:removeGeneratedSourceSetters</argument>
                    <argument>-Xlombok:ToString</argument>
                    <argument>-Xlombok:Builder</argument>
                </arguments>
            </configuration>
        </plugin>

Then the generated class will have the lombok annotations as you want. there are couple of others similar plugins more or less doing the same thing, check them:

https://jitpack.io/p/wwerner/jaxb-lombok-plugin

https://github.com/mplushnikov/xjc-lombok-plugin