5

I'm trying to generate schema files from some Java classes using jaxb2-maven-plugin(version 2.5.0). I'm getting the schema1.xsd file without any warnings, but it contains extra empty lines:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0">

  <xs:complexType name="sampleRequest">

    <xs:sequence>

      <xs:element minOccurs="0" name="someField" type="xs:string"/>

    </xs:sequence>

  </xs:complexType>

</xs:schema>

I couldn't find out any reason or a way to configure this behavior(http://www.mojohaus.org/jaxb2-maven-plugin/Documentation/v2.2/schemagen-mojo.html). I'm using pretty simple configuration so far:

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>jaxb2-maven-plugin</artifactId>
            <version>2.5.0</version>
            <executions>
                <execution>
                    <goals>
                        <goal>schemagen</goal>
                    </goals>
                    <phase>generate-resources</phase>
                    <configuration>
                        <includes>
                            <include>path/to/my/package/*.java</include>
                        </includes>
                        <outputDirectory>${project.build.directory}/schemas</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>

Am I doing something wrong or is there any way to configure the output? I don't think that's an encoding issue since there are actually some indenting spaces on the blank lines, not just line breaks:

enter image description here

I'm using JDK 11

NeplatnyUdaj
  • 5,471
  • 4
  • 38
  • 70
  • got the same problem while upgrading from jdk 8 to jdk 11 and spring boot from 2.0.0 -> 2.2.4. Found a solution ? – elonderin Feb 10 '20 at 15:24
  • I'm having the same problem, but in my situation there are three empty lines between any two non-blank lines. Has anyone a solution? – zovits Feb 20 '20 at 12:28

1 Answers1

0

I have ended up with following workaround solution by utilizing maven-replacer-plugin:

<plugin>
    <groupId>com.google.code.maven-replacer-plugin</groupId>
    <artifactId>maven-replacer-plugin</artifactId>
    <version>1.4.1</version>
    <executions>
        <execution>
            <id>remove-empty-lines</id>
            <phase>generate-resources</phase>
            <goals>
                <goal>replace</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <includes>
            <include>${project.build.directory}/schemas/*.xsd</include>
        </includes>
        <token>\s*$</token>
        <value/>
        <regexFlags>
            <regexFlag>MULTILINE</regexFlag>
        </regexFlags>
    </configuration>
</plugin>

With JDK 8 and without maven-replacer-plugin my XSDs generated by jaxb2-maven-plugin look reasonable:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:EchoService="http://example/EchoService" elementFormDefault="qualified" targetNamespace="http://example/EchoService" version="1.0">

  <xs:element name="echoServiceRequest" type="EchoService:echoServiceRequest"/>

  <xs:complexType name="echoServiceRequest">
    <xs:sequence>
      <xs:element name="message" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

With JDK 11 and without maven-replacer-plugin my XSDs generated by jaxb2-maven-plugin contain many empty lines:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:EchoService="http://example/EchoService" elementFormDefault="qualified" targetNamespace="http://example/EchoService" version="1.0">



  <xs:element name="echoServiceRequest" type="EchoService:echoServiceRequest"/>



  <xs:complexType name="echoServiceRequest">



    <xs:sequence>



      <xs:element name="message" type="xs:string"/>



    </xs:sequence>



  </xs:complexType>



</xs:schema>

With JDK 11 and with maven-replacer-plugin my XSDs generated by jaxb2-maven-plugin look reasonable again:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:EchoService="http://example/EchoService" elementFormDefault="qualified" targetNamespace="http://example/EchoService" version="1.0">
  <xs:element name="echoServiceRequest" type="EchoService:echoServiceRequest"/>
  <xs:complexType name="echoServiceRequest">
    <xs:sequence>
      <xs:element name="message" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

So I guess there is a bug in jaxb2-maven-plugin / xjc / JDK 11 causing empty lines.

I admit that this is a workaround solution and that maven-replacer-plugin is very old and probably not maintained anymore.

Hope this helps others.

ondrej.lerch
  • 118
  • 5