如何修改Java 9的ANT build.xml中的xjc块?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何修改Java 9的ANT build.xml中的xjc块?相关的知识,希望对你有一定的参考价值。

我有一个build.xml文件,它使用xjc生成一些Java类。在Java 1.8中,一切都很好,但更新到Java 9会导致以下错误:

xjc-gen-boxes:
  [xjc] C:UsersdevDocumentsDevccce2uildgensrccooxesv2 is not found and thus excluded from the dependency check
  [xjc] Compiling file:/C:/Users/dev/Documents/Dev/bc/bcce2/co/src/co/kat/install/boxes/Box.xsd

BUILD FAILED
C:UsersdevDocumentsDevccce2couild.xml:720: java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException
    at com.sun.tools.xjc.reader.xmlschema.bindinfo.AnnotationParserFactoryImpl.create(AnnotationParserFactoryImpl.java:79)
    at com.sun.xml.xsom.impl.parser.NGCCRuntimeEx.createAnnotationParser(NGCCRuntimeEx.java:359)
    at com.sun.xml.xsom.impl.parser.state.annotation.action0(annotation.java:49)
    at com.sun.xml.xsom.impl.parser.state.annotation.enterElement(annotation.java:74)
    at com.sun.xml.xsom.impl.parser.state.NGCCRuntime.sendEnterElement(NGCCRuntime.java:378)
    at com.sun.xml.xsom.impl.parser.state.NGCCHandler.spawnChildFromEnterElement(NGCCHandler.java:74)
    at com.sun.xml.xsom.impl.parser.state.complexType.enterElement(complexType.java:429)
    at com.sun.xml.xsom.impl.parser.state.NGCCRuntime.sendEnterElement(NGCCRuntime.java:378)
    at com.sun.xml.xsom.impl.parser.state.NGCCHandler.revertToParentFromEnterElement(NGCCHandler.java:111)
    at com.sun.xml.xsom.impl.parser.state.foreignAttributes.enterElement(foreignAttributes.java:51)
    at com.sun.xml.xsom.impl.parser.state.NGCCRuntime.sendEnterElement(NGCCRuntime.java:378)
    at com.sun.xml.xsom.impl.parser.state.NGCCHandler.spawnChildFromEnterElement(NGCCHandler.java:74)
    at com.sun.xml.xsom.impl.parser.state.complexType.enterElement(complexType.java:202)
    at com.sun.xml.xsom.impl.parser.state.NGCCRuntime.sendEnterElement(NGCCRuntime.java:378)
    at com.sun.xml.xsom.impl.parser.state.complexType.enterElement(complexType.java:610)
    at com.sun.xml.xsom.impl.parser.state.NGCCRuntime.sendEnterElement(NGCCRuntime.java:378)
    at com.sun.xml.xsom.impl.parser.state.complexType.enterElement(complexType.java:457)
    at com.sun.xml.xsom.impl.parser.state.NGCCRuntime.sendEnterElement(NGCCRuntime.java:378)
    at com.sun.xml.xsom.impl.parser.state.complexType.enterElement(complexType.java:175)
    at com.sun.xml.xsom.impl.parser.state.NGCCRuntime.sendEnterElement(NGCCRuntime.java:378)
    at com.sun.xml.xsom.impl.parser.state.complexType.enterElement(complexType.java:537)
    at com.sun.xml.xsom.impl.parser.state.NGCCRuntime.sendEnterElement(NGCCRuntime.java:378)
    at com.sun.xml.xsom.impl.parser.state.complexType.enterElement(complexType.java:549)
    at com.sun.xml.xsom.impl.parser.state.NGCCRuntime.startElement(NGCCRuntime.java:219)
    at java.xml/org.xml.sax.helpers.XMLFilterImpl.startElement(XMLFilterImpl.java:551)
    at com.sun.tools.xjc.util.SubtreeCutter.startElement(SubtreeCutter.java:104)
    at com.sun.tools.xjc.reader.ExtensionBindingChecker.startElement(ExtensionBindingChecker.java:144)
    at java.xml/org.xml.sax.helpers.XMLFilterImpl.startElement(XMLFilterImpl.java:551)
    at com.sun.tools.xjc.reader.xmlschema.parser.IncorrectNamespaceURIChecker.startElement(IncorrectNamespaceURIChecker.java:113)
    at java.xml/org.xml.sax.helpers.XMLFilterImpl.startElement(XMLFilterImpl.java:551)
    at com.sun.tools.xjc.reader.xmlschema.parser.CustomizationContextChecker.startElement(CustomizationContextChecker.java:188)
    at java.xml/org.xml.sax.helpers.XMLFilterImpl.startElement(XMLFilterImpl.java:551)
    at com.sun.tools.xjc.ModelLoader$SpeculationChecker.startElement(ModelLoader.java:455)
    at java.xml/org.xml.sax.helpers.XMLFilterImpl.startElement(XMLFilterImpl.java:551)
    at com.sun.tools.xjc.reader.internalizer.VersionChecker.startElement(VersionChecker.java:98)
    at java.xml/org.xml.sax.helpers.XMLFilterImpl.startElement(XMLFilterImpl.java:551)
    at java.xml/com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:509)
    at java.xml/com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:374)
    at java.xml/com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2706)
    at java.xml/com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:601)
    at java.xml/com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:112)
    at java.xml/com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:531)
    at java.xml/com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:885)
    at java.xml/com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:821)
    at java.xml/com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
    at java.xml/com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
    at java.xml/com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:639)
    at java.xml/org.xml.sax.helpers.XMLFilterImpl.parse(XMLFilterImpl.java:357)
    at com.sun.xml.xsom.parser.JAXPParser.parse(JAXPParser.java:115)
    at com.sun.tools.xjc.ModelLoader$2.parse(ModelLoader.java:479)
    at com.sun.tools.xjc.ModelLoader$XMLSchemaParser.parse(ModelLoader.java:262)
    at com.sun.xml.xsom.impl.parser.NGCCRuntimeEx.parseEntity(NGCCRuntimeEx.java:337)
    at com.sun.xml.xsom.impl.parser.ParserContext.parse(ParserContext.java:124)
    at com.sun.xml.xsom.parser.XSOMParser.parse(XSOMParser.java:183)
    at com.sun.tools.xjc.ModelLoader.createXSOMSpeculative(ModelLoader.java:496)
    at com.sun.tools.xjc.ModelLoader.loadXMLSchema(ModelLoader.java:366)
    at com.sun.tools.xjc.ModelLoader.load(ModelLoader.java:167)
    at com.sun.tools.xjc.ModelLoader.load(ModelLoader.java:113)
    at com.sun.tools.xjc.XJC2Task._doXJC(XJC2Task.java:496)
    at com.sun.tools.xjc.XJC2Task.doXJC(XJC2Task.java:443)
    at com.sun.tools.xjc.XJC2Task.execute(XJC2Task.java:369)
    at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:293)
    at jdk.internal.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
    at org.apache.tools.ant.Task.perform(Task.java:348)
    at org.apache.tools.ant.Target.execute(Target.java:435)
    at org.apache.tools.ant.Target.performTasks(Target.java:456)
    at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1405)
    at org.apache.tools.ant.helper.SingleCheckExecutor.executeTargets(SingleCheckExecutor.java:38)
    at org.apache.tools.ant.Project.executeTargets(Project.java:1260)
    at org.apache.tools.ant.taskdefs.Ant.execute(Ant.java:441)
    at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:293)
    at jdk.internal.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
    at org.apache.tools.ant.Task.perform(Task.java:348)
    at org.apache.tools.ant.Target.execute(Target.java:435)
    at org.apache.tools.ant.Target.performTasks(Target.java:456)
    at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1405)
    at org.apache.tools.ant.helper.SingleCheckExecutor.executeTargets(SingleCheckExecutor.java:38)
    at org.apache.tools.ant.Project.executeTargets(Project.java:1260)
    at org.apache.tools.ant.taskdefs.Ant.execute(Ant.java:441)
    at org.apache.tools.ant.taskdefs.CallTarget.execute(CallTarget.java:105)
    at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:293)
    at jdk.internal.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
    at org.apache.tools.ant.Task.perform(Task.java:348)
    at org.apache.tools.ant.Target.execute(Target.java:435)
    at org.apache.tools.ant.Target.performTasks(Target.java:456)
    at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1405)
    at org.apache.tools.ant.Project.executeTarget(Project.java:1376)
    at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
    at org.apache.tools.ant.Project.executeTargets(Project.java:1260)
    at org.apache.tools.ant.Main.runBuild(Main.java:853)
    at org.apache.tools.ant.Main.startAnt(Main.java:235)
    at org.apache.tools.ant.launch.Launcher.run(Launcher.java:285)
    at org.apache.tools.ant.launch.Launcher.main(Launcher.java:112)
Caused by: java.lang.ClassNotFoundException: javax.xml.bind.JAXBException
    at org.apache.tools.ant.AntClassLoader.findClassInComponents(AntClassLoader.java:1388)
    at org.apache.tools.ant.AntClassLoader.findClass(AntClassLoader.java:1337)
    at org.apache.tools.ant.AntClassLoader.loadClass(AntClassLoader.java:1089)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:496)
    ... 102 more

到达build.xml文件中的此点时发生错误:

<taskdef name="xjc" classname="com.sun.tools.xjc.XJC2Task">
    <classpath>
        <pathelement path="${basedir}/lib/webservices-tools.jar"/>
        <pathelement path="${basedir}/lib/webservices-rt.jar"/>
        <pathelement path="${basedir}/lib/jaxb-commons-lang-plugin-2.2.jar" />
        <pathelement path="${basedir}/lib/commons-lang-2.4.jar" />
    </classpath>
  </taskdef>

<target name="xjc-gen-boxes">
    <xjc destdir="${path.out.gensrc}">
          <arg value="-Xcommons-lang"/>
          <schema dir="${boxes.schema.home.translated}" includes="*.xsd"/>
          <produces dir="${path.out.gensrc}/co/boxes/v2"/>
    </xjc>
</target>

我尝试过利用JDK目录中的xjc.exe,但基线和Java 9构建之间的比较显示文件丢失或不完整。这是我用它做的,如果它有帮助:

<target name="xjc-gen-boxes">   
    <mkdir dir="${path.out.gensrc}/co/boxes/v2"/> 
    <exec executable="${env.JAVA_HOME}/bin/xjc.exe">
        <arg value="${boxes.schema.home.translated}/*.xsd"/>
        <arg value="-d"/>
        <arg value="${path.out.gensrc}"/>
        <arg value="-classpath"/>
        <arg value="${basedir}/lib/commons-lang-2.4.jar"/>
        <arg value="${basedir}/lib/jaxb-commons-lang-plugin-2.2.jar"/>
        <arg value="${basedir}/lib/webservices-rt.jar"/>
        <arg value="${basedir}/lib/webservices-tools.jar"/>
    </exec>
</target>

有没有办法修改build.xml文件,以便xjc仍然可以工作?欢迎任何建议。

答案

JAXB2 Basics的作者在这里。

检查JAXB2 Basics releases,有Ant示例项目(例如Purchase Order sample)。

这是相关部分:

<target name="generate-sources">
    <taskdef name="xjc" classname="org.jvnet.jaxb2_commons.xjc.XJC2Task">
        <!-- XJC2 Task classpath -->
        <classpath>
            <fileset dir="${basedir}/lib">
                <include name="activation-*.jar"/>
                <include name="jaxb-api-*.jar"/>
                <include name="jaxb-impl-*.jar"/>
                <include name="jaxb-runtime-*.jar"/>
                <include name="jaxb-core-*.jar"/>
                <include name="jsr173_api-*.jar"/>
                <include name="stax-api-*.jar"/>

                <include name="jaxb-xjc-*.jar"/>
                <include name="jaxb2-basics-ant-*.jar"/>

                <include name="slf4j-*.jar"/>
                <include name="jcl-over-slf4j-*.jar"/>
            </fileset>
        </classpath>
    </taskdef>
    <mkdir dir="${basedir}/target/generated-sources/xjc"/>
    <xjc destdir="${basedir}/target/generated-sources/xjc" extension="true">
        <!--arg line="
            -Xequals
            -XhashCode
            -XtoString
            -Xcopyable
            -Xmergeable
            -Xinheritance"/-->
        <binding dir="${basedir}/src/main/resources">
            <include name="**/*.xjb"/>
        </binding>
        <schema dir="${basedir}/src/main/resources">
            <include name="**/*.xsd"/>
        </schema>
        <!-- Plugins -->
        <classpath>
            <fileset dir="${basedir}/lib">
                <!-- JAXB2 Basics library -->
                <!--include name="jaxb2-basics-plugins-*.jar"/-->
            </fileset>
        </classpath>
    </xjc>
</target>
另一答案

不确定您的环境,但默认情况下不解析Java EE模块(包括JAXB)。请参考https://docs.oracle.com/javase/9/migrate/toc.htm#JSMIG-GUID-F640FA9D-FB66-4D85-AD2B-D931174C09A3

以上是关于如何修改Java 9的ANT build.xml中的xjc块?的主要内容,如果未能解决你的问题,请参考以下文章

Java:如何使用自定义 Ant build.xml 将 ProGuard 集成到 Jar 项目中

build.xml: 21: Class not found: javac1.8

如何使用Ant解决模式解析异常?

ANT build.xml 编译出错Error running javac.exe compiler,详细情况如下:

java如何打包

有没有从Eclipse自动生成ant build.xml文件的方法?