蚂蚁构建后java项目无法正常工作

时间:2011-08-25 12:44:31

标签: java eclipse ant

我是Java新手,花了太多时间在这个问题上,无法解决它:

我在Eclipse Java项目中运行build.xml文件。项目成功构建,所有测试都通过了,但是在该项目不起作用之后,似乎是.classpath文件出现了错误,导致如果我更改构建路径设置(例如删​​除库并再次添加),一切正常......

build.xml文件 `

<!-- Sets variables which can later be used. -->
<!-- The value of a property is accessed via ${} -->
<property name="src.dir" location="src" />
<property name="build.dir" location="build" />
<property name="dist.dir" location="dist" />
<property name="test.report.dir" location="test report" />

<!-- Define the classpath which includes -->
<path id="junit.class.path">

    <!-- needed libs -->
    <pathelement location="lib/commons-dbcp.jar" />
    <pathelement location="lib/commons-logging.jar" />
    <pathelement location="lib/commons-pool.jar" />
    <pathelement location="lib/log4j-1.2.15.jar" />
    <pathelement location="lib/mysql-connector-java-5.1.6-bin.jar" />
    <pathelement location="lib/spring.jar" />
    <pathelement location="lib/spring-webmvc.jar" />
    <pathelement location="lib/standard.jar" />

    <!-- JUnit 3-4 -->
    <pathelement location="C:\Program Files\eclipse\plugins\org.junit_4.8.1.v4_8_1_v20100427-1100\junit.jar" />
    <pathelement location="C:\Program Files\eclipse\plugins\org.junit_3.8.2.v3_8_2_v20100427-1100\junit.jar" />

    <!-- repository-config.xml -->
    <pathelement location="config" />

    <!-- classes after compiling -->
    <pathelement location="${build.dir}" />
</path>

<!-- Deletes the existing build, docs and dist directory-->
<target name="clean">
    <delete dir="${build.dir}" />
    <delete dir="${dist.dir}" />
    <delete dir="${test.report.dir}" />
</target>

<!-- Creates the  build, docs and dist directory-->
<target name="makedir">
    <mkdir dir="${build.dir}" />
    <mkdir dir="${dist.dir}" />
    <mkdir dir="${test.report.dir}" />
</target>

<!-- Compiles the java code (including the usage of library for JUnit -->
<target name="compile" depends="clean, makedir">
    <javac srcdir="${src.dir}" destdir="${build.dir}">
        <classpath refid="junit.class.path" />
    </javac>
</target>

<!--Creates the deployable jar file  -->
<target name="jar" depends="compile">
    <jar destfile="${dist.dir}\empdb.build.ant.jar" basedir="${build.dir}">
    </jar>
</target>

<!-- Run the JUnit Tests -->
<!-- Output is XML, could also be plain-->
<target name="junit" depends="jar">
    <junit printsummary="on" fork="true" haltonfailure="yes">
        <classpath refid="junit.class.path" />
        <formatter type="xml" />
        <batchtest todir="${test.report.dir}">
            <fileset dir="${src.dir}">
                <include name="**/*Test*.java" />
            </fileset>
        </batchtest>
    </junit>
</target>

<target name="main" depends="junit">
    <description>Main target</description>
</target>

`

这是项目构建后的一个测试错误

Class not found sef.test.repository.StubProjectRepositoryImplTest
java.lang.ClassNotFoundException: sef.test.repository.StubProjectRepositoryImplTest
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadClass(RemoteTestRunner.java:693)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadClasses(RemoteTestRunner.java:429)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

2 个答案:

答案 0 :(得分:2)

当文件发生变化时,eclipse常常会翻转。

试试这个。

  1. 右键单击您的项目,然后按刷新
  2. 使用项目 - &gt;清洁......在日食中
  3. 使用项目 - &gt;在eclipse中构建所有

答案 1 :(得分:2)

看起来您使用Eclipse的构建作为ant编译的目标,因此在您的ant编译目标运行后,您的工作区不同步。

如果Eclipse和Ant之间的类路径相同,为什么还需要Ant的清理和编译目标?如果它们不同,Ant应该编译到不同的目的地。

如果您真的希望Ant和Eclipse编译到同一目的地,请告诉Eclipse通过在项目根目录上按F5来刷新,或者在外部工具下设置ant build配置以自动刷新: enter image description here