schemagen Ant任务无法使用gradle正常运行

时间:2019-05-02 12:32:36

标签: java gradle ant jaxb schemagen

由于JAXB中的schemagen工具不再是JDK 11的一部分,因此我想使用gradle设置一个项目,该项目通过可用的ant任务提供了schemagen工具的功能。 虽然,我让它运行ant任务的行为不符合预期。特别是创建的架构与源代码注释不匹配。

此外,总会抛出一个异常,我无法解决。

我在带有bash外壳的Windows 10计算机上使用2.3.2版的JDK 11,Gradle 5.4.1和JAXB。

项目看起来像这样(gradle java项目):

Movie.javasrc/main/java/movie内:

package movie;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Movie {

    @XmlElement
    public String name;

    @XmlAttribute
    public String genre;

}

build.gradle

plugins {
    id 'java'
    id 'application'
}

repositories {
    jcenter()
}

configurations {
    schemagen
}

dependencies {
    implementation group: 'org.glassfish.jaxb', name: 'jaxb-runtime', version: '2.3.2'
    schemagen group: 'org.glassfish.jaxb', name: 'jaxb-jxc', version: '2.3.2'
}

def srcDir = file("src/main/java/movie")
def schemaDir = file("$buildDir/schema")

task schemagen {
    doLast {
        ant.taskdef(name: 'schemagen', classname: 'com.sun.tools.jxc.SchemaGenTask', classpath: configurations.schemagen.asPath)
        schemaDir.mkdirs()
        ant.schemagen(
            srcdir: srcDir,
            destdir: schemaDir,
            includeAntRuntime: false,
            debug: true,
            verbose: true)
    }
}

现在,当我运行gradle schemagen --stacktrace时,控制台上将显示以下输出:

> Task :schemagen FAILED
[ant:schemagen] anomalous package-info.java path: package-info.java
[ant:schemagen] [parsing started SimpleFileObject[...\src\main\java\movie\Movie.java]]
[ant:schemagen] [parsing completed 0ms]
[ant:schemagen] [parsing started SimpleFileObject[...\src\main\java\movie\package-info.java]]
[ant:schemagen] [parsing completed 0ms]
[ant:schemagen] [loading /modules/jdk.jconsole/module-info.class]
...many more
[ant:schemagen] [search path for source files: ...\src\main\java\movie]
[ant:schemagen] [search path for class files: C:\Program Files\Amazon Corretto\jdk11.0.3_7\lib\modules,...\build\schema]
[ant:schemagen] [loading /modules/java.base/java/lang/Object.class]
[ant:schemagen] [loading /modules/java.base/java/lang/String.class]
[ant:schemagen] [loading /modules/java.base/java/lang/Deprecated.class]
[ant:schemagen] [loading /modules/java.base/java/lang/annotation/Retention.class]
[ant:schemagen] [loading /modules/java.base/java/lang/annotation/RetentionPolicy.class]
[ant:schemagen] [loading /modules/java.base/java/lang/annotation/Target.class]
[ant:schemagen] [loading /modules/java.base/java/lang/annotation/ElementType.class]
[ant:schemagen] Round 1:
[ant:schemagen]         input files: {movie.Movie}
[ant:schemagen]         annotations: [XmlRootElement, XmlElement, XmlAttribute]
[ant:schemagen]         last round: false
[ant:schemagen] [loading /modules/java.base/java/lang/Character.class]
...many more
[ant:schemagen] [loading /modules/java.base/java/util/Collection.class]
[ant:schemagen] Processor com.sun.tools.jxc.ap.SchemaGenerator matches [XmlRootElement, XmlAttribute, XmlElement] and returns false.
[ant:schemagen] Round 2:
[ant:schemagen]         input files: {}
[ant:schemagen]         annotations: []
[ant:schemagen]         last round: true
[ant:schemagen] [total 297ms]

FAILURE: Build failed with an exception.

* Where:
Build file '...\build.gradle' line: 26

* What went wrong:
Execution failed for task ':schemagen'.
> schema generation failed

* Try:
Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Exception is:
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':schemagen'.
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter$3.accept(ExecuteActionsTaskExecuter.java:151)
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter$3.accept(ExecuteActionsTaskExecuter.java:148)
        at org.gradle.internal.Try$Failure.ifSuccessfulOrElse(Try.java:191)
        ...many more
Caused by: : schema generation failed
        at com.sun.tools.jxc.ApBasedTask.compile(ApBasedTask.java:142)
        at com.sun.tools.jxc.SchemaGenBase.compile(SchemaGenBase.java:228)
        at org.apache.tools.ant.taskdefs.Javac.execute(Javac.java:1130)
        at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:293)
        at jdk.internal.reflect.GeneratedMethodAccessor314.invoke(Unknown Source)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
        ...many more

(缩短了输出重定向文件系统路径和不必要的信息)

尽管任务失败,但是创建了一个架构,schema1.xsd

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:complexType name="movie">
    <xs:sequence>
      <xs:element name="name" type="xs:string" minOccurs="0"/>
      <xs:element name="genre" type="xs:string" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

在这里@XmlAttribute-注释将被忽略,流派只是作为元素包含在内。

我原本希望这样:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:complexType name="movie">
    <xs:sequence>
      <xs:element name="name" type="xs:string" minOccurs="0"/>
    </xs:sequence>
    <xs:extension base="xs:string">
        <xs:attribute name="genre" type="xs:boolean" use="required"/>
    </xs:extension>
  </xs:complexType>
</xs:schema>

有人成功建立了这样的项目,并从schemagen ant任务中获得了有效的架构吗?

1 个答案:

答案 0 :(得分:0)

对于任何寻求解决方案的人。这是由于schemagen对类路径没有必需的依赖关系。

在schemagen依赖性配置中,添加所需的依赖性。对于@XmlRootElement,依赖项是bind-api。

然后使用classpath {pathelement(path :)}设置schemagen的类路径,如下所示。

    dependencies { 
    schemagen (
        'jakarta.xml.bind:jakarta.xml.bind-api:3.0.0-RC3',
        'jakarta.activation:jakarta.activation-api:2.0.0-RC3',
        'com.sun.xml.bind:jaxb-jxc:3.0.0-M4',
        'com.sun.xml.bind:jaxb-osgi:3.0.0-M4',
        'javax.xml.bind:jaxb-api:2.4.0-b180830.0359')
    }
    task schemagen {
        doLast {
            ant.taskdef(name: 'schemagen', classname: 'com.sun.tools.jxc.SchemaGenTask', classpath: configurations.schemagen.asPath)
            schemaDir.mkdirs()
            ant.schemagen(
                srcdir: srcDir,
                destdir: schemaDir,
                includeAntRuntime: false,
                debug: true,
                verbose: true){
                schema(file: "CustomSchema.xsd", namespace: "")
                classpath {
                    pathelement(path: configurations.schemagen.asPath)
                }
        }
    }