rackerlabs / gradle-jaxb-plugin

Gradle plugin to ease projects that use xsds and the ant jaxb task
GNU General Public License v2.0
34 stars 23 forks source link

output separate schemas to separate packages #5

Closed aplatypus closed 7 years ago

aplatypus commented 7 years ago

Hello,

I am posting as either a suggestion or to as how I can achieve the following (without restructuring code and directories, etc).

We have a set of XSD schemas in a parent resource folder

We want to keep these separate. So that the xjc could generate schema/** and produce something like this as a package structure:

I have attempted most logical variations. The mose successful is the example below. If I comment-out the second closure, the "consist" schema classes are generated. When I uncomment the "response" classes are generated.

It isn't automatic of course. I thought I'd ask here first to see if you know HOW we might accomplish this kind of thing?

jaxb
{
    xsdDir = "${project.rootDir}/ManifestLibrary/src/main/resources/schemas/consist"
    xjc
    {
            println "  -- ${project} xjc consist task";
        println "";
            destinationDir="src/generated/java"
        generatePackage = "ex.manifest.schema.consist"
    }

        /****/
    xsdDir = "${project.rootDir}/ManifestLibrary/src/main/resources/schemas/response";
    xjc
    {
            println "  -- ${project} xjc  response task";
       println "";
           destinationDir="src/generated/java"
           generatePackage = "ex.manifest.schema.response"
    }
       /****/
}

Are there alternatives? Can I put the individual blocks inside some other structure so they are called separately?

Kind regards, Will

pvik commented 7 years ago

I had a similar problem. I solved this by having a seperate .gradle file for each schema file that has to be generated and calling the xjc task in each of the schema gradle files.

In your case, add the following to your build.gradle

task genConsistSchema(type: GradleBuild) {
    buildFile = 'gen-schema-consist.gradle'
    tasks = ['xjc']
}

task genResponseSchema(type: GradleBuild) {
    buildFile = 'gen-schema-response.gradle'
    tasks = ['xjc']
}

task genAllSchema {
    description 'Generate POJOs for all schemas'
    doFirst {
        println 'Generating All Schemas'
    }

    dependsOn genConsistSchema, genResponseSchema
}

and create gen-schema-consist.gradle as follows

buildscript {
    repositories {
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }
    dependencies {
        classpath "gradle.plugin.org.openrepose:gradle-jaxb-plugin:2.2.3"
    }
}

apply plugin: "org.openrepose.gradle.plugins.jaxb"

jaxb {
    xsdDir = "${project.rootDir}/ManifestLibrary/src/main/resources/schemas/consist"
    xjc {

        destinationDir     = "src/main/java"
        taskClassname      = "org.jvnet.jaxb2_commons.xjc.XJC2Task"
        generatePackage    = "ex.manifest.schema.consist"
        args               = ["-Xinheritance", "-Xannotate"]
    }
}

repositories {
    mavenCentral()
}

dependencies {
    jaxb (
            'org.jvnet.jaxb2_commons:jaxb2-basics-ant:0.11.1',
            'org.jvnet.jaxb2_commons:jaxb2-basics:0.11.1',
            'org.jvnet.jaxb2_commons:jaxb2-basics-annotate:1.0.2',
            'com.sun.xml.bind:jaxb-xjc:2.2.7',
            'com.sun.xml.bind:jaxb-impl:2.2.7',
            'javax.xml.bind:jaxb-api:2.2.7'
    )
}

and gen-schema-response.gradle as follows:

buildscript {
    repositories {
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }
    dependencies {
        classpath "gradle.plugin.org.openrepose:gradle-jaxb-plugin:2.2.3"
    }
}

apply plugin: "org.openrepose.gradle.plugins.jaxb"

jaxb {
    xsdDir = "${project.rootDir}/ManifestLibrary/src/main/resources/schemas/response";
    xjc {
        destinationDir     = "src/main/java"
        taskClassname      = "org.jvnet.jaxb2_commons.xjc.XJC2Task"
        generatePackage    = "ex.manifest.schema.response"
        args               = ["-Xinheritance", "-Xannotate"]
    }
}

repositories {
    mavenCentral()
}

dependencies {
    jaxb (
            'org.jvnet.jaxb2_commons:jaxb2-basics-ant:0.11.1',
            'org.jvnet.jaxb2_commons:jaxb2-basics:0.11.1',
            'org.jvnet.jaxb2_commons:jaxb2-basics-annotate:1.0.2',
            'com.sun.xml.bind:jaxb-xjc:2.2.7',
            'com.sun.xml.bind:jaxb-impl:2.2.7',
            'javax.xml.bind:jaxb-api:2.2.7'
    )
}

This method requires you to create a seperate gradle file for each schema you want generated into a seperate package.

However, this allows me to geerate all the schema from the genAllSchema task.

Hope this helps!

I am fairly new to using gradle for my build management, so if there is a better way to do this please let me know. (For example, if a single plugin can be called multiple times from a single task, we should be able to solve this from a single task in the build.gradle file).

Lesurglesum commented 7 years ago

You should be able to do what you want with binding files and don't specify the package in your gradle file.

jaxb {
    xsdDir = "${project.rootDir}/schemes"
    bindingsDir = "${project.rootDir}/schemes/bindings/java"
    bindings = ["binding1.xjb", "binding2.xjb"]

    xjc {
        taskClassname = "org.jvnet.jaxb2_commons.xjc.XJC2Task"
        header = false
    }
}

In binding1.xjb:

[...]
    <jxb:schemaBindings>
        <jxb:package name="ex.manifest.schema.consist"/>
    </jxb:schemaBindings>
[...]

In binding2.xjb:

[...]
    <jxb:schemaBindings>
        <jxb:package name="ex.manifest.schema.response"/>
    </jxb:schemaBindings>
[...]

http://docs.oracle.com/cd/E17802_01/webservices/webservices/docs/1.5/tutorial/doc/JAXBUsing4.html

wdschei commented 7 years ago

@aplatypus,

Did the suggestions @pvik and/or @Muselgrusel do what you needed?

Kindest regards, Bill

wdschei commented 7 years ago

Given the available workarounds, we are going to consider this issue closed.

CalamarBicefalo commented 6 years ago

I might be missing something but doing what @Muselgrusel proposed, although it technically picks up the config. It still errors complaining about duplicated types. E.g.:

[ant:xjc] [ERROR] 'AType' is already defined
[ant:xjc]   line 3104 of file:/some.xsd
[ant:xjc] 
[ant:xjc] [ERROR] (related to above error) the first definition appears here
[ant:xjc]   line 2629 of file:/another.xsd

some.xsd and another.xsd have on binding each configuring different packages though.

Am I missing something obvious here?