根据 Oracle 文档,schemagen 工具已作为 JEP 320 ( http://openjdk.java.net/jeps/320 ) 的一部分从 JDK 中删除。该 JEP 指向现在提供缺失工具的 Maven 工件。JEP 中工件的坐标是错误的,更新后的坐标可以在这个问题的答案中找到: 我应该在我的 Maven 项目中为 JAXB RI 使用哪些工件?
然而,缺少的是如何调用这些工具。在 JAXB-RI Git 存储库中的 JEP 中有指向的 shell 脚本。然而,这些脚本仍然没有记录并且难以调用。该 git repo 中的构建说明表明它是使用标准“mvn clean install”构建的,但是不会产生与此处文档中使用的“bin”文件夹匹配的输出结构:https ://javaee.github.io /jaxb-v2/doc/user-guide/ch04.html#tools-schemagen
理想情况下,我想从 Gradle 运行 schemagen,避免使用 shell 脚本,因为它们不是从 maven 依赖项中获得的。
我目前的尝试,改编自一个名为旧 schemagen.exe 的工作版本,如下所示:
(“真正的”build.gradle 文件中有更多内容来指定我的应用程序的依赖项等)
configurations {
schemagenTool
}
dependencies {
schemagenTool "org.glassfish.jaxb:jaxb-jxc:${jaxbVersion}"
}
task schemaGen(type: Exec, dependsOn: [compileJava,configurations.schemaGenTool]) {
workingDir projectDir
executable 'java'
doFirst {
file('build/Schemas').mkdirs()
args '--module-path', "${configurations.schemaGenTool.asPath}",
'-m', 'jaxb.jxc/com.sun.tools.jxc.SchemaGeneratorFacade',
// Note: automatic module name is NOT com.sun.tool.jxc
// as documented
// Args to schemagen (these worked for the schemagen.exe)
'-d', 'build/Schemas',
'-classpath', "${compileJava.destinationDir}${File.pathSeparator}${configurations.compile.asPath}",
"src/main/java/path/to/my/ModelClass.java"
//println "\nschemagen: ${args.join(' ')}\n"
}
doLast {
// Above creates "build/Schemas/schema1.xsd" (despite printing a different path!)
// Rename it
def destFile = file('build/Schemas/model.xsd')
destFile.delete()
if (!file('build/Schemas/schema1.xsd').renameTo(destFile)) {
throw new GradleException("Failed to write new build/Schemas/model.xsd")
}
}
}
但是,这会导致错误:
Error occurred during initialization of boot layer
java.lang.module.ResolutionException: Modules jaxb.runtime and jaxb.core export package com.sun.xml.bind.marshaller to module relaxngDatatype