build.gradle.kts 的 Jaxb。如何将它从 groovy 翻译成 kotlin?

Jaxb for build.gradle.kts. How to translate it from groovy to kotlin?

我需要将 jaxb 添加到我的 build.gradle.kts。

我有这样的build.gradlegroovy例子:

task jaxb() {
    System.setProperty('javax.xml.accessExternalSchema', 'file')
    description 'Converts xsds to classes'
    def baseGeneratedDir = file('build/generated')
    doLast {
        baseGeneratedDir.mkdirs()

        ant.taskdef(name: 'xjc', classname: 'org.jvnet.jaxb2_commons.xjc.XJC2Task', classpath: configurations.jaxb.asPath)
        ant.jaxbTargetDir = baseGeneratedDir
        ant.xjc(destdir: '${jaxbTargetDir}', package: 'com.company.ms.app.generated.model',
            schema: 'src/main/resources/xsd/CreateProduct.xsd',
            binding: 'src/main/resources/xsd/global.xjb', extension: 'true') {
            arg(value: "-Xannotate")
            arg(value: "-Xequals")
            arg(value: "-XhashCode")
            arg(value: "-XtoString")
        }
    }
}

我试图将这段代码翻译成 kotlin 用于 build.gradle.kts:

jaxb {
    xsdDir = "src/main/resources/xsd"
    xjc {
        destinationDir = "src/main/java/"
        generatePackage = "$group.build.generated.model"
        args = listOf("-Xannotate", "-Xequals", "-XhashCode", "-XtoString")
    }
}

但不是 class:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "StatusType", propOrder = {
    "statusCode",
    "statusDesc"
})
public class StatusType implements Serializable, Equals2, HashCode2, ToString2
{ ... }

我得到:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "StatusType", propOrder = {
    "statusCode",
    "statusDesc"
})
public class StatusType implements Equals2, HashCode2, ToString2
{

使用 bindingsbindingsDir 添加了可序列化:

jaxb {
    System.setProperty("javax.xml.accessExternalSchema", "file")
    xsdDir = "src/main/resources/xsd"
    bindingsDir = "src/main/resources/xsd/"
    bindings = listOf("global.xjb")
    xjc {
        taskClassname = "org.jvnet.jaxb2_commons.xjc.XJC2Task"
        destinationDir = "src/main/java/"
        generatePackage = "$group.build.generated.model"
        args = listOf("-Xannotate", "-Xequals", "-XhashCode", "-XtoString")
    }
}

文件global.xjb:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<jaxb:bindings 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:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:annox="http://annox.dev.java.net"

xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"

version="2.1">

<jaxb:globalBindings>

<xjc:serializable uid="-1"/>

</jaxb:globalBindings>

</jaxb:bindings>

对于打算将 build.gradle 转换为 Kotlins DSL (build.gradle.kts) 格式的用户对于生成远程 WSDL 的配置块和 jaxb 可以遵循以下指南:

原build.gradleGroovy代码:

configurations {
   jaxb
}
dependencies {
...
 jaxb("com.sun.xml.bind:jaxb-xjc:2.1.7")
...
}
task genJaxb {
ext.sourcesDir = "${buildDir}/generated-sources/jaxb"
ext.classesDir = "${buildDir}/classes/jaxb"
ext.schema = "https://remote-web-service/ServiceName.svc?wsdl=wsdl0"

outputs.dir classesDir

doLast() {
    project.ant {
        taskdef name: "xjc", classname: "com.sun.tools.xjc.XJCTask",
                classpath: configurations.jaxb.asPath
        mkdir(dir: sourcesDir)
        mkdir(dir: classesDir)

        xjc(destdir: sourcesDir, schema: schema,
                package: "target.package.name") {
            arg(value: "-wsdl")
            produces(dir: sourcesDir, includes: "**/*.java")
        }

        javac(destdir: classesDir, source: 1.8, target: 1.8, debug: true,
                debugLevel: "lines,vars,source",
                classpath: configurations.jaxb.asPath) {
            src(path: sourcesDir)
            include(name: "**/*.java")
            include(name: "*.java")
        }

        copy(todir: classesDir) {
            fileset(dir: sourcesDir, erroronmissingdir: false) {
                exclude(name: "**/*.java")
            }
        }
    }
  }
}
build.dependsOn genJaxb

并且上面在build.gradle.kts

中变成下面的
val jaxb by configurations.creating
dependencies {
...
   jaxb("com.sun.xml.bind:jaxb-xjc:2.1.7")
...
}
tasks.register("genJaxb") {
   ext["sourcesDir"] = "${buildDir}/generated-sources/jaxb"
   ext["classesDir"] = "${buildDir}/classes/jaxb"
   ext["schema"] = "https://remote-web-service/ServiceName.svc?wsdl=wsdl0"

   ext["classesDir"]?.let { outputs.dir(it) }

  doLast {
    ant.withGroovyBuilder {
        "taskdef"(
            "name" to "xjc", "classname" to "com.sun.tools.xjc.XJCTask",
            "classpath" to jaxb.asPath
        )
        ext["sourcesDir"]?.let { mkdir(it) }
        ext["classesDir"]?.let { mkdir(it) }

        "xjc"(
            "destdir" to ext["sourcesDir"], "schema" to ext["schema"],
            "package" to "target.package.name"
        ) {
            "arg"("value" to "-wsdl")
            "produces"("dir" to ext["sourcesDir"], "includes" to "**/*.java")
        }

        "javac"(
            "destdir" to ext["classesDir"], "source" to 1.8, "target" to 1.8, "debug" to true,
            "debugLevel" to "lines,vars,source", "classpath" to jaxb.asPath
        ) {
            "src"("path" to ext["sourcesDir"])
            "include"("name" to "**/*.java")
            "include"("name" to "*.java")
        }

        "copy"("todir" to ext["classesDir"]) {
            "fileset"("dir" to ext["sourcesDir"], "erroronmissingdir" to false) {
                "exclude"("name" to "**/*.java")
            }
        }
    }
  }
}
tasks.build {
   dependsOn("genJaxb")
}