问题描述
我正在尝试对多模块 Scala 项目执行测试:
Myproject
-module A
-module B
模块 A 是前端,B 是带有测试的 Scala 项目。当我进入 Myproject/B 并执行 mvn scalatest:test(或 mvn test)时,它们执行得很好。但是,如果我从根文件夹尝试,我会收到一个错误,阻止我执行 mvn install 除非我跳过测试(这很糟糕)。
错误是:无法找到或加载主类 org.scalatest.tools.Runner。
显然,scalatest 正在寻找根文件夹中不存在的测试类,实际上只有主 pom.xml,所有源都在子模块 A 和 B 中
B 也有子模块,但如果我直接在 B 目录下运行 maven 命令,那里的测试会被执行并发现很好。真的,问题是当我在根目录下时。编译甚至 sbt-complier:testCompile 运行良好,实际上 scalatest:test 几乎立即失败:
[INFO] Myproject.......................................... FAILURE [ 1.108 s]
[INFO] B................................................... SKIPPED
[INFO] A................................................... SKIPPED
[ERROR] Failed to execute goal org.scalatest:scalatest-maven-plugin:2.0.0:test (test) on project Myproject: There are test failures -> [Help 1]
从 Myproject 中的 pom.xml 中提取:
<project>
<groupId>.....</groupId>
<artifactId>;;;;;;</artifactId>
<packaging>pom</packaging>
<description>Myproject</description>
<version>....</version>
<name>Myproject</name>
<modules>
<module>A</module>
<module>B</module>
</modules>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.scalatest</groupId>
<artifactId>scalatest_2.12</artifactId>
<version>${scalatest.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>com.google.code.sbt-compiler-maven-plugin</groupId>
<artifactId>sbt-compiler-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.scalatest</groupId>
<artifactId>scalatest-maven-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
<!-- I need to set that to true and override that in the pom.xml from the module B -->
</configuration>
</plugin>
</plugins>
</build>
</project>
谢谢。
解决方法
好的,所以我相信我找到了解决方案。 mvn clean 测试有效,所以这就是我所做的:基本上,在根 pom 中,我说忽略来自父级的配置,模块 A 中的相同内容,然后重新定义模块 B 中的所有内容。我不得不调整一个有点什么目标/ id 在 scalatest 中使用,但使用“mvn test”来自根目录或 B 模块目录工作。
在父 pom 中跳过了 Surefire 测试。
Myproject 和 A pom.xml,在 build/plugins 下:
<plugin>
<groupId>org.scalatest</groupId>
<artifactId>scalatest-maven-plugin</artifactId>
<inherited>false</inherited>
<executions>
<execution>
<id>test</id>
<phase>none</phase>
</execution>
</executions>
</plugin>
在 build/plugins 下的 B pom.xml 中:
<plugin>
<groupId>org.scalatest</groupId>
<artifactId>scalatest-maven-plugin</artifactId>
<executions>
<execution>
<id>scala-test</id>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
<configuration>
<reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory>
<junitxml>.</junitxml>
<skipTests>false</skipTests>
</configuration>
</plugin>