带有 jdk8 和 maven-jaxb2-plugin 的 SAXParseException

SAXParseException with jdk8 and maven-jaxb2-plugin

如果你使用像org.jvnet.jaxb2.maven2:maven-jaxb2-plugin这样的插件来解析你的xsd文件,你在从jdk7升级到jdk8时会遇到这个异常:

org.xml.sax.SAXParseException; systemId: file:/D:/Work/my/schema.xsd; lineNumber: 27; columnNumber: 133; schema_reference: Failed to read schema document 'CoreComponentsTechnicalSpecification-1p0.xsd', because 'file' access is not allowed due to restriction set by the accessExternalSchema property.

如何让这个插件与 jdk8 一起工作?

这个问题的根本原因与this one相同。有两种方法可以解决这个问题:

设置javax.xml.accessExternalSchema系统属性:

如果您只是在本地构建,您可以将此行添加到 /path/to/jdk1.8.0/jre/lib 下名为 jaxp.properties 的文件(如果它不存在):

javax.xml.accessExternalSchema=all

如果您可能正在与其他人一起处理该项目,尤其是当他们仍在使用 jdk7 时,这将不起作用。您可以 运行 使用在命令行中指定的系统 属性 构建 Maven:

$mvn <target and options> -Djavax.xml.accessExternalSchema=all

您也可以使用插件为您设置系统属性:

<plugin>
    <!-- Needed to run the plugin xjc en Java 8 or superior -->
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>properties-maven-plugin</artifactId>
    <version>1.0-alpha-2</version>
    <executions>
        <execution>
            <id>set-additional-system-properties</id>
            <goals>
                <goal>set-system-properties</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <properties>
            <property>
                <name>javax.xml.accessExternalSchema</name>
                <value>all</value>
            </property>
            <property>
                <name>javax.xml.accessExternalDTD</name>
                <value>all</value>
            </property>
        </properties>
    </configuration>
</plugin>

您还可以配置 maven-jaxb2-plugin 来设置 属性:

<plugin>
   <groupId>org.jvnet.jax-ws-commons</groupId>
   <artifactId>jaxws-maven-plugin</artifactId>
   <version>2.3</version>
   <configuration>
     <!-- Needed with JAXP 1.5 -->
     <vmArgs>
         <vmArg>-Djavax.xml.accessExternalSchema=all</vmArg>
     </vmArgs>
   </configuration>
</plugin>

设置目标版本: 如果不想使用系统属性,可以将 maven-jaxb2-plugin 设置为目标版本 2.0:

<plugin>
    <groupId>org.jvnet.jaxb2.maven2</groupId>
    <artifactId>maven-jaxb2-plugin</artifactId>
    <version>${maven.plugin.jaxb2.version}</version>
    <configuration>
        <args>
            <arg>-target</arg>
            <arg>2.0</arg>
        </args>
    </configuration>
</plugin>

使用2.4版本的插件:

<externalEntityProcessing>true</externalEntityProcessing>