我有一个带有混合Java和Scala代码的Maven项目。我想使用一个位于scala测试文件夹中的辅助类来进行Java测试。文件树如下所示,省略了包:
+ test/
+ java/...
- SomeTest.java
+ scala/...
- Aux.scala
- OtherTest.scala
我想从Aux.scala
导入代码,以便在SomeTest.java
类中使用。它在我的IDE中运行良好,其中所有文件夹都标记为测试文件夹。但是,在用Maven构建这个项目时,我会从Java编译器那里得到一个导入错误。
如何将Maven配置为使用Scala测试代码进行Java测试?
发布于 2015-09-29 14:24:20
为了解决Java测试编译阶段对Scala类的依赖,您必须将scala-maven-plugin
的scala-maven-plugin
目标绑定到process-test-resources
阶段。这样,在编译Java测试类时就已经编译了Scala类。
下面的代码段应该可以做到这一点:
<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>
我的混合pom.xml
/Scala项目的完整构建元素如下:
<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>
https://stackoverflow.com/questions/32845047
复制相似问题