junit.jar是否必须在ant lib目录中才能运行junit任务

时间:2012-02-17 15:19:24

标签: ant junit jenkins

我正在构建一个Ant构建文件,该文件在我的eclipse项目中正常工作,但在我们的Jenkins autobuild设置中没有。我在我的计算机上安装了ant并从控制台运行了构建。它工作,但我意识到它没有像我希望的那样在我的项目库中使用junit-4.10.jar,而是在ant lib中使用junit.jar。在我的ant lib中重命名junit.jar文件后,ant build无效。

基本上我们Jenkins autobuild设置中的问题是它自己的ant lib目录中没有junit.jar。我可以指定junit任务来使用项目lib中的jar而不是ant lib中的jar吗?

编辑:我修改了我的build.xml文件,现在它看起来像这样,仍然无法正常工作。我的junit-4.10.jar位于/ war / WEB-INF / lib / test目录中:

<project name="vlp" default="junit" basedir=".">
    <tstamp />
    <!-- ################# PROPERTIES ################ -->
    <!-- directory properties -->
    <!-- source -->
    <property name="vlp.src" location="src" />
    <property name="vlp.test" location="test" />
    <!-- build -->
    <property name="src.build" location="bin/src" />
    <property name="test.build" location="bin/test" />
    <!-- libraries -->
    <property name="vlp.lib.dir" location="war/WEB-INF/lib" />
    <property name="vlp.testlib.dir" location="war/WEB-INF/lib/test" />
    <!-- compile classpath -->
    <path id="compile.path">
        <fileset dir="${vlp.lib.dir}" includes="*.jar" />
    </path>
    <!-- test classpath -->
    <path id="test.path">
        <fileset dir="${vlp.testlib.dir}" includes="*.jar" />
        <path refid="compile.path" />
    </path>
    <!-- ############### CLEANING ################## -->
    <!-- Cleaning old compile files -->
    <target name="clean" description="Clean all the old build files.">
        <delete dir="${src.build}" />
        <delete dir="${dist}" />
    </target>
    <!-- ############## COMPILATION ############### -->
    <!-- Compile source -->
    <target name="src.compile" depends="clean" description="Compile the source code when everything has been cleaned.">
        <mkdir dir="${src.build}" />
        <javac encoding="utf-8" destdir="${src.build}" nowarn="true">
            <src path="${vlp.src}" />
            <classpath refid="compile.path" />
        </javac>
    </target>
    <!-- Compile test -->
    <target name="test.compile" depends="clean" description="Compile the source code when everything has been cleaned.">
        <mkdir dir="${test.build}" />
        <javac encoding="utf-8" destdir="${test.build}" nowarn="true">
            <src path="${vlp.test}" />
            <classpath>
                <pathelement location="${src.build}" />
                <path refid="test.path" />
            </classpath>
        </javac>
    </target>
    <!-- ########### RUNS JUNIT TEST ############ -->
    <target name="junit" depends="src.compile,test.compile" description="Runs all the unit test in the application. Does not halt build if test are failed.">
        <junit printsummary="on" haltonfailure="false" showoutput="true">
            <formatter type="brief" usefile="false" />
            <classpath>
                <pathelement location="${test.build}" />
                <path refid="test.path" />
            </classpath>
            <batchtest>
                <fileset dir="${vlp.test}">
                    <include name="**/Test*.java" />
                    <exclude name="**/AllTests.java" />
                </fileset>
            </batchtest>
        </junit>
    </target>
</project>

编辑:可以找到模拟问题here,但答案不同,效果很好。请注意,在计算机上安装ant-junit比尝试将其添加到libs和所有内容要容易得多。

1 个答案:

答案 0 :(得分:1)

请参阅此问题的答案:

H2 database org.h2.Driver ClassNotFoundException

我通常将junit jar指定为测试类路径,然后在调用junit ANT任务时使用它


更新

以下构建文件是我用于构建的起始模板的示例。

设置使用ivy管理类路径的基础结构。从Maven Central repository(默认情况下)下载依赖项。使构建更加便携和可重复。

的build.xml

<project name="ivy demo" default="build" xmlns:ivy="antlib:org.apache.ivy.ant">

    <!--
    ==========
    Properties
    ==========
    -->
    <property name="build.dir"  location="build"/>
    <property name="class.dir"  location="${build.dir}/classes"/>
    <property name="report.dir" location="${build.dir}/reports"/>

    <!-- 
    =======
    Targets
    =======
    -->
    <target name="install-ivy" description="Used to install the ivy task jar">
        <mkdir dir="${user.home}/.ant/lib"/>
        <get dest="${user.home}/.ant/lib/ivy.jar" src="http://search.maven.org/remotecontent?filepath=org/apache/ivy/ivy/2.2.0/ivy-2.2.0.jar"/>
    </target>

    <target name="init" description="Download dependencies, setup project classpaths and create build directories">
        <ivy:resolve/>

        <ivy:cachepath pathid="compile.path" conf="compile"/>
        <ivy:cachepath pathid="runtime.path" conf="runtime"/>
        <ivy:cachepath pathid="test.path"    conf="test"/>

        <ivy:report todir="${report.dir}" graph="false"/>

        <mkdir dir="${class.dir}"/>
    </target>

    <target name="build" depends="init" description="Build the project">
        <echo message="Build logic goes here"/>
    </target>

    <target name="clean" description="Remove build directories">
        <delete dir="${build.dir}"/>
    </target>

    <target name="clean-all" depends="clean" description="Purge ivy cache">
        <ivy:cleancache />
    </target>

</project>

的ivy.xml

此文件用于指定项目的依赖项。常春藤配置用于管理类路径分组。

<ivy-module version="2.0">
    <info organisation="org.demo" module="demo"/>
    <configurations>
        <conf name="compile"/>
        <conf name="runtime" extends="compile"/>
        <conf name="test"    extends="runtime"/>
    </configurations>
    <!--
    Dependencies can be found using Maven Central's search site:

        http://search.maven.org/
    -->
    <dependencies>
        <!-- Compile dependencies -->
        <dependency org="org.slf4j" name="slf4j-api" rev="1.6.4" conf="compile->default"/>

        <!-- Runtime dependencies -->
        <dependency org="log4j" name="log4j" rev="1.2.16" conf="runtime->default"/>

        <!-- Test dependencies -->
        <dependency org="junit" name="junit" rev="4.10" conf="test->default"/>
    </dependencies>
</ivy-module>