当前位置: 首页 > 面试题库 >

在Ant,Ivy和JUnit中找不到类-build.xml中的错误?

边永贞
2023-03-14
问题内容

我正在尝试使用Ant,Ivy和JUnit进行一个简单的(?)测试项目。基本思想是Ivy将下载junit.jar,然后Ant将使用它。

请注意,junit jar位于类路径上,因为否则(classpath在junit任务中没有元素),我会看到“ <junit><classpath>必须包含junit.jar(如果不在Ant自己的类路径中)”。另外,下面给出的类(junit.framework.TestListener)junit-4.8.2.jar中。

但是,当我尝试ant test以下操作时,我看到:

test:

BUILD FAILED
/home/andrew/project/guice/hg/build.xml:33: java.lang.NoClassDefFoundError: junit/framework/TestListener
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
...
        at org.apache.tools.ant.launch.Launcher.run(Launcher.java:280)
        at org.apache.tools.ant.launch.Launcher.main(Launcher.java:109)
Caused by: java.lang.ClassNotFoundException: junit.framework.TestListener
        at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
...

所以我猜我的build.xml有问题吗?什么?

这是build.xml:

<project xmlns:ivy="antlib:org.apache.ivy.ant" 
         name="java-example" default="dist" basedir=".">

  <description>
    simple example build file
  </description>

  <property name="src" location="src"/>
  <property name="build" location="build"/>
  <property name="dist" location="dist"/>
  <property name="lib" location="lib"/>

  <path id="lib.path">
    <fileset dir="${lib}"/>
  </path>

  <target name="init">
    <tstamp/>
    <mkdir dir="${build}"/>
  </target>

  <target name="compile" depends="init,resolve"
          description="compile the source">
    <javac srcdir="${src}" destdir="${build}" classpathref="lib.path"
           includeantruntime="false">
      <compilerarg value="-Xlint"/>
    </javac>
  </target>

  <target name="test" depends="compile"
          description="run the tests">
    <junit>
      <classpath refid="lib.path"/>
      <batchtest>
        <fileset dir="${build}">
          <include name="**/*Test.class"/>
        </fileset>
      </batchtest>
    </junit>
  </target>

  <target name="dist" depends="compile"
          description="generate the distribution">
    <mkdir dir="${dist}/lib"/>
    <jar jarfile="${dist}/lib/example-${DSTAMP}.jar" basedir="${build}"/>
  </target>

  <target name="clean"
          description="clean up">
    <delete dir="${build}"/>
    <delete dir="${dist}"/>
  </target>

  <target name="resolve"
          description="download required dependencies">
    <ivy:retrieve/>
  </target>

</project>

以及编译后的现有目录结构:

.
├── build
│   └── com
│       └── isti
│           └── example
│               ├── AppendToList.class
│               ├── DumpToStdout.class
│               ├── LimitedCounter.class
│               ├── MessageSink.class
│               ├── MessageSource.class
│               └── SinkToSourceTest.class
├── build.xml
├── dist
│   └── lib
│       └── example-20130412.jar
├── ivy.xml
├── lib
│   ├── junit-4.8.2.jar
│   ├── junit-4.8.2-javadoc.jar
│   └── junit-4.8.2-sources.jar
├── README.md
└── src
    ├── main
    │   └── java
    │       └── com
    │           └── isti
    │               └── example
    │                   ├── AppendToList.java
    │                   ├── DumpToStdout.java
    │                   ├── LimitedCounter.java
    │                   ├── MessageSink.java
    │                   └── MessageSource.java
    └── test
        └── java
            └── com
                └── isti
                    └── example
                        └── SinkToSourceTest.java

更新顺便说一句,ant -lib lib test(明确给予lib目录)的作品。随机网络搜索结果中对此的处理方式有很多混乱的描述-但我的印象是,上述方法与最新文档(我使用ant 1.9)一致(请参见第5点)。因此,我认为这可能成为虫子; 虫子。


