【问题标题】:SAXParseException with jdk8 and maven-jaxb2-plugin带有 jdk8 和 maven-jaxb2-plugin 的 SAXParseException
【发布时间】:2015-11-06 14:40:45
【问题描述】:

如果你使用像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吗?
  • @Stefan 绝对是,但是这个问题,虽然它的根本原因相同,但与使用 maven 插件生成代码无关。

标签: java maven xsd


【解决方案1】:

这个问题的根本原因与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>

【讨论】:

  • 您好 Niel,我使用的是 JRE1.8.0_111,但找不到文件 jaxp.properties。有什么想法吗?
  • 不知道,对不起@AyadiAkrem
  • properties-maven-plugin 与 1.6 版本的 jaxb2-maven-plugin 配合使用
【解决方案2】:

使用2.4版本的插件:

<externalEntityProcessing>true</externalEntityProcessing>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-14
    • 1970-01-01
    • 2023-03-31
    • 2015-01-12
    • 1970-01-01
    相关资源
    最近更新 更多