gpt4 book ai didi

java - 与 Maven 中的 Java 测试共享测试文件夹中的 Scala 类

转载 作者:行者123 更新时间:2023-11-30 08:08:56 25 4
gpt4 key购买 nike

我有一个混合了 Java 和 Scala 代码的 Maven 项目。我想使用位于 scala 测试文件夹中的辅助类进行 Java 测试。文件树如下所示,省略了包:

+ test/
+ java/...
- SomeTest.java
+ scala/...
- Aux.scala
- OtherTest.scala

我想从 Aux.scala 导入代码以用于 SomeTest.java 类。它在我的 IDE 中运行良好,其中所有文件夹都被标记为测试文件夹。但是,在 Maven 中构建此项目时,Java 编译器出现导入错误。

如何配置 Maven 以使用 Scala 测试代码进行 Java 测试?

最佳答案

为了在 Java 测试编译阶段解决对 Scala 类的依赖,您必须将 scala-maven-plugintestCompile 目标绑定(bind)到 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>

我的用于混合 Java/Scala 项目的 pom.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>(?&lt;!(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>

关于java - 与 Maven 中的 Java 测试共享测试文件夹中的 Scala 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32845047/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com