问题答案:


项目包含以下文件:

├── build.xml
├── ivy.xml
└── src
    ├── main
    │   ├── java
    │   │   └── org
    │   │       └── demo
    │   │           └── App.java
    │   └── resources
    │       └── log4j.properties
    └── test
        └── java
            └── org
                └── demo
                    └── AppTest.java

构建运行如下:

$ ant 
Buildfile: /home/mark/Files/Dev/ivy/demo/build.xml

resolve:
[ivy:resolve] :: Apache Ivy 2.3.0 - 20130110142753 :: http://ant.apache.org/ivy/ ::
[ivy:resolve] :: loading settings :: url = jar:file:/home/mark/.ant/lib/ivy.jar!/org/apache/ivy/core/settings/ivysettings.xml
[ivy:resolve] :: resolving dependencies :: com.myspotontheweb#demo;working@mark-Lemur-Ultra
[ivy:resolve]   confs: [compile, runtime, test]
[ivy:resolve]   found org.slf4j#slf4j-api;1.7.5 in public
[ivy:resolve]   found org.slf4j#slf4j-log4j12;1.7.5 in public
[ivy:resolve]   found log4j#log4j;1.2.17 in public
[ivy:resolve]   found junit#junit;4.11 in public
[ivy:resolve]   found org.hamcrest#hamcrest-core;1.3 in public
[ivy:resolve] :: resolution report :: resolve 347ms :: artifacts dl 14ms
    ---------------------------------------------------------------------
    |                  |            modules            ||   artifacts   |
    |       conf       | number| search|dwnlded|evicted|| number|dwnlded|
    ---------------------------------------------------------------------
    |      compile     |   1   |   0   |   0   |   0   ||   1   |   0   |
    |      runtime     |   3   |   0   |   0   |   0   ||   3   |   0   |
    |       test       |   5   |   0   |   0   |   0   ||   5   |   0   |
    ---------------------------------------------------------------------
[ivy:report] Processing /home/mark/.ivy2/cache/com.myspotontheweb-demo-compile.xml to /home/mark/Files/Dev/ivy/demo/build/ivy-reports/com.myspotontheweb-demo-compile.html
[ivy:report] Processing /home/mark/.ivy2/cache/com.myspotontheweb-demo-runtime.xml to /home/mark/Files/Dev/ivy/demo/build/ivy-reports/com.myspotontheweb-demo-runtime.html
[ivy:report] Processing /home/mark/.ivy2/cache/com.myspotontheweb-demo-test.xml to /home/mark/Files/Dev/ivy/demo/build/ivy-reports/com.myspotontheweb-demo-test.html

resources:
     [copy] Copying 1 file to /home/mark/Files/Dev/ivy/demo/build/classes

compile:
    [javac] Compiling 1 source file to /home/mark/Files/Dev/ivy/demo/build/classes

compile-tests:
    [mkdir] Created dir: /home/mark/Files/Dev/ivy/demo/build/test-classes
    [javac] Compiling 1 source file to /home/mark/Files/Dev/ivy/demo/build/test-classes

