Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SAXParseException with jdk8 and maven-jaxb2-plugin

Tags:

java

maven

xsd

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

like image 972
Niel de Wet Avatar asked Nov 06 '15 14:11

Niel de Wet


2 Answers

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:

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 :

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:

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

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

<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>
like image 77
Niel de Wet Avatar answered Oct 28 '22 14:10

Niel de Wet


With the 2.4 version of the plugin:

<externalEntityProcessing>true</externalEntityProcessing>
like image 31
Rob Tulloh Avatar answered Oct 28 '22 14:10

Rob Tulloh