Maven Xjc/Jaxb Plugin cannot parse simple Xsd file - xsd

I'm using the org.jvnet plugin and trying to convert an xsd file into a Java Jaxb-annotated object for use in marshalling and unmarshalling. However, there's an error when trying to compile a simple Xsd file for use.
I think there is a simple syntax error I am making, a second look would be really appreciated. If there's any more details I should provide please let me know. All relevant files and messages are included below.
Error message:
[ERROR] Error while parsing schema(s).Location [ file:/C:/.../project/asdfasdf/src/main/resources/RandomObjectSchema.
xsd{6,46}].
org.xml.sax.SAXParseException; systemId:
file:/C:/.../project/asdfasdf/src/main/resources/RandomObjectSchema.xsd;
lineNumber: 6; columnNumber: 46; src-resolve.4.2: Error resolving component 'xsd:int'.
It was detected that 'xsd:int' is in namespace 'http://www.w3c.org/2001/XMLSchema',
but components from this namespace are not referenceable from schema document
'file:/C:/.../project/asdfasdf/src/main/resources/RandomObjectSchema.xsd'.
If this is the incorrect namespace, perhaps the prefix of 'xsd:int' needs to be changed.
If this is the correct namespace, then an appropriate 'import' tag should be added to
'file:/C:/.../project/asdfasdf/src/main/resources/RandomObjectSchema.xsd'.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAX
ParseException(ErrorHandlerWrapper.java:203)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(Err
orHandlerWrapper.java:134)
File.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3c.org/2001/XMLSchema" targetNamespace="http://asdf.com/asdf">
<xsd:element name="RandomObject">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:int"></xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>asdf</groupId>
<artifactId>asdfasdf</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>asdfasdf</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.13.1</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

You have a mistake in your xsd namespace.
You have:
http://www.w3c.org/2001/XMLSchema
It should be:
http://www.w3.org/2001/XMLSchema
(www.w3c.org vs. www.w3.org)
The error log is quite verbose about it:
[ERROR] s4s-elt-schema-ns: The namespace of element 'schema' must be from the schema namespace, 'http://www.w3.org/2001/XMLSchema'.
line 2 of file:/.../test.xsd

Related

Generate Java Classes from XSD with default package name and XSD namespace

I have 2 sets of XSDs, One for Inbound operations and other for outbound operations. Both XSD sets have similar namespace but since are from different sources need to be maintained seperately in the same codeset. Each XSD set has deeply nested classes and generates roughly 650 classes.
I am using a Maven JAXB plugin to generate Java classes.
If I specify an <outputDirectory>, the file generation fails because of namespace collision between Outbound and Inbound operation. If I specify <packageName>, all classes are generated in the same package which results in namespace collision within Inbound operations.
Is there a way in which Maven will follow the package name provided by XSD namespace and prefix the package name with 'inbound' or 'outbound' to seperate the resulting package names. For e.g the packages create by XSD is
com.example.operation
com.example.operation.v1
Can it be modified to
inbound.com.example.operation
inbound.com.example.operation.v1
My Maven pom.xml file
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<groupId>com.example</groupId>
<modelVersion>4.0.0</modelVersion>
<artifactId>sample-integration-model</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sample-integration-model</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>2.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<encoding>UTF-8</encoding>
<source>1.8</source>
<target>1.8</target>
<showWarnings>true</showWarnings>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>xjc-outbound</id>
<goals>
<goal>xjc</goal>
</goals>
<configuration>
<laxSchemaValidation>true</laxSchemaValidation>
<outputDirectory>target/jaxb2/outbound</outputDirectory>
<sources>
<source>src/main/xsd/OutboundXsd/RequestMessagesDictionary</source>
<source>src/main/xsd/OutboundXsd/ResponseMessagesDictionary</source>
</sources>
</configuration>
</execution>
<execution>
<id>xjc-inbound</id>
<goals>
<goal>xjc</goal>
</goals>
<configuration>
<laxSchemaValidation>true</laxSchemaValidation>
<outputDirectory>target/jaxb2/inbound</outputDirectory>
<sources>
<source>src/main/xsd/InboundXsd/RequestMessagesDictionary</source>
<source>src/main/xsd/InboundXsd/ResponseMessagesDictionary</source>
</sources>
<clearOutputDir>false</clearOutputDir>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
You can tell Maven to set the generated classes in a given package.
Use <generatePackage>desired.package</generatePackage> inside the execution tag.
It is well documented here.

