【问题标题】:Running tests with maven packaging type "pom"使用 Maven 包装类型“pom”运行测试
【发布时间】:2012-02-09 05:46:30
【问题描述】:

当我的 pom 设置为打包类型“pom”时,我在运行单元测试时遇到了一些问题。起初,它说这个项目不需要目标,所以我将 maven-surefire-plugin 添加到我的 pom.xml 以将测试阶段绑定到 maven-surefire-plugin 测试目标。

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.9</version>
            <executions>
                <execution>
                    <phase>test</phase>
                    <goals>
                        <goal>test</goal>
                    </goals>
                </execution>
            </executions>
        </plugin> 

现在,surefire 插件正在执行,但它说没有要运行的测试。如果我将打包类型更改为 jar 并运行 mvn test,那么它会获取我的测试文件。

当我运行 mvn test -X 时,它显示“testSourceDirectory = C:\dev\dsl\src\test\java”,这是正确的位置。包装类型“pom”与“jar”的测试位置是否不同?我尝试添加

            <configuration>
                <testSourceDirectory>src/test/java</testSourceDirectory>
            </configuration>

到万无一失的插件,但它根本没有帮助。

【问题讨论】:

  • Pom 打包仅用于元数据项目,不是吗?你想做什么?
  • 我实际上处于同样的场景中——我有一个系统测试模块,我只想运行它,但不想从中生成一个 jar。但是,如果不实际指定 pom 之外的其他东西的包装类型,我就无法运行测试。我该怎么做(这可能值得一个单独的问题)。
  • @DaveNewton 我想这样做是因为它不是 Java 应用程序,我想生成一个带有数据库脚本的 zip 文件作为工件。
  • @whaley 如下所示,您需要将 compiler:testCompile 目标绑定到测试编译阶段,并将 surefire:test 目标绑定到测试阶段。

标签: maven junit pom.xml maven-surefire-plugin


【解决方案1】:

正如 Dave 所说,如果您使用 pom 打包,它只会执行以下生命周期目标。参考this相关maven文档。

  • 安装
  • 部署

如果您需要它来运行任何其他目标,则需要明确指定它。例如,

mvn clean compiler:testCompile surefire:test

【讨论】:

  • 哦,谢谢!我知道它与测试阶段无关,但我并没有想到我也需要运行 compiler:testCompile。
  • 还有一个问题。我以为我可以将 compiler:testCompile 绑定到测试阶段,但它似乎不起作用。有什么建议? org.apache.maven.pluginsmaven-compiler-plugin2.3.2test-编译testCompile
  • 没关系,我有两个 maven-compiler-plugin 部分,所以它被覆盖了
  • 我将它用作 mvn clean package 编译器:testCompile surefire:test 因为我在没有包的情况下出现错误
猜你喜欢
  • 2012-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-27
  • 1970-01-01
  • 2019-05-08
  • 1970-01-01
  • 2013-06-06
相关资源
最近更新 更多