【问题标题】:Spring is error with Ant build fileSpring 是 Ant 构建文件的错误
【发布时间】:2010-12-06 16:25:42
【问题描述】:

我曾经使用 NetBean IDE 自动构建和运行。它适用于 Spring。但是,现在我想编写自己的 Ant 构建文件。它构建成功,唯一的问题是 ClassPathXmlApplicationContext 似乎在运行时找不到类路径。我正在使用带有以下库的 Spring 3.0.5,放在 lib 文件夹中:

org.springframework.beans-3.0.5.RELEASE.jar
org.springframework.beans-sources-3.0.5.RELEASE.jar
org.springframework.context.support-sources-3.0.5.RELEASE.jar
org.springframework.context-3.0.5.RELEASE.jar
org.springframework.context-sources-3.0.5.RELEASE.jar
org.springframework.core-3.0.5.RELEASE.jar
org.springframework.core-sources-3.0.5.RELEASE.jar

文件夹结构:

开发堡垒

+---库

+---src

    + config

    +.......

+---构建

    +----classes

    +----jar

我想从我的 devFortress.xml 中获取上下文: ApplicationContext context = new ClassPathXmlApplicationContext("DevFortress.xml"); 最初,DevFortress.xml 在 src 包中的 config 包中。但是,我只是想让我的程序运行起来,所以我把它放到了lib文件夹,classes和jar中,但是没有希望。

这是我的构建文件:

<project name="DevFortress" basedir="." default="main">
    <property name="src.dir" value="src"/>
    <property name="config.dir" value="${src.dir}/config"/>
    <property name="build.dir" value="build"/>
    <property name="classes.dir" value="${build.dir}/classes"/>
    <property name="jar.dir" value="${build.dir}/jar"/>

    <property name="main-class" value="Controller.Main"/>

    <property name="lib.dir" value="lib"/>

    <path id="classpath">
        <fileset dir="${lib.dir}" includes="**/*.jar"/>
        <fileset dir="${config.dir}" includes="**/*.xml"/>
    </path>
    <target name="clean">
        <delete dir="${build.dir}"/>
    </target>

<target name="compile">
    <mkdir dir="${classes.dir}"/>
    <javac srcdir="${src.dir}" destdir="${classes.dir}" classpathref="classpath"/>
</target>

<target name="jar" depends="compile">
    <mkdir dir="${jar.dir}"/>
    <jar destfile = "${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
        <manifest>
            <attribute name="Main-Class" value="${main-class}"/>
        </manifest>
    </jar>
</target>

<target name="run" depends = "jar">
    <java fork="true" classname="${main-class}">
            <classpath>
                <path refid="classpath"/>
                <path location="${jar.dir}/${ant.project.name}.jar"/>
            </classpath>
        </java>
</target>

<target name="clean-build" depends="clean,jar"/>

<target name="main" depends="clean,run"/>
</project>

这是怎么回事?

【问题讨论】:

    标签: java spring ant


    【解决方案1】:

    您是否尝试过仅将配置文件包含到 jar 创建过程中

    【讨论】:

    • 在生成jar文件之前将配置文件复制到classes.dir,这样就在jar里面了
    • 哇,我遇到了另一个问题。我错了。我使用了在NetBean中构建的spring framework 2.0.5,它可以工作。但是,当我在官方网站上下载时,将那些旧的内置库替换为新的(我删除了所有在 NetBean 中的库包中)。该程序现在无法运行。它不能再归档类路径了。
    【解决方案2】:

    另外,我在 Spring XML 文件中的 XML 模式:

    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:context="http://www.springframework.org/schema/context"
    
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
              http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.6.SEC01.xsd
              http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.6.SEC01.xsd"/>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-13
      • 2015-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多