简体   繁体   English

使用jdk8和maven-jaxb2-plugin的SAXParseException

[英]SAXParseException with jdk8 and maven-jaxb2-plugin

If you use a plugin like org.jvnet.jaxb2.maven2:maven-jaxb2-plugin to 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.

How do you make this plugin work with jdk8? 你如何使这个插件与jdk8一起工作?

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. 如果您可能正在与其他人一起处理该项目,这将无效,特别是如果他们仍在使用jdk7。 You could just run your maven build with the system property specified on the command line: 您可以使用命令行中指定的系统属性运行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-plugin to 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-plugin to 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>

随着2.4版本的插件:

<externalEntityProcessing>true</externalEntityProcessing>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM