2017-11-22 19 views
1

Ant를 처음 사용하고 프로젝트에서 jar 파일을 만들고 테스트를 실행하고 jar 파일을 실행하도록 작업을 설정했습니다. 이 리소스를 사용하여 나에게 명확하지 않은 이유로 테스트를 제외한 모든 것을 성공적으로 얻을 수 있습니다. "Getting started – Ant" 테스트를 다운로드하려고했지만 junit 라이브러리가 보이지 않기 때문에 성공하지 못했습니다. 여기 내 실수는 다음과 같습니다Ant 패키지 org.junit가 존재하지 않습니다.

[mkdir] Created dir: D:\Eclipse project\CalcSimple\build\main 
[javac] Compiling 3 source files to D:\Eclipse project\CalcSimple\build\main 
[javac] D:\Eclipse project\CalcSimple\src\testing\CalcAPITest.java:4: error: package org.junit does not exist 
[javac] import org.junit.Test; 

enter image description here

테스트 자체 근로자가 원시적 계산기를 계산. 오류를 찾고 해결 방법을 제안 할 수 없습니까? 프로젝트 구조 : enter image description here

내 코드 :

<project name="CalcAnt" basedir="." default="main"> 


    <property name="build.dir" value="build"/> 
    <property name="jar.dir"  value="${build.dir}/jar"/> 
    <property name="main.build.dir" value="build/main"/> 
    <property name="main.src.dir" value="src"/> 
    <property name="test.build.dir" value="build/test"/> 
    <property name="test.src.dir" value="src/testing"/> 
    <property name="main-class" value="CalcSimples.CalcSimple"/> 

    <!--PATH LIB--> 
    <path id="classpath.test"> 
     <pathelement location="lib/junit-4.11.jar"/> 
     <pathelement location="lib/hamcrest-core-1.3.jar"/> 
     <pathelement location="${main.build.dir}"/> 
    </path> 

     <!--CLEAN--> 
    <target name="clean"> 
     <delete dir="${build.dir}"/> 
    </target> 

     <!--COMPILE--> 
    <target name="compile"> 
     <mkdir dir="${main.build.dir}"/> 
     <javac srcdir="${main.src.dir}" destdir="${main.build.dir}" includeantruntime="false"/> 
    </target> 

     <!--TEST-COMPILE--> 
    <target name="test-compile" depends="compile"> 
     <mkdir dir="${test.build.dir}"/> 
     <javac srcdir="${test.src.dir}" destdir="${test.build.dir}" includeantruntime="false"> 
      <classpath refid="classpath.test"/> 
     </javac> 
    </target> 

    <!--TEST--> 
    <target name="test" depends="test-compile"> 
     <junit printsummary="on" haltonfailure="yes" fork="true"> 
      <classpath> 
       <path refid="classpath.test"/> 
       <pathelement location="${test.build.dir}"/> 
      </classpath> 
      <formatter type="brief" usefile="false" /> 
      <batchtest> 
       <fileset dir="${test.src.dir}" includes="**/*Test.java" > 
       </fileset> 
      </batchtest> 
     </junit> 
    </target> 

    <!--CREATE JAR--> 
    <target name="jar" depends="compile"> 
     <mkdir dir="${jar.dir}"/> 
     <jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${main.build.dir}"> 
      <manifest> 
       <attribute name="Main-Class" value="${main-class}"/> 
      </manifest> 
     </jar> 
    </target> 

    <!--RUN JAR--> 
    <target name="run" depends="jar"> 
     <java jar="${jar.dir}/${ant.project.name}.jar" fork="true"/> 
    </target> 

    <!--CLEAN-BUILD--> 
    <target name="clean-build" depends="clean,jar"/> 

    <!--MAIN--> 
    <target name="main" depends="clean,run"/> 
</project> 

답변

1

귀하의 테스트가 src 아래 살고 당신의 "컴파일"대상이 javac 작업이 junit.jar를 알고하지 않기 때문에 실패하는, 컴파일을 시도 .

src 디렉토리에서 테스트를 이동하거나 테스트가 "compile"대상에 의해 컴파일되지 않도록 명시 적으로 제외해야합니다. 별도의 소스 트리에 주요 테스트 소스를 유지

<javac srcdir="${main.src.dir}" destdir="${main.build.dir}" includeantruntime="false"> 
    <exclude name="testing/**"/> 
</javac> 

src 변화 "컴파일"의 javac의 서브 디렉토리로 testing을 유지하려면하지만 가장 좋은 방법으로 간주됩니다. 일반적으로 src/main과 같은 것을 만들고, CalcSimples 디렉토리를 이동 한 다음 main.src.dirsrc/main으로 설정합니다.