java 带有 jdk8 和 maven-jaxb2-plugin 的 SAXParseException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33569261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 21:51:37  来源:igfitidea点击:

SAXParseException with jdk8 and maven-jaxb2-plugin

javamavenxsd

提问by Niel de Wet

If you use a plugin like org.jvnet.jaxb2.maven2:maven-jaxb2-pluginto parse your xsd files, you encounter this exception when upgrading from jdk7 to jdk8:

如果你使用插件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.

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.

How do you make this plugin work with jdk8?

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

回答by Niel de Wet

This question has the same root cause as this one. There are two ways to solve this problem:

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

Setting the javax.xml.accessExternalSchema system property:

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

If you are only building locally, you can add this line to a file named jaxp.properties (if it doesn't exist) under /path/to/jdk1.8.0/jre/lib :

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

javax.xml.accessExternalSchema=all


This won't work if you might be working on the project with others, especially if they are still using jdk7. You could just run your maven build with the system property specified on the command line:

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

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


You can also use a plugin to set the system property for you:

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

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


You can also configure the maven-jaxb2-pluginto set the property:

您还可以配置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>


Setting the target version:If you don't want to use system properties, you can set up the maven-jaxb2-pluginto target version 2.0:

设置目标版本:如果不想使用系统属性,可以设置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>

回答by Rob Tulloh

With the 2.4 version of the plugin:

使用 2.4 版本的插件:

<externalEntityProcessing>true</externalEntityProcessing>