Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@XmlRootElement not added to corresponding Java Class #49

Open
cashol opened this issue Mar 14, 2022 · 1 comment
Open

@XmlRootElement not added to corresponding Java Class #49

cashol opened this issue Mar 14, 2022 · 1 comment

Comments

@cashol
Copy link

cashol commented Mar 14, 2022

Hi.
I am using Java 17 and Jakarta Xml Binding 3.0 for code generation from XML Schema.

The jaxb task results with the following error:
Working Directory: D:\DEV\git\s2r-cloud\utilities\xml-utils
Gradle user home: D:\DEV\gradle_user_home
Gradle Distribution: Local installation at D:\DEV\Gradle
Gradle Version: 7.4
Java Home: D:\DEV\Java
JVM Arguments: None
Program Arguments: None
Build Scans Enabled: false
Offline Mode Enabled: false
Gradle Tasks: build

Task :jaxbJavaGenBttcg FAILED

FAILURE: Build failed with an exception.

  • What went wrong:
    Execution failed for task ':jaxbJavaGenBttcg'.

xjc failed

  • Try:

Run with --stacktrace option to get the stack trace.
Run with --info or --debug option to get more log output.
Run with --scan to get full insights.

BUILD FAILED in 1s
1 actionable task: 1 executed

build.gradle:
buildscript {
apply from: '../../common-versions.gradle'

ext {
}
}

plugins {
id 'java'

id 'com.intershop.gradle.jaxb' version '5.1.0'
}

sourceCompatibility = '17'
targetCompatibility = '17'

repositories {
mavenLocal()
mavenCentral()
}

dependencies {
implementation "org.slf4j:slf4j-api:${versionSlf4j}"
implementation "org.slf4j:slf4j-ext:${versionSlf4j}"

implementation "org.apache.commons:commons-lang3:${versionApacheCommonsLang3}"

implementation "jakarta.xml.bind:jakarta.xml.bind-api:${versionJakartaXmlBindApi}"
implementation "org.eclipse.persistence:org.eclipse.persistence.moxy:${versionEclipsePersistenceMoxy}"

// Use JUnit Jupiter for testing.
testImplementation "org.slf4j:slf4j-simple:${versionSlf4j}"
testImplementation(platform("org.junit:junit-bom:${versionJunitJupiter}"))
testImplementation('org.junit.jupiter:junit-jupiter')
testImplementation "org.assertj:assertj-core:${versionAssertJ}"
testImplementation "org.awaitility:awaitility:${versionAwaitility}"
}

tasks.named('test') {
// Use junit platform for unit tests.
useJUnitPlatform()
}

jaxb {
// generate java code from schema
javaGen {
//generates java code for project from project schema
bttcg {
packageName = 'com.siemens.ra.cg.schema.bttcg'
//schema = file('src/main/resources/person.xsd')
schemas = files('src/main/resources/xsd/bttCgElements.xsd',
'src/main/resources/xsd/bttCgHeadwayPatterns.xsd',
'src/main/resources/xsd/bttCgATROffPatterns.xsd')
binding = file('src/main/resources/xsd/bindings/bindings.xjb')
catalog = file('src/main/resources/xsd/catalog/schema.cat')
outputDir = file('src-gen/main/java')
}
}
}

bindings/bindings.xjb:

<jxb:bindings xmlns:jxb="https://jakarta.ee/xml/ns/jaxb"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:annox="http://annox.dev.java.net"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jaxb https://jakarta.ee/xml/ns/jaxb/bindingschema_3_0.xsd"
jxb:extensionBindingPrefixes="annox" jxb:version="3.0">

<!-- Annotate the following classes with XmlRootElement -->
<jxb:bindings schemaLocation="../bttCgElements.xsd" node="/xsd:schema">
    <jxb:bindings node="xsd:complexType[@name='Timetable']">
        <annox:annotate>
            <annox:annotate
                annox:class="javax.xml.bind.annotation.XmlRootElement"
                name="timetable" />
        </annox:annotate>
    </jxb:bindings>
</jxb:bindings>

<!-- Annotate the following classes with XmlRootElement -->
<jxb:bindings schemaLocation="../bttCgHeadwayPatterns.xsd" node="/xsd:schema">
    <jxb:bindings node="xsd:complexType[@name='headwayPatterns']"> 
    	<!--
        <annox:annotate>
            <annox:annotate
                annox:class="javax.xml.bind.annotation.XmlRootElement" 
                name="headwayPatterns" /> 
        </annox:annotate> 
        -->
    </jxb:bindings> 
</jxb:bindings> 
 
<!-- Annotate the following classes with XmlRootElement -->
<jxb:bindings schemaLocation="../bttCgATROffPatterns.xsd" node="/xsd:schema">
    <jxb:bindings node="xsd:complexType[@name='AtrOffPatterns']"> 
    	<!--
        <annox:annotate>
            <annox:annotate
                annox:class="javax.xml.bind.annotation.XmlRootElement" 
                name="AtrOffPatterns" /> 
        </annox:annotate> 
        -->
    </jxb:bindings> 
</jxb:bindings>     