Karaf custom appender starts with error: java.lang.NoClassDefFoundError: javax/crypto/SecretKey

I'd a custom log4j custom appender, it works in my testing environment like:
C:\Log4jTest>java -cp . Log4jTest
But when configuring it into Karaf, when starting it always throws error:
org.apache.felix.configadmin-1.2.8|[org.osgi.service.log.LogService, org.knopflerfish.service.log.LogService, org.ops4j.pax.logging.PaxLoggingService, org.osgi.service.cm.ManagedService, id=8, bundle=4]: Unexpected problem updating Configuration PID=org.ops4j.pax.logging, factoryPID=null, bundleLocation=mvn:org.ops4j.pax.logging/pax-logging-service/1.6.9
java.lang.NoClassDefFoundError: javax/crypto/SecretKey
at com.microsoft.azure.storage.Credentials.(Credentials.java:63)
at com.microsoft.azure.storage.StorageCredentialsAccountAndKey.(StorageCredentialsAccountAndKey.java:42)
......
My mvn POM.XML is very simple:
[<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.hcit.logger</groupId>
<artifactId>cloud-logger-service</artifactId>
<version>1.0.0</version>
<packaging>bundle</packaging>
<dependencies>
<dependency>
<groupId>org.ops4j.pax.logging</groupId>
<artifactId>pax-logging-service</artifactId>
<version>1.6.9</version>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-storage</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.3.7</version>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-Name>${project.groupId}.${project.artifactId}</Bundle-Name>
<Bundle-SymbolicName>${project.groupId}.${project.artifactId}</Bundle-SymbolicName>
<Export-Package>com.hcit.logger</Export-Package>
<Import-Package>!*</Import-Package>
<Embed-Dependency>*;scope=compile|runtime;inline=true</Embed-Dependency>
<_failok>true</_failok>
<Fragment-Host>org.ops4j.pax.logging.pax-logging-service</Fragment-Host>
<Implementation-Version>${project.version}</Implementation-Version>
<Bundle-Version>${project.version}</Bundle-Version>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
</project>
]
And snippet of my org.ops4j.pax.logging.cfg:
....
log4j.appender.hafauditCloudLoggerAppender=com.hcit.logger.CloudLoggerAppender
log4j.appender.hafauditCloudLoggerAppender.Threshold=DEBUG
log4j.appender.hafauditCloudLoggerAppender.TableName=LoggerTable
log4j.category.com.gehcit.haf.audit.consumer = DEBUG,hafauditCloudLoggerAppender
......
I checked that my jre.properties did have the javax.crypto:
......
javax.crypto, \
javax.crypto.interfaces, \
javax.crypto.spec, \
......
I'm new to Karaf, and wonder how to resolve it? my JDK is jdk1.7.0_72 and thanks.
Finally I found the tricks, if changing the config.properties by adding those components like this:
org.osgi.framework.bootdelegation=org.apache.karaf.jaas.boot,sun.,com.sun.,javax.transaction,javax.transaction.,javax.sql.,oracle.,com.microsoft.sqlserver., javax.crypto, javax.crypto., javax.xml., com.fasterxml.*
It works.
of course in POM.xml should add the dependency of fasterxml:
[<dependency>
<groupId>de.matrixweb.smaller</groupId>
<artifactId>ant</artifactId>
<version>0.8.4</version>
</dependency>]

Maven ANTLR generating wrong directory

I have the following pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>db.project</groupId>
<artifactId>engine</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<antlr4.visitor>true</antlr4.visitor>
<antlr4.listener>true</antlr4.listener>
</properties>
<dependencies>
<dependency>
<groupId>org.antlr</groupId>
<artifactId>antlr4-runtime</artifactId>
<version>4.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<testSourceDirectory>test</testSourceDirectory>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.antlr</groupId>
<artifactId>antlr4-maven-plugin</artifactId>
<version>4.0</version>
<configuration>
<sourceDirectory>src</sourceDirectory>
</configuration>
<executions>
<execution>
<goals>
<goal>antlr4</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
Normally, I use ANLTR 4 plugin which is described in this link : https://github.com/jknack/antlr4ide . But I want to use maven now, when I update my grammar in com.db.project directory, parser and lexer are generated in both default package (i.e., src/default package) and src/com/db/project/ twice, whereas I only want my lexer and parser in src/com/db/project/ directory. (including both .g4 file and java files in the same package)
How can I change this via pom.xml?
Simple change 4.0 to 4.2 in your antlr4-maven-plugin's version tag.
Note that the plugin will recursively scan src/main/antlr4 for .g4 grammar files. If your grammar resides in src/main/antlr4/com/company then the package of your generated parser/lexer files will be com.company.
Here's a small template project to get you started, in case you need it:
https://github.com/bkiers/antlr4-template

