首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从maven运行ant目标

从maven运行ant目标
EN

Stack Overflow用户
提问于 2012-03-07 20:20:18
回答 3查看 3.7K关注 0票数 2

我们首先在maven中包装我们的ant构建,从而开始移动到maven。

我们可以让maven构建项目,但不能运行测试。我们试图在build.xml中从maven执行ant测试目标。当然,当我们从ant运行测试目标时,一切都很好。

下面是为一个测试类显示的错误(其他测试类相同)。这个类中有几个测试方法,但是没有一个在运行。这似乎是一个JUnit版本问题;测试是JUnit 4测试,如果我们更改测试类以扩展TestCase (JUnit 3样式),它会找到测试。

代码语言:javascript
复制
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)

这是我们的pom:

代码语言:javascript
复制
<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

代码语言:javascript
复制
<?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>

更新:

那么,当我们告诉maven使用JUnit 4时,它为什么要使用JUnit 3呢?我运行了mvn -X测试,发现JUnit 3.8.1 jar是出于某种原因被拉进来的,但我不知道如何找出原因。

更新:

我发现maven antrun插件依赖于junit 3.8.1 http://maven.apache.org/plugins/maven-antrun-plugin/dependencies.html

这就解释了Junit 3是如何被拉进来的。

那我现在该怎么办?是否有一种方法可以通过使用maven antrun插件在JUnit中执行build.xml中的ant任务来运行我的build.xml 4测试?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-08-09 08:18:37

这不是对问题的直接回答,而是我建议处理这个问题(因此我首先作为评论):

移动到maven不应该通过包装ant目标来完成。尝试(至少)采用maven标准目录布局。如果无法做到这一点,请尝试通过配置更改标准的maven测试源目录(覆盖Supor POM设置)。最后,使用maven构建生命周期来编译和测试。

票数 1
EN

Stack Overflow用户

发布于 2012-08-06 09:43:40

不确定你是否还需要解决这个问题,但我们也有类似的问题。

您可以使用junit.framework.JUnit4TestAdapter类在Junit 3测试运行程序中运行Junit 4测试。

因此,使用测试类作为示例,您可以简单地向类添加以下方法定义:

代码语言:javascript
复制
public static junit.framework.Test suite() {
    return new junit.framework.JUnit4TestAdapter(ExampleBaseObjectDaoTest.class);
}

然后,测试应该在maven中运行。

此解决方案的缺点是需要将suite()方法添加到所有测试类中。

票数 1
EN

Stack Overflow用户

发布于 2020-09-02 18:38:09

以防有人还在为这事而挣扎..。

我遇到了同样的“没有测试发现”的问题,并尝试添加扩展TestCase和添加套件()方法,如上面所示。这种工作方式(取决于所使用的TestRunner )并不是很令人愉快。

我偶然发现了一个运行Ant JUnit4测试任务的更好的解决方案:

  1. 在maven-antrun-plugin配置中包括以下依赖项:
代码语言:javascript
复制
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.13</version>
</dependency>
<dependency>
    <groupId>org.apache.ant</groupId
    <artifactId>ant</artifactId>
    <version>1.9.4</version>
</dependency>
<dependency>
    <groupId>org.apache.ant</groupId>
    <artifactId>ant-junit</artifactId>
    <version>1.9.4</version>
</dependency>
<dependency>
    <groupId>org.apache.ant</groupId>
    <artifactId>ant-junit4</artifactId>
    <version>1.9.4</version>
</dependency>

请注意,我使用的是antrun plugin版本3.0.0和1.9.4版本的依赖项。此解决方案也适用于1.9.15版本的依赖项。没有使用1.10.x依赖项,但我没有非常努力。

  1. 为Ant junit目标创建一个执行,如下所示:
代码语言:javascript
复制
<execution>
    <id>ant-unit-tests</id>
    <phase>whatever</phase>
    <goals><goal>run</goal></goals>
    <configuration>
        <target>
            <property name="maven_test_classpath" refid="maven.test.classpath"/>
            <property name="maven_plugin_classpath" refid="maven.plugin.classpath"/>
            <ant antfile="${project.basedir}/your_build.xml" target="your_ant_target" />
        </target>
    </configuration>
</execution>

请注意,我已经将maven_test_classpath和maven_plugin_classpath传递给Ant,因为我希望Maven管理依赖关系并生成适当的类路径。

  1. 在build.xml文件任务配置中,包括引用从Maven传入的类路径的子元素:
代码语言:javascript
复制
<junit... >
    ...
    <classpath>
        <pathelement path="${maven_test_classpath}" />
        <pathelement path="${maven_plugin_classpath}" />
    </classpath>
    ...
</junit>

注意,我发现maven_plugin_classpath实际上并不是必需的。我保留了它的配置,因为我不明白为什么不需要它。YMMV

结果运行各种复杂的JUnit4单元和在Ant中使用junit、batchtest和junitreport元素执行的集成测试。不需要扩展TestCase和添加静态套件()方法。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9608453

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档