Search code examples
javamavenpom.xmlcheckstylejaxb2

How to format a JAXB generated java class?


My code fails due to checkstyle issues while compiling auto-generated jaxb classes. Any hints? Mentioned below is the maven error and except from my pom.xml.

Error

[INFO] --- maven-checkstyle-plugin:2.12:checkstyle (enforce-style) @ my ---
[INFO] Starting audit...
DefaultRole.java:42:1: '{' should be on the previous line.
RoleT.java:39:1: '{' should be on the previous line.
Audit done.

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------

Pom.xml

<plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>jaxb2-maven-plugin</artifactId>

            <executions>
                <execution>
                    <goals>
                        <goal>xjc</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <schemaDirectory>src/main/webapp/schemas/</schemaDirectory>
                <packageName>com.my.schema.jaxbtest</packageName>
                <outputDirectory>${basedir}/src/main/java</outputDirectory>
            </configuration>
        </plugin>

Solution

  • Don't bother formatting generated source code!

    Instead, add a CheckStyle exclusion for the generated sources in the com.my.schema.jaxbtest package.