java - 生成的 XSD 中的空行

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

最佳答案

通过使用 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我的 XSD 由 jaxb2-maven-plugin 生成看起来很合理:
<?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我的 XSD 由 jaxb2-maven-plugin 生成包含许多空行:
<?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我的 XSD 由 jaxb2-maven-plugin 生成再看合理:
<?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很旧,可能不再维护了。

希望这对其他人有帮助。

关于java - 生成的 XSD 中的空行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57309893/

相关文章:

小数点可接受的 xsd 模式?

XML Schema Identity constraints - 我可以在选择器的 XPath 中使用绝对路径吗?

xsd - Jaxb 从导入的架构中为未使用的元素生成对象

java - 使用 org.springframework.oxm jaxb2marshaller 追加 CDATA

java - JAXB 编译器正在绑定(bind) xs :boolean to Java boolean instead of Boolean wrapper class

java - 关闭命令窗口时如何在生产模式下启动 grail 应用程序而不终止 session ?

java - 使用 hibernate 批量保存或更新

java - 解析 XSD 文档时出错

java - 如何使用 Java 有效地解析 HTML?

java - 安装java没有效果(jre或jdk,1.8来自oracle站点)