test:
    [mkdir] Created dir: /home/mark/Files/Dev/ivy/demo/build/test-reports
    [junit] Running org.demo.AppTest
    [junit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0.085 sec

build:
[ivy:retrieve] :: retrieving :: com.myspotontheweb#demo
[ivy:retrieve]  confs: [runtime]
[ivy:retrieve]  3 artifacts copied, 0 already retrieved (512kB/16ms)
      [jar] Building jar: /home/mark/Files/Dev/ivy/demo/build/dist/demo.jar

BUILD SUCCESSFUL
Total time: 4 seconds

常春藤
常春藤的一个非常强大的功能是配置。这些使您可以将依赖项分组在一起。

<ivy-module version="2.0">
    <info organisation="com.myspotontheweb" module="demo"/>

    <configurations>
        <conf name="compile" description="Required to compile application"/>
        <conf name="runtime" description="Additional run-time dependencies" extends="compile"/>
        <conf name="test"    description="Required for test only" extends="runtime"/>
    </configurations>

    <dependencies>
        <!-- compile dependencies -->
        <dependency org="org.slf4j" name="slf4j-api" rev="1.7.5" conf="compile->default"/>

        <!-- runtime dependencies -->
        <dependency org="org.slf4j" name="slf4j-log4j12" rev="1.7.5" conf="runtime->default"/>

        <!-- test dependencies -->
        <dependency org="junit" name="junit" rev="4.11" conf="test->default"/>
    </dependencies>

</ivy-module>

笔记:

配置使用“扩展”功能来模拟Maven“编译”,“运行时”和“测试” Maven范围。
注意每个依赖项上的特殊“ conf”属性。这是从本地到远程的映射。有关ivy如何管理远程Maven模块的更多详细信息,请参见:ivy如何将maven范围映射到ivy配置
build.xml
常春藤配置可以被诸如cachepath(创建ANT路径)和检索(将文件复制到构建中)之类的任务所利用。我还建议使用报告目标,以便您可以查看每种配置中出现了哪些jar(用于管理传递依赖项)

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

    <!--
    ================
    Build properties
    ================
    -->
    <property name="src.dir"          location="src/main/java"/>
    <property name="resources.dir"    location="src/main/resources"/>
    <property name="test.src.dir"     location="src/test/java"/>
    <property name="build.dir"        location="build"/>
    <property name="classes.dir"      location="${build.dir}/classes"/>
    <property name="test.classes.dir" location="${build.dir}/test-classes"/>
    <property name="ivy.reports.dir"  location="${build.dir}/ivy-reports"/>
    <property name="test.reports.dir" location="${build.dir}/test-reports"/>
    <property name="dist.dir"         location="${build.dir}/dist"/>

    <property name="jar.main.class" value="org.demo.App"/>
    <property name="jar.file"       value="${dist.dir}/${ant.project.name}.jar"/>

    <available classname="org.apache.ivy.Main" property="ivy.installed"/>

    <!--
    ===========
    Build setup
    ===========
    -->
    <target name="install-ivy" description="Install ivy" unless="ivy.installed">
        <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.3.0/ivy-2.3.0.jar"/>
        <fail message="Ivy has been installed. Run the build again"/>
    </target>

    <target name="resolve" depends="install-ivy" description="Use ivy to resolve classpaths">
        <ivy:resolve/>

        <ivy:report todir='${ivy.reports.dir}' graph='false' xml='false'/>

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

    <!--
    ===============
    Compile targets
    ===============
    -->
    <target name="resources" description="Copy resources into classpath">
        <copy todir="${classes.dir}">
            <fileset dir="${resources.dir}"/>
        </copy>
    </target>

    <target name="compile" depends="resolve,resources" description="Compile code">
        <mkdir dir="${classes.dir}"/>
        <javac srcdir="${src.dir}" destdir="${classes.dir}" includeantruntime="false" debug="true" classpathref="compile.path"/>
    </target>

    <target name="compile-tests" depends="compile" description="Compile tests">
        <mkdir dir="${test.classes.dir}"/>
        <javac srcdir="${test.src.dir}" destdir="${test.classes.dir}" includeantruntime="false" debug="true">
            <classpath>
                <path refid="test.path"/>
                <pathelement path="${classes.dir}"/>
            </classpath>
        </javac>
    </target>

    <!--
    ============
    Test targets
    ============
    -->
    <target name="test" depends="compile-tests" description="Run unit tests">
        <mkdir dir="${test.reports.dir}"/>
        <junit printsummary="yes" haltonfailure="yes">
            <classpath>
                <path refid="test.path"/>
                <pathelement path="${classes.dir}"/>
                <pathelement path="${test.classes.dir}"/>
            </classpath>
            <formatter type="xml"/>
            <batchtest fork="yes" todir="${test.reports.dir}">
                <fileset dir="${test.src.dir}">
                    <include name="**/*Test*.java"/>
                    <exclude name="**/AllTests.java"/>
                </fileset>
            </batchtest>
        </junit>
    </target>

    <!--
    =====================
    Build and run targets
    =====================
    -->
    <target name="build" depends="test" description="Create executable jar archive">
        <ivy:retrieve pattern="${dist.dir}/lib/[artifact]-[revision](-[classifier]).[ext]" conf="runtime"/>

        <manifestclasspath property="jar.classpath" jarfile="${jar.file}">
            <classpath>
                <fileset dir="${dist.dir}/lib" includes="*.jar"/>
            </classpath>
        </manifestclasspath>

        <jar destfile="${jar.file}" basedir="${classes.dir}">
            <manifest>
                <attribute name="Main-Class" value="${jar.main.class}" />
                <attribute name="Class-Path" value="${jar.classpath}" />
            </manifest>
        </jar>
    </target>

    <target name="run" depends="build" description="Run code">
        <java jar="${jar.file}" fork="true"/>
    </target>

    <!--
    =============
    Clean targets
    =============
    -->
    <target name="clean" description="Cleanup build files">
        <delete dir="${build.dir}"/>
    </target>

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

</project>

注意:

有条件的“ install-ivy”目标将自动安装ivy。只需重新运行构建,只需完成一次。

App.java

你好世界记录示例。

package org.demo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Hello world!
 *
 */
public class App {
    static final Logger log = LoggerFactory.getLogger(App.class);

    public static void main( String[] args ) {
        App a = new App();
        a.speak("hello world");
    }

    public void speak(String message) {
        log.info(message);
    }
}

AppTest.java

这是我档案中的一个旧示例。不使用Junit断言。

ackage org.demo;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

/**
 * Unit test for simple App.
 */
public class AppTest 
    extends TestCase
{
    /**
     * Create the test case
     *
     * @param testName name of the test case
     */
    public AppTest( String testName )
    {
        super( testName );
    }

    /**
     * @return the suite of tests being tested
     */
    public static Test suite()
    {
        return new TestSuite( AppTest.class );
    }

    /**
     * Rigourous Test :-)
     */
    public void testApp()
    {
        assertTrue( true );
    }
}
log4j.properties
# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=DEBUG, A1

# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender

# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n


 类似资料:
  • 问题内容: 在Windows 7中安装我的蚂蚁后。在cmd中,我键入 ant -v, 它给出了ant版本,但它也表示以下内容。 系统中有什么问题。我该如何纠正这个问题? 问题答案: 您应该 改为使用命令。 该 选项等同于 选项。 请参阅命令行选项摘要

  • 我是ANT build新手,在编译ANT build时遇到错误“package org.testng.annotations不存在”和“package org.openqa.selenium不存在”。xml。 这里是build.xml 下面是错误列表 我还设置了JAVA\u HOME C:\Program Files\JAVA\jdk1.7.0\u 25\bin和ANT\u HOME C:\Use

  • 最后是如何编写ivy.xml来获取我正在使用的库的最新版本。例如,我正在使用jgraph.jar、commons-codec-1.6.jar等,有人能演示一下如何在ivy.xml文件中编写代码来获取这些文件的最新版本吗?这些都在maven中央存储库中可用,我可以通过ivy.xml连接到它吗?如果是,怎么做?

  • 问题内容: 我有使用iso-8859-1编码的Java源文件。运行 ant时 ,出现“警告:编码UTF-8的不可映射字符”。如果我运行 ant -Dfile.encoding = iso-8859-1 或向每个javac语句添加 encoding =“ ISO-8859-1” ,就可以避免这种情况。 有没有一种方法可以在build.xml中全局设置属性? _< 属性名称=“ file.encodi

  • 这里是整个build.xml,第26行是javac标记: