我试图用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.orgjaxb2-maven-pluginDocumentationv2.2schemagen-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>
是我做错了什么还是有什么方法可以配置输出?我认为这不是编码的问题,因为实际上在空行上有一些缩进的空格,而不仅仅是换行符。
我使用的是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>
所以我猜测有一个bug在 jaxb2-maven-plugin
xjc JDK 11导致空行。
我承认这是一个变通的解决方案,并且 maven-replacer-plugin
是很老的,可能已经不维护了。
希望对别人有所帮助。