2012-03-07 16 views
2

まず、mavenにantビルドをラップしてmavenへの移行を開始します。mavenからant junitターゲットを実行しています

プロジェクトをビルドするためにmavenを取得できますが、テストを実行することはできません。 build.xmlのantテスト対象をmavenから実行しようとしています。もちろん、私たちがアリからテストターゲットを実行するときは、すべてが問題ありません。

以下は、1つのテストクラスで表示されるエラーです(他のテストクラスでも同じです)。このクラスにはいくつかのテストメソッドがありますが、どれも実行されていません。これはJUnitのバージョン問題のようです。テストはJUnit 4テストです。テストクラスを変更してTestCase(JUnit 3スタイル)を拡張すると、テストが見つかります。ここで

No tests found in org.mathforum.common.dao.ExampleBaseObjectDaoTest 

junit.framework.AssertionFailedError: No tests found in org.mathforum.common.dao.ExampleBaseObjectDaoTest at 
org.apache.maven.plugin.antrun.AntRunMojo.execute(AntRunMojo.java:327) at 
org.apache.maven.plugin.DefaultPluginManager.executeMojo(DefaultPluginManager.java:490) at 
org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:694) at 
org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalWithLifecycle(DefaultLifecycleExecutor.java:556) at 
org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoal(DefaultLifecycleExecutor.java:535) at 
org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalAndHandleFailures(DefaultLifecycleExecutor.java:387) at 
org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegments(DefaultLifecycleExecutor.java:348) at 
org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:180) at 
org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:328) at 
org.apache.maven.DefaultMaven.execute(DefaultMaven.java:138) at 
org.apache.maven.cli.MavenCli.main(MavenCli.java:362) at 
org.apache.maven.cli.compat.CompatibleMain.main(CompatibleMain.java:60) at 
org.codehaus.classworlds.Launcher.launchEnhanced(Launcher.java:315) at 
org.codehaus.classworlds.Launcher.launch(Launcher.java:255) at  
org.codehaus.classworlds.Launcher.mainWithExitCode(Launcher.java:430) at  
org.codehaus.classworlds.Launcher.main(Launcher.java:375) 

は、私たちのポンポンです:

<project> 
<modelVersion>4.0.0</modelVersion> 
<artifactId>mf-common</artifactId> 
<groupId>org.mathforum</groupId> 
<version>1.0-SNAPSHOT</version> 
<build> 
    <plugins> 

     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-compiler-plugin</artifactId> 
      <version>2.1</version> 
      <configuration> 
       <source>1.6</source> 
       <target>1.6</target> 
      </configuration> 
     </plugin> 

     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-antrun-plugin</artifactId> 
      <version>1.7</version> 
      <dependencies> 
       <dependency> 
        <groupId>com.sun</groupId> 
        <artifactId>tools</artifactId> 
        <version>1.5.0</version> 
        <scope>system</scope> 
        <systemPath>${java.home}/../lib/tools.jar</systemPath> 
       </dependency> 
       <dependency> 
        <groupId>org.aspectj</groupId> 
        <artifactId>aspectjtools</artifactId> <!-- apectj ant plugin --> 
        <version>1.6.1</version> 
       </dependency> 
       <dependency> 
        <groupId>ant</groupId> 
        <artifactId>ant-junit</artifactId> 
        <version>1.6.5</version> 
        <!--scope>test</scope --> 
       </dependency> 
       <dependency> 
        <groupId>junit</groupId> 
        <artifactId>junit</artifactId> 
        <version>4.8.2</version> 
        <!--scope>test</scope --> 
       </dependency> 
       <dependency> 
        <groupId>commons-net</groupId> 
        <artifactId>commons-net</artifactId> 
        <version>1.4.1</version> 
       </dependency> 
      </dependencies> 
      <executions> 
       <execution> 
        <id>compile</id> 
        <phase>compile</phase> 
        <configuration> 
         <target> 
          <ant antfile="${basedir}/build.xml"> 
           <target name="build" /> 
          </ant> 
         </target> 
        </configuration> 
        <goals> 
         <goal>run</goal> 
        </goals> 
       </execution> 
       <execution> 
        <id>test</id> 
        <phase>test</phase> 
        <configuration> 
         <target> 
          <ant antfile="${basedir}/build.xml"> 
           <target name="test" /> 
           <!-- need test failure to fail the build --> 
          </ant> 
         </target> 
        </configuration> 
        <goals> 
         <goal>run</goal> 
        </goals> 
       </execution> 
      </executions> 
     </plugin> 
    </plugins> 
</build> 
<dependencies> 
    <dependency> 
     <groupId>ant</groupId> 
     <artifactId>ant-junit</artifactId> 
     <version>1.6.5</version> 
     <!--scope>test</scope --> 
    </dependency> 
    <dependency> 
     <groupId>junit</groupId> 
     <artifactId>junit</artifactId> 
     <version>4.8.2</version> 
     <!--scope>test</scope --> 
    </dependency> 
</dependencies> 
</project> 

そして、ここでは私のbuild.xmlある

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 

<project basedir="." default="build" name="mf-common" xmlns:aspectj="antlib:org.aspectj"> 
<path id="EAR Libraries.libraryclasspath" /> 
<path id="aspectj.libraryclasspath"> 
    <fileset dir="lib"> 
     <include name="*.jar" /> 
    </fileset> 
