【问题标题】:Does the junit.jar has to be in the ant lib directory to run junit taskjunit.jar 是否必须在 ant lib 目录中才能运行 junit 任务
【发布时间】:2012-02-17 15:19:23
【问题描述】:

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

所以基本上我们的 Jenkins 自动构建设置中的问题是它自己的 ant lib 目录中没有 junit.jar。我可以指定 junit 任务以使用项目库中的 jar 而不是 ant 库中的 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 比尝试将其添加到您的库和所有内容中要容易得多。

【问题讨论】:

    标签: ant junit jenkins


    【解决方案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 配置用于管理类路径分组。

    <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>
    

    【讨论】:

    • @David 您确定 junit jar 存在于您在 build.xml 中配置的位置的 Jenkins 服务器上吗?顺便说一句,我个人使用 Apache ivy 插件来管理我构建的所有依赖项。该工具可以自动下载此类依赖项,因为它们在本地缺少
    • @David 我已经用一个示例模板更新了我的帖子,以使用 ivy 插件。所有依赖项都在 ivy.xml 文件中声明。在构建开始时,它们会被下载并缓存以供将来构建。
    • 我没有用ivy,因为老板不想用太多插件,但我找到了方法!我会尽快发布我的解决方案!
    • @David:我遇到了和你一样的问题,如果你能发布你找到的解决方案将不胜感激。
    • @ibrahimArief 查看我更新的帖子 ^ 或转到:stackoverflow.com/questions/9774264/…
    猜你喜欢
    • 2014-10-27
    • 2011-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-19
    • 1970-01-01
    • 2011-05-20
    • 1970-01-01
    相关资源
    最近更新 更多