生成的XSD中的空行

时间:2019-08-01 13:16:15

标签: java xsd jaxb2-maven-plugin

我正在尝试使用jaxb2-maven-plugin(版本2.5.0)从某些Java类生成模式文件。我得到的schema1.xsd文件没有任何警告,但其中包含多余的空行:

<?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>

我找不到任何配置此行为的原因或方法(http://www.mojohaus.org/jaxb2-maven-plugin/Documentation/v2.2/schemagen-mojo.html)。到目前为止,我使用的是非常简单的配置:

        <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>

我做错什么了吗,或者有什么方法可以配置输出?我不认为这是编码问题,因为实际上空行上有一些缩进空间,而不仅仅是换行符:

enter image description here

我正在使用JDK 11

1 个答案:

答案 0 :(得分:0)

通过使用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>

在使用JDK 8且没有maven-replacer-plugin的情况下,由jaxb2-maven-plugin生成的XSD看起来很合理:

<?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>

在使用JDK 11且没有maven-replacer-plugin的情况下,由jaxb2-maven-plugin生成的XSD包含许多空行:

<?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>

对于JDK 11和maven-replacer-plugin,由jaxb2-maven-plugin生成的XSD看起来又是合理的:

<?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>

所以我想jaxb2-maven-plugin / xjc / JDK 11中有一个错误,导致空行。

我承认这是一种解决方法,maven-replacer-plugin很老,可能不再维护了。

希望这对其他人有帮助。