</path> 
<path id="common.classpath"> 
    <fileset dir="test/lib"> 
     <include name="*.jar" /> 
    </fileset> 
    <path refid="aspectj.libraryclasspath" /> 

    <path refid="EAR Libraries.libraryclasspath" /> 
</path> 

<taskdef resource="org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties"> 
    <classpath> 
     <path refid="common.classpath" /> 
     <pathelement location="lib/aspectjtools.jar" /> 
    </classpath> 
</taskdef> 

<property environment="env" /> 
<property name="debuglevel" value="source,lines,vars" /> 
<property name="target" value="1.6" /> 
<property name="source" value="1.6" /> 

<target name="init"> 
    <mkdir dir="bin" /> 
    <copy includeemptydirs="false" todir="bin"> 
     <fileset dir="src" excludes="**/*.launch, **/*.java" /> 
    </copy> 
    <copy includeemptydirs="false" todir="bin"> 
     <fileset dir="test/src" excludes="**/*.launch, **/*.java" /> 
    </copy> 
</target> 

<target name="clean"> 
    <delete dir="bin" /> 
    <delete file="dist/${ant.project.name}.jar" /> 
</target> 

<target depends="clean" name="cleanall" /> 

<target depends="init" name="build"> 
    <echo message="${ant.project.name}: ${ant.file}" /> 

    <javac debug="true" debuglevel="${debuglevel}" destdir="bin" source="${source}" target="${target}"> 
     <src path="src" /> 
     <classpath refid="common.classpath" /> 
    </javac> 
    <javac debug="true" debuglevel="${debuglevel}" destdir="bin" source="${source}" target="${target}"> 
     <src path="test/src" /> 
     <classpath refid="common.classpath" /> 
    </javac> 
    <copy includeemptydirs="false" todir="bin"> 
     <fileset dir="src" excludes="**/*.launch, **/*.java" /> 
    </copy> 
    <copy includeemptydirs="false" todir="bin"> 
     <fileset dir="test/src" excludes="**/*.launch, **/*.java" /> 
    </copy> 

    <echo message="after javac" /> 

    <aspectj:iajc debug="true" noweave="true" outJar="dist/mf-common.jar" sourceRootCopyFilter="**/CVS/*,**/*.java,**/*properties" sourceroots="bin/" source="${source}" target="${target}"> 
     <classpath refid="common.classpath" /> 
    </aspectj:iajc> 

</target> 

<target name="test" depends="build"> 
    <ant dir="${basedir}" inheritAll="false" antfile="test.xml" /> 
</target> 
</project> 

UPDATE:我々

はなぜMavenのは、JUnitの3で実行されていますJUnit 4を使うように言ったのですか?私はmvn -X testを実行し、何らかの理由でJUnit 3.8.1 jarファイルが引っ張られているのを見ましたが、なぜそれを理解するのか分かりません。

UPDATE:私はMavenのantrunプラグインはJUnitの3に引っ張らなっている方法について説明3.8.1 http://maven.apache.org/plugins/maven-antrun-plugin/dependencies.html

だから、JUnitのに依存していることを見つけ

だから私は何をしますか今? maven antrunプラグインを使用してbuild.xmlのantタスクを使用してJUnit 4テストを実行する方法はありますか?

+0

MavenのJunit依存関係が使用されていますか?おそらく、使用されているANTがアクセスするJunitライブラリです。 – ziggy

+0

私はそれを調べ、maven antrunプラグインはjunit 3.8.1に依存しているので、それが理由です。 – atroutt

+0

私はこれと同様のhttp://stackoverflow.com/questions/2021771/surefire-is-not-picking-up-junit-4-testsから学びましたが、プラグインを削除する以外に解決策はありません。紛争の原因となります。 mavenからantを実行する別の方法がない限り、antrunを削除することはできません。 – atroutt

答えて

1

これは、質問に直接答えではないですが、問題は(したがって、私が最初のコメントとしてそれを置く)を処理するために私の推薦:達人に移動

は、Antターゲットをラップすることにより行うべきではありません。 maven standard directory layoutを採用してください(少なくとも)。これを行うことができない場合は、コンフィギュレーション(Supor POM設定を上書きする)で標準のMavenテストソースのdirectoyを変更してみてください。最後に、コンパイルとテストのためにmaven build lifecycleを使用します。

1

この問題の解決策がまだ必要かどうかはわかりませんが、同様の問題がありました。

Junit 3テストランナーでjunit.framework.JUnit4TestAdapterクラスのJunit 4テストを実行できます。

だから、あなたは、単にクラスに次のメソッドの定義を追加することができ、例として、あなたのテストクラスを使用して:

public static junit.framework.Test suite() { 
    return new junit.framework.JUnit4TestAdapter(ExampleBaseObjectDaoTest.class); 
} 

テストは、その後のmavenで実行する必要があります。

このソリューションの欠点は、すべてのテストクラスにsuite()メソッドを追加する必要があることです。

+0

ありがとう!私はちょうど、上記のコメントで提案された@FrVaBeのように、mavenからantターゲットを実行しようとすることをあきらめました。 – atroutt

関連する問題