Not able to generate annotation through maven jaxb plugin

I am trying to add javax.persistence.Id as an annotation to a filed and generate as Java objects through Maven JAXB plugin. However I run into class not found exception for javax.persistence.id I did make sure that the javax.persistence is included in the maven dependency and I see maven pulling it as dependency but when I run through jaxb plugin it won't work.
Here is my XML <xsd:complexType name="MyTable">
<xsd:sequence>
<xsd:element name="id" type="xsd:int"/>
</xsd:sequence>
</xsd:complexType>
Here is my binding.xjb file
<jaxb:bindings version="2.1"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
xmlns:annox="http://annox.dev.java.net" jaxb:extensionBindingPrefixes="annox">
<jaxb:bindings schemaLocation="mytable.xsd">
<jaxb:bindings node="xs:complexType[#name='MyTable']/xs:sequence/xs:element[#name='id']">
<annox:annotate target="field">
<annox:annotate annox:class="javax.persistence.Id"/>
</annox:annotate>
</jaxb:bindings>
</jaxb:bindings>
Here is my relevant Pom.xml
<dependencies>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.2.9</version>
</dependency>
<dependency>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics</artifactId>
<version>0.6.4</version>
</dependency>
<dependency>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics-annotate</artifactId>
<version>0.6.4</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0.2</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.8.3</version>
<executions>
<execution>
<phase>generate-sources</phase>
<configuration>
<forceRegenerate>true</forceRegenerate>
<schemaDirectory>myschema</schemaDirectory>
<bindingIncludes>
<include>binding.xjb</include>
</bindingIncludes>
<extension>true</extension>
<args>
<arg>-Xvalue-constructor</arg>
<arg>-XtoString</arg>
<arg>-Xequals</arg>
<arg>-XhashCode</arg>
<arg>-Xcopyable</arg>
<arg>-Xannotate</arg>
</args>
<plugins>
<plugin> <groupId>org.jvnet.jaxb2_commons</groupId>
jaxb2-basics 0.6.4
org.jvnet.jaxb2_commons
jaxb2-basics-annotate 0.6.4
org.jvnet.jaxb2_commons
jaxb2-value-constructor
3.0
</plugins>
</configuration>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<versionRange>[0.7.4,)</versionRange>
<goals>
<goal>generate</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore></ignore>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
However when I run Maven-->Generate-Sources I get this error
Caused by: org.jvnet.annox.parser.AnnotationElementParseException: Could not parse the annotation element.
at org.jvnet.annox.parser.XAnnotationParser.parse(XAnnotationParser.java:90)
at org.jvnet.jaxb2_commons.plugin.annotate.AnnotatePlugin.annotate(AnnotatePlugin.java:387)
... 31 more
Caused by: org.jvnet.annox.annotation.AnnotationClassNotFoundException: Annotation class [javax.persistence.Id] could not be found.
... 33 more
Caused by: java.lang.ClassNotFoundException: javax.persistence.Id
If I simply add #Id annotation to any java class in the project then I can add and I see javax.persistence.Id getting imported with no problem. What is going wrong when I use maven & binding.xjb? Am I not defining the annotation properly? Many thanks!
I guess it's also the same problem that I had: you added javax.persistence as a Maven dependency, but not as a dependency to your JAXB plugin:
Add something like this (if you're using Hibernate):
</project>
...
<build>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>${maven-jaxb2-plugin.version}</version>
<executions>
...
</executions>
<configuration>
...
</configuration>
<dependencies>
<!-- Hibernate Persistence Annotations -->
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<version>${hibernate-jpa-2.0-api.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
...
</project>
I had a similar issue and was able to solve it with that. In the following example please note that I'm putting the annotations into the XSD and not into the XJB file but the Maven configuration should be similar.
Here's my XSD:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="2.1"
elementFormDefault="qualified" targetNamespace="http://www.gl-group.com/ewelding/schemas"
xmlns:ew="http://www.companyname.com/project/schemas"
xmlns:xmime="http://www.w3.org/2005/05/xmlmime" jaxb:extensionBindingPrefixes="annox"
xmlns:annox="http://annox.dev.java.net"
xmlns:ja="http://annox.dev.java.net/javax.xml.bind.annotation"
xmlns:jpa="http://annox.dev.java.net/javax.persistence"
xmlns:solrj="http://annox.dev.java.net/org.apache.solr.client.solrj.beans"
xmlns:jackson="http://annox.dev.java.net/com.fasterxml.jackson.annotation">
...
<xs:element name="Certificate">
<xs:annotation>
<xs:appinfo>
<annox:annotate>
<ja:XmlAccessorType value="NONE"/>
<jpa:Entity name="Certificate"/>
<solrj:Field value="testByOrderOf"/>
<jackson:JsonFormat shape="STRING" pattern="yyyy-MM-dd'T'HH:mm:ss'Z'" timezone="GMT"/>
</annox:annotate>
</xs:appinfo>
</xs:annotation>
...
<xs:element ref="ew:dateOfBirth">
<xs:annotation>
<xs:appinfo>
<annox:annotate target="field">
<solrj:Field value="dateOfBirth"/>
<jackson:JsonFormat shape="STRING" pattern="yyyy-MM-dd'T'HH:mm:ss'Z'" timezone="GMT"/>
</annox:annotate>
</xs:appinfo>
</xs:annotation>
</xs:element>
...
</xs:element>
And here are the relevant sections from my pom.xml:
...
<properties>
...
<maven-jaxb2-plugin.version>0.8.3</maven-jaxb2-plugin.version>
<jaxb2-basics.version>0.6.4</jaxb2-basics.version>
<jaxb2-value-constructor.version>3.0</jaxb2-value-constructor.version>
<solr-solrj.version>4.3.0</solr-solrj.version>
<jackson.version>2.2.2</jackson.version>
<hibernate-jpa-2.0-api.version>1.0.1.Final</hibernate-jpa-2.0-api.version>
...
</properties>
...
<build>
<plugins>
...
<!-- Generate Java sources from XSD schema files -->
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>${maven-jaxb2-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<extension>true</extension>
<args>
<arg>-Xannotate</arg>
<arg>-Xvalue-constructor</arg>
<arg>-Xinheritance</arg>
<arg>-enableIntrospection</arg>
</args>
<!-- Include our schema -->
<schemaDirectory>src/main/resources</schemaDirectory>
<schemaIncludes>
<include>certificate.xsd</include>
</schemaIncludes>
<bindingIncludes>
<bindings>certificate.xjb</bindings>
</bindingIncludes>
<generateDirectory>src/main/java</generateDirectory>
<generatePackage>com.company.project.generated</generatePackage>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics</artifactId>
<version>${jaxb2-basics.version}</version>
</plugin>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics-annotate</artifactId>
<version>${jaxb2-basics.version}</version>
</plugin>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-value-constructor</artifactId>
<version>${jaxb2-value-constructor.version}</version>
</plugin>
</plugins>
</configuration>
<dependencies>
<!-- SolrJ - only needed for the #Field annotation -->
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>${solr-solrj.version}</version>
</dependency>
<!-- Jackson2 Annotations -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
</dependency>
<!-- Hibernate Persistence Annotations -->
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<version>${hibernate-jpa-2.0-api.version}</version>
</dependency>
</dependencies>
</plugin>
...
</plugins>
</build>
I had the same exception, this fixed it. Just be aware that my example puts everything into the XSD and not into the XJB... but the dependency-thing mentioned above should help you as well.

xjc / jaxb2-commons interface binding issue, customization attached to the wrong place

I'm trying to add an interface using the jaxb2-basics artifact from the jaxb2_commons maven group.
My pom.xml contains the following dependencies
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.2.7</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.2.6</version>
</dependency>
<dependency>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics-runtime</artifactId>
<version>0.6.4</version>
</dependency>
and the plugin configuration looks like
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb22-plugin</artifactId>
<version>0.8.2</version>
<executions>
<execution>
<id>jaxb-generate-messages-in</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<specVersion>2.2</specVersion>
<schemaLanguage>XMLSCHEMA</schemaLanguage>
<schemaDirectory>src/main/schema</schemaDirectory>
<schemaIncludes>
<include>MESSAGES-IN.xsd</include>
</schemaIncludes>
<bindingDirectory>src/main/binding</bindingDirectory>
<bindingIncludes>
<include>messages-in-binding.xjb</include>
</bindingIncludes>
<episodeFile>${project.build.directory}/generated-sources/messages-in/META-INF/jaxb-messages-in.episode</episodeFile>
<generateDirectory>${project.build.directory}/generated-sources/messages-in</generateDirectory>
<extension>true</extension>
<args>
<arg>-Xinheritance</arg>
</args>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics</artifactId>
<version>0.6.4</version>
</plugin>
</plugins>
</configuration>
</execution>
<execution>
<id>jaxb-generate-messages-out</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<specVersion>2.2</specVersion>
<schemaLanguage>XMLSCHEMA</schemaLanguage>
<schemaDirectory>src/main/schema</schemaDirectory>
<schemaIncludes>
<include>MESSAGES-OUT.xsd</include>
</schemaIncludes>
<bindingDirectory>src/main/binding</bindingDirectory>
<bindingIncludes>
<include>messages-out-binding.xjb</include>
</bindingIncludes>
<episodeFile>${project.build.directory}/generated-sources/messages-out/META-INF/jaxb-messages-out.episode</episodeFile>
<generateDirectory>${project.build.directory}/generated-sources/messages-out</generateDirectory>
</configuration>
</execution>
</executions>
<configuration>
<verbose>true</verbose>
</configuration>
</plugin>
As you can see from the above, there are two invocations of xjc, which both work. Focusing on the first one, my bindings file
<jaxb:bindings
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
xmlns:inheritance="http://jaxb2-commons.dev.java.net/basic/inheritance"
jaxb:extensionBindingPrefixes="xjc"
version="1.0">
<jaxb:bindings schemaLocation="../schema/MESSAGES-IN.xsd" node="/xs:schema">
<jaxb:globalBindings typesafeEnumMaxMembers="3000">
<jaxb:serializable uid="1"/>
</jaxb:globalBindings>
<jaxb:schemaBindings>
<jaxb:package name="com.whatever.messages.request"/>
</jaxb:schemaBindings>
<jaxb:bindings node="//xs:simpleType[#name='YesOrNo']">
<jaxb:class ref="com.whatever.messages.YesOrNo"/>
</jaxb:bindings>
<jaxb:bindings noade="//xs:complexType[#name='someLogin']">
<jaxb:class name="LoginRequest">
<jaxb:javadoc><![CDATA[A Login request message.]]>
</jaxb:javadoc>
</jaxb:class>
</jaxb:bindings>
</jaxb:bindings>
</jaxb:bindings>
works like a charm; but, when I attempt to add an interface to 'LoginRequest'...
<jaxb:bindings node="//xs:complexType[#name='someLogin']">
<jaxb:class name="LoginRequest">
<jaxb:javadoc><![CDATA[A Login request message.]]>
</jaxb:javadoc>
</jaxb:class>
<inheritance:implements>com.whatever.messages.Request</inheritance:implements>
</jaxb:bindings>
I receive the error message
Error while parsing schema(s).Location [ file:/C:/Users/justme/Documents/NetBeansProjects/someproject/src/main/binding/messages-in-binding.xjb{19,42}].
com.sun.istack.SAXParseException2; systemId: file:/C:/Users/justme/Documents/NetBeansProjects/someproject/src/main/binding/messsages-in-binding.xjb; lineNumber: 19; columnNumber: 42; compiler was unable to honor this class customization. It is attached to a wrong place, or its inconsistent with other bindings.
which reports that the location was
Error while generating code.Location [ file:/C:/Users/justme/Documents/NetBeansProjects/someproject/src/main/schema/MESSAGES-IN.xsd{106693,54}].
which happens to correspond to
<xs:complexType name="someLogin" mixed="true">
...
</xs:complexType>
Now, I've tried a second directive to bind the interface to the XSD element
<xs:element name="someLogin" type="someLogin" substitutionGroup="externalMethod"/>
But I just get the same error message with the element's line number as the location.
Obviously one wants to attach an interface to a class, and all of the examples look pretty close to my bindings file, but something must be wrong.
My environment is
Apache Maven 3.0.4 (r1232337; 2012-01-17 02:44:56-0600)
Maven home: C:\Program Files\NetBeans 7.2.1\java\maven
Java version: 1.7.0_07, vendor: Oracle Corporation
Java home: C:\Program Files (x86)\Java\jdk1.7.0_07\jre
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 7", version: "6.1", arch: "x86", family: "windows"
Can someone explain why xjc believes the extension is operating on the wrong XSD type?
In the construction
<jaxb:bindings
node="//Xs:complexType[#name='someLogin">
...
</jaxb:bindings>
the node attribute is expected to be an XPath expression. (Or so it says at Oracle). Try adding the closing square bracket to make it one, and see if that helps.

Resources