繁体   English   中英

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

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

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

我有这样的 build.gradle groovy 示例:

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
{
  • 未添加可序列化:-(

  • 此外,还生成了 schema/episodes/CreateProduct.xsd.episode 文件。 我怎样才能关闭它的生成?

  • 如何尽可能准确地将 jaxb 的 groovy 代码翻译成 kotlin?

For users aiming to convert the build.gradle to the Kotlins DSL ( build.gradle.kts ) format for the configurations block & jaxb to generate remote WSDL can follow the guide below:

原厂build.gradle Groovy码:

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")
}

Serializable 是使用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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM