【问题标题】:Share Scala class in test folder with Java tests in Maven与 Maven 中的 Java 测试共享测试文件夹中的 Scala 类
【发布时间】:2023-03-09 23:50:01
【问题描述】:

我有一个混合了 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 maven unit-testing


    【解决方案1】:

    为了在 Java 测试编译阶段解决对 Scala 类的依赖,您必须将scala-maven-plugintestCompile 目标绑定到process-test-resources 阶段。这样,当你编译 Java 测试类时,Scala 类就已经编译好了。

    下面的 sn-p 应该可以解决问题:

    <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 的混合 Java/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>(?&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>
    

    【讨论】:

    • 似乎可行,而且是简洁的解决方案。遗憾的是,这些 Maven 配置中的大多数对我来说仍然是 Voodoo。
    猜你喜欢
    • 2014-06-22
    • 1970-01-01
    • 2019-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多