gradlejaxbxjc

How to run jaxb xjc task with gradle?


I picked up 3 gradle plugins for jaxb/xjc and none of them worked for me.

For each case, I've put the maven model xsd maven-4.0.0.xsd https://maven.apache.org/xsd/maven-4.0.0.xsd in src/main/xsd. (it's just an example, any other xsd will do)

http://github.com/stianh/gradle-jaxb-plugin

This plugin looks for xsd files in src/<source set>/xsd/**/*.xsd.

Here is the build.gradle:

    apply plugin: 'jaxb'
    
    dependencies {
      jaxb 'com.sun.xml.bind:jaxb-xjc:2.2.4-1'
    }
    
    buildscript {
      repositories {
        mavenCentral()
      }
      dependencies {
        classpath 'no.entitas.gradle.jaxb:gradle-jaxb-plugin:2.0'
      }
    }

Error:

* What went wrong:
A problem occurred evaluating root project 'test-gradle'.
> Failed to apply plugin [id 'jaxb']
   > Could not find method add() for arguments [jaxb, no.entitas.gradle.jaxb.plugin.JaxbPlugin$_apply_closure1@62c72501] on configuration container.

https://github.com/hibernate/gradle-xjc-plugin

Here is the build.gradle:

    buildscript {
        repositories {
            mavenCentral()
        }
        dependencies {
            classpath 'org.hibernate.build.gradle:gradle-xjc-plugin:1.0.2.Final'
        }
    }
    
    apply plugin: 'org.hibernate.build.gradle.xjc'
    
    dependencies {
        xjc 'org.glassfish.jaxb:jaxb-xjc:2.2.11'
        xjc 'org.jvnet.jaxb2_commons:jaxb2-basics:0.9.3'
        xjc 'org.jvnet.jaxb2_commons:jaxb2-basics-ant:0.9.3'
    }
    
    xjc {
        xjcTaskName = 'org.jvnet.jaxb2_commons.xjc.XJC2Task'
        // access the schemas NamedDomainObjectContainer
        schemas {
    
            // and add a new SchemaDescriptor to it under the name 'cfg'
            cfg {
                // and now, configure the SchemaDescriptor
                xsd = file( 'src/main/xsd/maven-4.0.0.xsd')
            }
        }
    }

Error:

* What went wrong:
Neither path nor baseDir may be null or empty string. path='null' basedir='C:\Users\xxxxx\eclipse-workspace\test-gradle'

https://github.com/IntershopCommunicationsAG/jaxb-gradle-plugin

Here is the build.gradle:

    plugins {
        id 'java'
        id 'com.intershop.gradle.jaxb' version '5.1.0'
    }
    
    jaxb {
        javaGen {
            posConfig {
                packageName = 'org.apache.maven.model'
                schema = file('src/main/xsd/maven-4.0.0.xsd')
            }
        }
    }
    
    repositories {
        mavenCentral()
    }
    
    dependencies {
        jaxb 'com.sun.xml.bind:jaxb-xjc:3.0.1'
        jaxb 'com.sun.xml.bind:jaxb-jxc:3.0.1'
        jaxb 'com.sun.xml.bind:jaxb-impl:3.0.1'
        jaxb 'com.sun.xml.bind:jaxb-core:3.0.1'
        jaxb 'org.glassfish.jaxb:jaxb-runtime:3.0.1'
        jaxb 'jakarta.xml.bind:jakarta.xml.bind-api:3.0.1'
        jaxb 'jakarta.activation:jakarta.activation-api:2.0.1'
    }

Error:

* What went wrong:
An exception occurred applying plugin request [id: 'com.intershop.gradle.jaxb', version: '5.1.0']
> Failed to apply plugin [id 'com.intershop.gradle.jaxb']
   > Could not generate a proxy class for class com.intershop.gradle.jaxb.extension.JaxbExtension.

After more research, I found 2 other plugins that worked for me, but I'm still looking for a solution without plugin.

The jacobono/gradle-jaxb-plugin uses relative directories and creates the destination directory :

    buildscript {
      repositories {
        maven {
          url "https://plugins.gradle.org/m2/"
        }
      }
      dependencies {
        classpath "com.github.jacobono:gradle-jaxb-plugin:1.3.5"
      }
    }
    
    apply plugin: "com.github.jacobono.jaxb"
    
    repositories {
      mavenCentral()
    }
    
    sourceSets {
        main {
            java {
                srcDirs = ["${project.buildDir}/generated-sources/xjc",'src/main/java']
            }
            resources {
                srcDirs = ['src/main/resources']
            }
        }
    }
    
    dependencies {
      jaxb 'org.jvnet.jaxb2_commons:jaxb2-basics-ant:1.11.1'
      jaxb 'org.jvnet.jaxb2_commons:jaxb2-basics:1.11.1'
      jaxb 'org.jvnet.jaxb2_commons:jaxb2-basics-annotate:1.1.0'
      jaxb 'org.glassfish.jaxb:jaxb-xjc:2.2.11'
      jaxb 'org.glassfish.jaxb:jaxb-runtime:2.2.11'
      jaxb 'javax.xml.bind:jaxb-api:2.2.11'
      jaxb 'org.slf4j:slf4j-nop:1.7.30'
    }
    
    jaxb {
      xsdDir = "src/main/xsd"
      xjc {
         taskClassname        = "org.jvnet.jaxb2_commons.xjc.XJC2Task"
         generatePackage      = "org.apache.maven.model"
         destinationDir       = "build/generated-sources/xjc"
         args                 = ["-Xinheritance", "-Xannotate"]
      }
    }
    
    compileJava.dependsOn 'xjc'

