在 gradle 中使用 ant XJC 从 JAR 编译多个 XSD

Using ant XJC in gradle to compile multiple XSDs from JAR

所以我可以使用 ant xjc 从合同 jar 中的一个 xsd 生成 类。 如何从这个 jar 的多个模式生成 类 而无需解压缩它

 ant.xjc(package: packageName, destdir: project.ext.generatedSrcDir,    
 extension: 'true',
 schema: "jar:file:///$pathToContractJar!/MySchema.xsd")
configurations {
    jaxb
}
dependencies {
    jaxb 'com.sun.xml.bind:jaxb-xjc:2.2.6'
    jaxb 'com.sun.xml.bind:jaxb-impl:2.2.6'
    jaxb 'javax.xml.bind:jaxb-api:2.2.6'    
}
task xjc {
    def xsds = zipTree(pathToContractJar).matching { 
        include: '*.xsd' 
    }
    inputs.dir "src/main/resources/bindings"
    inputs.files xsds
    outputs.dir "$buildDir/xjc"
    doLast {
        System.setProperty('javax.xml.accessExternalSchema', 'all')
        mkdir "$buildDir/xjc/result"
        mkdir "$buildDir/xjc/xsd"

        copy {
            from xsds
            into "$buildDir/xjc/xsd"
        }
        ant.taskdef(name: 'xjc', classname: 'com.sun.tools.xjc.XJCTask', classpath: configurations.jaxb.asPath)
        ant.xjc(
            destdir: "$buildDir/xjc/result",
            package: packageName,
        ) {
            schema(dir: "$buildDir/xjc/xsd", includes: '*.xsd')
            binding(dir: "src/main/resources/bindings", includes: '*.xjb')
            arg(value: '-verbose')
        }
    }
}

我用了很长时间,但我发现移动到 Java 11 / Gradle 6 它不再起作用了。 Java 11 没有 JAXB API 因此需要将其添加为依赖项。此外,在 Gradle 中使用 Ant 调用 XJC 的机制也不起作用,可能我只是没有找到标志的神奇组合!相反,可以调用 JAR 清单中列出的 XJC main class,从而完全删除 Ant。这里的配置对我有用 Gradle 6.3 和 Java 11.

我还没有尝试将这种方法与 JAR + 绑定文件一起使用,但是根据使用信息可以使用这些参数,所以它应该可以工作!

更新:根据 this question

,使用 GlassFish 实现避免了 Sun 内部依赖性问题
sourceSets {

    generated {
        java.srcDir "$generated_dir"
    }
}

dependencies {

    compile sourceSets.generated.output

    // Generated code depends on the JAXB API, which is removed from base Java in JDK 11
    compile "org.glassfish.jaxb:jaxb-runtime:2.3.3"
    generatedCompile "org.glassfish.jaxb:jaxb-runtime:2.3.3"
}


// XJC tasks

// JAXB configuration holds classpath for running the JAXB XJC compiler
configurations {
    jaxb
}

dependencies {

    jaxb "org.glassfish.jaxb:jaxb-xjc:2.3.3"
}

// Cookie cutter function for defining multiple XJC tasks
// (not necessary if you only have a single task)!
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
        main '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
    }

    // Add a dependency on the new task so it gets invoked
    compileGeneratedJava.dependsOn tasks.getByName(taskName)
}

// Add all the XJC tasks you need

addXjcTask("xjcSchema1",
        "path/to/schema1.xsd",
        'com.example.generated.schema1',
        "$generated_dir")

addXjcTask("xjcSchema2",
        "path/to/schema2.xsd",
        'com.example.generated.schema2',
        "$generated_dir")