根据Oracle文档,SchemaGen工具作为JEP 320的一部分从JDK中删除。(
http://openjdk.java.net/jeps/320
)
Jep指出Maven工件现在提供了缺失的工具。工件的坐标在jep中是错误的,在这个问题的答案中找到了更新的坐标:
Which artifacts should I use for JAXB RI in my Maven project?
但是,缺少的是如何调用这些工具。JAXB-RIGit存储库中的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的工作版本,如下所示:
(在“real”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