The rackerlabs/gradle-jaxb-plugin uses absolute directories and creates the destination directory only if it's the default one ${project.buildDir}/generated-sources/xjc:

    buildscript {
      repositories {
        maven {
          url "https://plugins.gradle.org/m2/"
        }
      }
      dependencies {
        classpath 'gradle.plugin.org.openrepose:gradle-jaxb-plugin:2.5.0'
      }
    }
    
    apply plugin: "org.openrepose.gradle.plugins.jaxb"
    
    repositories {
      mavenCentral()
    }
    
    sourceSets {
        main {
            java {
                srcDirs = ["${project.buildDir}/generated-sources/xjc",'src/main/java']
            }
            resources {
                srcDirs = ['src/main/resources']
            }
        }
    }
    
    dependencies {
      jaxb 'org.jvnet.jaxb2_commons:jaxb2-basics-ant:1.11.1'
      jaxb 'org.jvnet.jaxb2_commons:jaxb2-basics:1.11.1'
      jaxb 'org.jvnet.jaxb2_commons:jaxb2-basics-annotate:1.1.0'
      jaxb 'org.glassfish.jaxb:jaxb-xjc:2.2.11'
      jaxb 'org.glassfish.jaxb:jaxb-runtime:2.2.11'
      jaxb 'javax.xml.bind:jaxb-api:2.2.11'
      jaxb 'org.slf4j:slf4j-nop:1.7.30'
    }
    
    jaxb {
      xsdDir = "${project.projectDir}/src/main/xsd"
      xjc {
         generateEpisodeFiles = false
         taskClassname        = "org.jvnet.jaxb2_commons.xjc.XJC2Task"
         generatePackage      = "org.apache.maven.model"
         args                 = ["-Xinheritance", "-Xannotate"]
      }
    }
    
    compileJava.dependsOn 'xjc'

Command : gradle build


Solution

  • Here is an example without using specific plugins (just java), and v4 of jaxb-xjc:

    (config expects maven-4.0.0.xsd to be in src/main/resources)

    plugins {
        id 'java'
    }
    
    group 'com.example.xjc-demo'
    archivesBaseName = "xjcdemo"
    version '1.0.0-SNAPSHOT'
    
    repositories {
        mavenCentral()
    }
    
    def generated_dir = "src/main/java"
    sourceSets {
        generated {
            java.srcDir "$generated_dir"
        }
    }
    
    // JAXB configuration holds classpath for running the JAXB XJC compiler
    configurations {
        jaxb
    }
    
    dependencies {
        jaxb "org.glassfish.jaxb:jaxb-xjc:4.0.1"
        jaxb "org.glassfish.jaxb:jaxb-runtime:4.0.1"
    
        // The next two dependencies are not necessary for generating code, only when running the code:
        // Generated code depends on the JAXB API, which is removed from base Java in JDK 11, and therefore needs to be added
        implementation "org.glassfish.jaxb:jaxb-runtime:4.0.1"
        implementation 'jakarta.xml.bind:jakarta.xml.bind-api:4.0.0' // In JAXB v3, 'javax.xml.bind' was moved to 'jakarta.xml.bind'
    }
    
    // Cookie cutter function for defining multiple XJC tasks
    def addXjcTask(taskName, schema, pkg, dest) {
    
        // If you haven't already, create the generated output dir before running XJC or it will fail
        file(dest).mkdirs()
    
    
        // The main XJC task, calls XJCFacade which is the entry point of the XJC JAR
        tasks.create(name: taskName, type: JavaExec) {
            classpath configurations.jaxb
            mainClass = 'com.sun.tools.xjc.XJCFacade'
    
            // To explore available args, download the XJC JAR and run java -jar jaxb-xjc.jar --help
            args schema, "-p", pkg, "-d", dest, "-no-header"
        }
    }
    
    // Add all the XJC tasks you need
    addXjcTask("maven-xsd",
            "src/main/resources/maven-4.0.0.xsd",
            "${archivesBaseName}.my_maven_package_name",
            "$generated_dir")
    
    tasks.register("generateSources") {
        description "Generate Java sources fram XSD files"
        dependsOn tasks.getByName("maven-xsd")
    }
    

    Run with ./gradlew generateSources (verified with Gradle v7.4.2).