java – 具有jdk8和maven-jaxb2-plugin的SAXParseException

如果您使用像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:由于accessExternalSchema属性设置了限制,因此无法读取模式文档’CoreComponentsTechnicalSpecification-1p0.xsd’,因为’file’访问是不允许的.

你如何使这个插件与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>

相关文章

最近看了一下学习资料,感觉进制转换其实还是挺有意思的,尤...
/*HashSet 基本操作 * --set:元素是无序的,存入和取出顺序不...
/*list 基本操作 * * List a=new List(); * 增 * a.add(inde...
/* * 内部类 * */ 1 class OutClass{ 2 //定义外部类的成员变...
集合的操作Iterator、Collection、Set和HashSet关系Iterator...
接口中常量的修饰关键字:public,static,final(常量)函数...