将测试文件夹中的Scala类与Maven中的Java测试共享
我有一个混合了Java和Scala代码的Maven项目。我想使用scala测试文件夹中的一个辅助类进行Java测试。文件树如下所示,省略了包:将测试文件夹中的Scala类与Maven中的Java测试共享,java,scala,maven,unit-testing,Java,Scala,Maven,Unit Testing,我有一个混合了Java和Scala代码的Maven项目。我想使用scala测试文件夹中的一个辅助类进行Java测试。文件树如下所示,省略了包: + test/ + java/... - SomeTest.java + scala/... - Aux.scala - OtherTest.scala 我想从Aux.scala导入代码,以便在SomeTest.java类中使用。它在我的IDE中运行良好,其中所有文件夹都标记为测试文件夹。然而,在Maven中构建这个项目
+ test/
+ java/...
- SomeTest.java
+ scala/...
- Aux.scala
- OtherTest.scala
我想从Aux.scala
导入代码,以便在SomeTest.java
类中使用。它在我的IDE中运行良好,其中所有文件夹都标记为测试文件夹。然而,在Maven中构建这个项目时,我从Java编译器中得到了一个导入错误
如何配置Maven使用Scala测试代码进行Java测试?为了在Java测试编译阶段解决对Scala类的依赖,必须将
Scala Maven插件的testCompile
目标绑定到过程测试资源
阶段。这样,Scala类在编译Java测试类时就已经编译好了
下面的代码段应该可以做到这一点:
net.alchim31.maven
scala maven插件
3.1.4
scala测试编译
过程测试资源
测试编译
对于混合Java/Scala项目,mypom.xml
的完整构建元素如下所示:
<build>
<sourceDirectory>src/main/scala</sourceDirectory>
<testSourceDirectory>src/test/java</testSourceDirectory>
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.1.4</version>
<executions>
<!-- Run scala compiler in the process-test-resources phase, so that dependencies on
scala classes can be resolved later in the (Java) test-compile phase -->
<execution>
<id>scala-test-compile</id>
<phase>process-test-resources</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
</plugin>
<plugin>
<groupId>org.scalatest</groupId>
<artifactId>scalatest-maven-plugin</artifactId>
<version>1.0</version>
<configuration>
<reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory>
<stdout>W</stdout> <!-- Skip coloring output -->
</configuration>
<executions>
<execution>
<id>scala-test</id>
<goals>
<goal>test</goal>
</goals>
<configuration>
<suffixes>(?<!(IT|Integration))(Test|Suite|Case)</suffixes>
</configuration>
</execution>
<execution>
<id>integration-test</id>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<suffixes>(IT|Integration)(Test|Suite|Case)</suffixes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
src/main/scala
src/test/java
net.alchim31.maven
scala maven插件
3.1.4
scala测试编译
过程测试资源
测试编译
org.apache.maven.plugins
maven surefire插件
2.18.1
org.scalatest
scalatest maven插件
1
${project.build.directory}/surefire报告
W
scala试验
测试
(?!(IT集成)(测试套件案例)
集成测试
集成测试
测试
(IT集成)(测试套件案例)
似乎有效,并提供了简洁的解决方案。可悲的是,对我来说,这些专家配置中的大多数仍然是巫毒。