<jxb:globalBindings>
	<jxb:javaType name="java.util.Date" xmlType="xsd:date"
		hasNsContext="false"
		parseMethod="s2r.utilities.xml.adapters.DateAdapter.parseDate"
		printMethod="s2r.utilities.xml.adapters.DateAdapter.printDate" />

	<jxb:javaType name="java.util.Date" xmlType="xsd:time"
		hasNsContext="false"
		parseMethod="s2r.utilities.xml.adapters.TimeAdapter.parseDate"
		printMethod="s2r.utilities.xml.adapters.TimeAdapter.printDate" />

	<jxb:javaType name="java.util.Date"
		xmlType="xsd:dateTime" hasNsContext="false"
		parseMethod="s2r.utilities.xml.adapters.DateTimeAdapter.parseDate"
		printMethod="s2r.utilities.xml.adapters.DateTimeAdapter.printDate" />
</jxb:globalBindings>

</jxb:bindings>

@cashol
Copy link
Author

cashol commented Mar 16, 2022

A possible solution without annotation plugin:

build.gradle:
buildscript {
apply from: '../../common-versions.gradle'

ext {
versionIntershopJaxbPlugin = '5.1.0'
}
}

plugins {
id 'java'

id 'com.intershop.gradle.jaxb' version "${versionIntershopJaxbPlugin}"
}

sourceCompatibility = '17'
targetCompatibility = '17'

repositories {
mavenLocal()
mavenCentral()
}

def SRC_GEN_DIR = 'src-gen/main/java'

sourceSets {
main.java.srcDirs = ['src/main/java', SRC_GEN_DIR]
main.resources.srcDirs = ['src/main/resources']
test.java.srcDirs = ['src/test/java']
test.resources.srcDirs = ['src/test/resources']
}

dependencies {
implementation "org.slf4j:slf4j-api:${versionSlf4j}"
implementation "org.slf4j:slf4j-ext:${versionSlf4j}"

implementation "org.apache.commons:commons-lang3:${versionApacheCommonsLang3}"

implementation "jakarta.xml.bind:jakarta.xml.bind-api:${versionJakartaXmlBindApi}"
implementation "org.eclipse.persistence:org.eclipse.persistence.moxy:${versionEclipsePersistenceMoxy}"

// Use JUnit Jupiter for testing.
testImplementation "org.slf4j:slf4j-simple:${versionSlf4j}"
testImplementation(platform("org.junit:junit-bom:${versionJunitJupiter}"))
testImplementation('org.junit.jupiter:junit-jupiter')
testImplementation "org.assertj:assertj-core:${versionAssertJ}"
testImplementation "org.awaitility:awaitility:${versionAwaitility}"
}

tasks.named('test') {
// Use junit platform for unit tests.
useJUnitPlatform()
}

////////////////////////////////////////
// Java code generation from XML schema:

// Create src-gen folder if it does not exist:
if(!file(SRC_GEN_DIR).exists()) {
file(SRC_GEN_DIR).mkdirs()
}

// Generate java code if it does not exist:
if(file(SRC_GEN_DIR).list().size() == 0) {
jaxb {
// Generates java code from schema:
javaGen {
bttcg {
packageName = 'com.siemens.ra.cg.schema.bttcg'
schemas = files('src/main/resources/xsd/bttCgElements.xsd',
'src/main/resources/xsd/bttCgHeadwayPatterns.xsd',
'src/main/resources/xsd/bttCgATROffPatterns.xsd')
binding = file('src/main/resources/xsd/bindings/bindings.xjb')
catalog = file('src/main/resources/xsd/catalog/schema.cat')
outputDir = file('src-gen/main/java')
extension = true
}
}
}
}

// Java code generation from XML schema.
////////////////////////////////////////

bindings/bindings.xjb:

<jxb:bindings xmlns:jxb="https://jakarta.ee/xml/ns/jaxb"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jaxb https://jakarta.ee/xml/ns/jaxb/bindingschema_3_0.xsd"
jxb:extensionBindingPrefixes="xjc" jxb:version="3.0">

<jxb:globalBindings>
	<!-- 
		Causes annotation of all toplevel xsd:element declarations in schema files with @XmlRootElement:
		- Timetable
		- HeadwayPatterns
		- AtrOffPatterns
	-->
	<xjc:simple/>
	
	<jxb:javaType name="java.util.Date" xmlType="xsd:date"
		hasNsContext="false"
		parseMethod="s2r.utilities.xml.adapters.DateAdapter.parseDate"
		printMethod="s2r.utilities.xml.adapters.DateAdapter.printDate" />

	<jxb:javaType name="java.util.Date" xmlType="xsd:time"
		hasNsContext="false"
		parseMethod="s2r.utilities.xml.adapters.TimeAdapter.parseDate"
		printMethod="s2r.utilities.xml.adapters.TimeAdapter.printDate" />

	<jxb:javaType name="java.util.Date"
		xmlType="xsd:dateTime" hasNsContext="false"
		parseMethod="s2r.utilities.xml.adapters.DateTimeAdapter.parseDate"
		printMethod="s2r.utilities.xml.adapters.DateTimeAdapter.printDate" />
</jxb:globalBindings>

</jxb:bindings>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant