4

我们已经使用 eclipse 构建了一个 spring-batch 应用程序。每当我通过eclipse执行程序时,它运行良好。但是当我尝试生成并运行使用 ant 创建的 jar 文件时,我得到了这个丑陋的堆栈跟踪。

2012 年 12 月 27 日 11:10:30,880 1141 [main][] 错误 (CommandLineJobRunner.java:355):作业终止错误:来自类路径资源 [launch-context.xml] 的 XML 文档中的第 12 行无效;嵌套异常是 org.xml.sax.SAXParseException:cvc-elt.1:找不到元素“bean”的声明。org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException:来自类路径资源 [launch-context.xml] 的 XML 文档中的第 12 行无效;嵌套异常是 org.xml.sax.SAXParseException:cvc-elt.1:找不到元素“bean”的声明。在 org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:396) 在 org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334) 在 org. springframework.batch.core.launch.support.CommandLineJobRunner.main(CommandLineJobRunner.java:574) at com.my.path.invoker.JobTest.main(JobTest.java:25) 原因:org.xml.sax.SAXParseException: cvc-elt.1:找不到元素“beans”的声明。在 com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195) 在 com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:131)在 com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:384) 在 com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:318)在 com.sun.org.apache.xerces 的 com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleStartElement(XMLSchemaValidator.java:1916)。. 还有 16 个

它基本上是说在第 11 行它找不到元素 bean 的声明。这是我的启动上下文。

<?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"        
        xmlns:batch="http://www.springframework.org/schema/batch"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:jdbc="http://www.springframework.org/schema/jdbc"
        xsi:schemaLocation="
            http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.1.xsd
            http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

        <bean id="placeholderConfig"
            class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="locations">
                <list>
                    <value>file:batch.properties</value>
                    <value>file:application.properties</value>
                </list>
            </property>
        </bean>


        <context:component-scan base-package="com.my.path" />

        <import resource="classpath:/META-INF/spring/batch-context.xml" />
        <import resource="classpath:/META-INF/spring/module-context.xml" />


    </beans>

和生成 jar 的 build.xml。

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- WARNING: Eclipse auto-generated file.
              Any modifications will be overwritten.
              To include a user specific buildfile here, simply create one in the same
              directory with the processing instruction <?eclipse.ant.import?>
              as the first entry and export the buildfile again. -->
<project basedir="." default="jar" name="ERS2Utilities">
    <property environment="env"/>
    <property name="debuglevel" value="source,lines,vars"/>
    <property name="target" value="1.6"/>
    <property name="source" value="1.6"/>
    <path id="CLASSPATH">
        <pathelement location="target/classes"/>
        <pathelement location="target/test-classes"/>
        <!-- 
        <pathelement location="src/main/resources/lib/antlr-2.7.7.jar"/>
        <pathelement location="src/main/resources/lib/antlr-runtime-3.2.jar"/>
        <pathelement location="src/main/resources/lib/org.springframework.instrument.tomcat-3.0.3.RELEASE.jar"/>
        <pathelement location="src/main/resources/lib/org.springframework.jms-3.0.3.RELEASE.jar"/>
        <pathelement location="src/main/resources/lib/org.springframework.js-2.1.1.RELEASE.jar"/>
        <pathelement location="src/main/resources/lib/org.springframework.web-3.0.3.RELEASE.jar"/>
        <pathelement location="src/main/resources/lib/org.springframework.web.portlet-3.0.3.RELEASE.jar"/>
        <pathelement location="src/main/resources/lib/org.springframework.web.servlet-3.0.3.RELEASE.jar"/>
        <pathelement location="src/main/resources/lib/org.springframework.web.struts-3.0.3.RELEASE.jar"/>
        <pathelement location="src/main/resources/lib/org.springframework.webflow-2.1.1.RELEASE.jar"/>
        <pathelement location="src/main/resources/lib/slf4j-api-1.6.1.jar"/>
        <pathelement location="src/main/resources/lib/slf4j-api.jar"/>
        <pathelement location="src/main/resources/lib/slf4j-log4j12-1.6.1.jar"/>
        <pathelement location="src/main/resources/lib/commons-digester.jar"/>
         -->
        <pathelement location="src/main/resources/lib/com.springsource.org.aopalliance-1.0.0.jar"/>
        <pathelement location="src/main/resources/lib/org.springframework.aop-3.0.3.RELEASE.jar"/>
        <pathelement location="src/main/resources/lib/org.springframework.asm-3.0.3.RELEASE.jar"/>
        <pathelement location="src/main/resources/lib/org.springframework.aspects-3.0.3.RELEASE.jar"/>
        <pathelement location="src/main/resources/lib/org.springframework.beans-3.0.3.RELEASE.jar"/>
        <pathelement location="src/main/resources/lib/org.springframework.binding-2.1.1.RELEASE.jar"/>
        <pathelement location="src/main/resources/lib/org.springframework.context-3.0.3.RELEASE.jar"/>
        <pathelement location="src/main/resources/lib/org.springframework.context.support-3.0.3.RELEASE.jar"/>
        <pathelement location="src/main/resources/lib/org.springframework.core-3.0.3.RELEASE.jar"/>
        <pathelement location="src/main/resources/lib/org.springframework.expression-3.0.3.RELEASE.jar"/>
        <pathelement location="src/main/resources/lib/org.springframework.instrument-3.0.3.RELEASE.jar"/>
        <pathelement location="src/main/resources/lib/org.springframework.jdbc-3.0.3.RELEASE.jar"/>
        <pathelement location="src/main/resources/lib/org.springframework.orm-3.0.3.RELEASE.jar"/>
        <pathelement location="src/main/resources/lib/org.springframework.oxm-3.0.3.RELEASE.jar"/>
        <pathelement location="src/main/resources/lib/org.springframework.test-3.0.3.RELEASE.jar"/>
        <pathelement location="src/main/resources/lib/org.springframework.transaction-3.0.3.RELEASE.jar"/>
        <pathelement location="src/main/resources/lib/spring-batch-infrastructure-2.1.9.RELEASE.jar"/>
        <pathelement location="src/main/resources/lib/spring-batch-core-2.1.9.RELEASE.jar"/>
        <pathelement location="src/main/resources/lib/spring-batch-test-2.1.9.RELEASE.jar"/>
        <pathelement location="src/main/resources/lib/commons-beanutils.jar"/>
        <pathelement location="src/main/resources/lib/commons-collections-3.1.jar"/>
        <pathelement location="src/main/resources/lib/commons-dbcp-1.4.jar"/>
        <pathelement location="src/main/resources/lib/commons-lang-2.6.jar"/>
        <pathelement location="src/main/resources/lib/commons-logging-1.1.1.jar"/>
        <pathelement location="src/main/resources/lib/commons-pool-1.5.4.jar"/>
        <pathelement location="src/main/resources/lib/junit-4.7.jar"/>
        <pathelement location="src/main/resources/lib/ojdbc14.jar"/>
        <pathelement location="src/main/resources/lib/log4j-1.2.16.jar"/>
        <pathelement location="src/main/resources/lib/HashUtility.jar"/>
        <pathelement location="src/main/resources/lib/commons-io-2.4.jar"/>
        <pathelement location="src/main/resources/lib/mockito-all-1.9.5.jar"/>
    </path>
    <target name="init">
        <mkdir dir="target/classes"/>
        <mkdir dir="target/test-classes"/>
        <copy includeemptydirs="false" todir="target/classes">
            <fileset dir="src/main/resources">
                <exclude name="**/*.java"/>
            </fileset>
        </copy>
        <copy includeemptydirs="false" todir="target/classes">
            <fileset dir="src/main/java">
                <exclude name="**/*.java"/>
            </fileset>
        </copy>
        <copy includeemptydirs="false" todir="target/test-classes">
            <fileset dir="src/test/java">
                <exclude name="**/*.java"/>
            </fileset>
        </copy>
        <copy includeemptydirs="false" todir="target/classes">
            <fileset dir="src/test/resources">
                <exclude name="**/*.java"/>
            </fileset>
        </copy>
    </target>
    <target name="clean">
        <delete dir="target/classes"/>
        <delete dir="target/test-classes"/>
    </target>



    <target depends="init" name="build-project">
        <echo message="${ant.project.name}: ${ant.file}"/>
        <javac debug="true" debuglevel="${debuglevel}" destdir="target/classes" source="${source}" target="${target}">
            <src path="src/main/resources"/>
            <classpath refid="CLASSPATH"/>
        </javac>
        <javac debug="true" debuglevel="${debuglevel}" destdir="target/classes" source="${source}" target="${target}">
            <src path="src/main/java"/>
            <classpath refid="CLASSPATH"/>
        </javac>
        <javac debug="true" debuglevel="${debuglevel}" destdir="target/test-classes" source="${source}" target="${target}">
            <src path="src/test/java"/>
            <classpath refid="CLASSPATH"/>
        </javac>
        <javac debug="true" debuglevel="${debuglevel}" destdir="target/classes" source="${source}" target="${target}">
            <src path="src/test/resources"/>
            <classpath refid="CLASSPATH"/>
        </javac>
    </target>

    <!-- <target description="compile project with Eclipse compiler" name="build-eclipse-compiler">
        <property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter"/>
        <antcall target="build"/>
    </target> -->

    <target name="JobTest">
        <java classname="com.my.path.invoker.JobTest" failonerror="true" fork="yes">
            <arg line="launch-context.xml oiaExtractorJob"/>
            <classpath refid="CLASSPATH"/>
        </java>
    </target>

    <target name="jar" depends="build-project">
        <copy todir="build/main" file="src/main/resources/log4j.xml"/>
        <copy todir="build/main" file="application.properties"/>
        <copy todir="build/main" file="batch.properties"/>
        <copy todir="build/main" file="src/main/resources/ERSUtilities.sh"/>
        <copy todir="build/main" file="src/main/resources/ERSUtilities.bat"/>

        <jar destfile="build/main/ERS2SupportingUtilities.jar">
            <fileset dir="target/classes"/>
            <restrict>
             <name name="**/*.class"/>
             <archives>
               <zips>
                 <fileset dir="./src/main/resources/lib" includes="**/*.jar"/>
               </zips>
             </archives>
            </restrict>
            <manifest>
              <attribute name="Main-Class" value="com.my.path.invoker.JobTest"/>
                <attribute name="Class-Path" value="./lib/log4j-1.2.16.jar
                                                    ./lib/org.springframework.context-3.0.3RELEASE.jar
                                                    ./lib/org.springframework.asm-3.0.3.RELEASE.jar
                                                    ./lib/junit-4.7.jar
                                                    ./lib/org.springframework.orm-3.0.3.RELEASE.jar
                                                    ./lib/org.springframework.transaction-3.0.3.RELEASE.jar
                                                    ./lib/org.springframework.aspects-3.0.3.RELEASE.jar
                                                    ./lib/commons-pool-1.5.4.jar
                                                    ./lib/org.springframework.core-3.0.3.RELEASE.jar
                                                    ./lib/commons-logging-1.1.1.jar
                                                    ./lib/HashUtility.jar
                                                    ./lib/org.springframework.expression-3.0.3.RELEASE.jar
                                                    ./lib/commons-lang-2.6.jar
                                                    ./lib/org.springframework.instrument-3.0.3.RELEASE.jar
                                                    ./lib/mockito-all-1.9.5.jar
                                                    ./lib/com.springsource.org.aopalliance-1.0.0.jar
                                                    ./lib/ojdbc14.jar
                                                    ./lib/commons-io-2.4.jar
                                                    ./lib/commons-collections-3.1.jar
                                                    ./lib/org.springframework.jdbc-3.0.3.RELEASE.jar
                                                    ./lib/spring-batch-infrastructure-2.1.9.RELEASE.jar
                                                    ./lib/org.springframework.context.support-3.0.3.RELEASE.jar
                                                    ./lib/commons-dbcp-1.4.jar
                                                    ./lib/spring-batch-test-2.1.9.RELEASE.jar
                                                    ./lib/org.springframework.beans-3.0.3.RELEASE.jar
                                                    ./lib/org.springframework.oxm-3.0.3.RELEASE.jar
                                                    ./lib/org.springframework.aop-3.0.3.RELEASE.jar
                                                    ./lib/commons-beanutils.jar
                                                    ./lib/org.springframework.binding-2.1.1.RELEASE.jar
                                                    ./lib/spring-batch-core-2.1.9.RELEASE.jar
                                                    ./lib/org.springframework.test-3.0.3.RELEASE.jar
                                                    ./launch-context.xml
                                                    ./log4j.xml"
                />
            </manifest>
        </jar>
    </target>
</project>

我在这上面花了超过 3 天的时间,但无济于事。我已确保我正在制作的 xsd 引用与我正在使用的 spring jar 版本兼容。

任何帮助将不胜感激。

编辑:

相同的 launch-context.xml 在 eclipse 中工作,当它通过给定的 ant build.xml 构建时,它会引发此错误。

编辑2:

我正在尝试将所有依赖的 jar 打包到一个胖 jar 中。我遇到过一些帖子表明我不能这样做。要引用打包的 jar,我需要one-jar或 eclipse 的jarinjarloader. 否则我将不得不将依赖的 jars 放在我的主 jar 之外。如果这是真正的问题,我将尝试这些选项并返回。同时,如果你们对此编辑有任何意见,请更新您的答案,我会将其标记为正确。

4

3 回答 3

1

您选择的 XML 编辑器/验证器是否成功验证了您的应用程序上下文(您称为启动上下文的那个)?它无效,因为http://www.springframework.org/schema/batch/spring-batch-2.1.xsd无效。

但是,您发布的内容(只是摘录?)甚至不需要batch命名空间。因此,您可以删除它,从而将其转换为有效的 XML。

于 2012-12-28T12:13:14.603 回答
0

在 ant 脚本中,您有 spring 3.0.3 的依赖项,而在应用程序上下文中,您包含 spring 3.0,这可能是问题吗?

于 2012-12-28T14:03:32.827 回答
0

如果您的执行服务器的防火墙阻止了对外部 Internet 的访问(这通常是一件好事),那么您需要使用 spring.handlers 和 spring.schemas 来帮助应用程序解析您的上下文文件。

这将引用您的框架库中存在的模式,而不是试图从互联网上提取它们。

/META-INF/spring.handlers

http\://www.springframework.org/schema/batch=org.springframework.batch.core.configuration.xml.CoreNamespaceHandler
http\://www.springframework.org/schema/context=org.springframework.context.config.ContextNamespaceHandler
http\://www.springframework.org/schema/jdbc=org.springframework.jdbc.config.JdbcNamespaceHandler

/META-INF/spring.schemas

http\://www.springframework.org/schema/beans/spring-beans-3.0.xsd=org/springframework/beans/factory/xml/spring-beans-3.0.xsd
http\://www.springframework.org/schema/context/spring-context-3.0.xsd=org/springframework/context/config/spring-context-3.0.xsd
http\://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd=org/springframework/jdbc/config/spring-jdbc-3.0.xsd
http\://www.springframework.org/schema/batch/spring-batch-2.1.xsd=org/springframework/batch/core/configuration/xml/spring-batch-2.1.xsd
于 2016-04-17T20:56:11.387 回答