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

发布于 2025-01-06 10:27:19 字数 3789 浏览 1 评论 0原文

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

所以基本上我们的 Jenkins 自动构建设置中的问题是它自己的 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>

编辑:可以找到类似的问题 ​​这里有一个效果很好的不同答案。请注意,在计算机上安装 ant-junit 比尝试将其添加到您的库和所有内容中要容易得多。

I was building an Ant build file that worked properly in my eclipse project but not on our Jenkins autobuild set-up. I installed ant on my computer and ran the build from the console. It worked, but I realized it didn't use the junit-4.10.jar in my project lib like I wished but the junit.jar in the ant lib. After renaming the junit.jar files in my ant lib, the ant build didn't work.

So basically the problem in our Jenkins autobuild setup is that there are no junit.jar in its own ant lib directory. Can I specify the junit task to use the jar in my project lib instead of the one in the ant lib?

EDIT : I have modified my build.xml file and now it looks like this, still doesnt work. My junit-4.10.jar is in the /war/WEB-INF/lib/test directory :

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

EDIT : Simlar question can be found here with a different answer that works well. Note that it is much easier to install ant-junit on the machine than to try to add it to your libs and everything.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

椵侞 2025-01-13 10:27:19

请参阅此问题的答案:

H2 数据库 org.h2.Driver ClassNotFoundException< /a>

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


更新

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

设置使用 ivy 来管理类路径的基础架构。依赖项是从 Maven 中央存储库(默认情况下)下载的。使构建更加便携和可重复。

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>

See the answer to this question:

H2 database org.h2.Driver ClassNotFoundException

I normally specify the junit jar to be on a test classpath and then use it when calling the junit ANT task


Update

The following build file is an example of a starting template I used for my builds.

Sets up an infrastructure that uses ivy to manage classpaths. Dependencies are downloaded from the Maven Central repository (by default). Makes build much more portable and repeatable.

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

This file is used to specify the project's dependencies. Ivy configurations are used to manage the classpath groupings.

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文