使用jdk8和maven-jaxb2-plugin的SAXParseException

Nie*_*Wet 17 java xsd maven

如果您使用类似于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一起工作?

Nie*_*Wet 42

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

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

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

javax.xml.accessExternalSchema=all
Run Code Online (Sandbox Code Playgroud)

如果您可能正在与其他人一起处理该项目,这将无效,特别是如果他们仍在使用jdk7.您可以使用命令行中指定的系统属性运行maven构建:

$mvn <target and options> -Djavax.xml.accessExternalSchema=all
Run Code Online (Sandbox Code Playgroud)

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

<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>
Run Code Online (Sandbox Code Playgroud)

您还可以配置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>
Run Code Online (Sandbox Code Playgroud)

设置目标版本: 如果您不想使用系统属性,可以设置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>
Run Code Online (Sandbox Code Playgroud)


小智 5

使用 2.4 版本的插件:

<externalEntityProcessing>true</externalEntityProcessing>
Run Code Online (Sandbox Code Playgroud)