首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用Maven中的Java测试共享测试文件夹中的Scala类

用Maven中的Java测试共享测试文件夹中的Scala类
EN

Stack Overflow用户
提问于 2015-09-29 13:16:57
回答 1查看 1.4K关注 0票数 2

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

代码语言:javascript
运行
复制
+ test/
  + java/...
    - SomeTest.java
  + scala/...
    - Aux.scala
    - OtherTest.scala

我想从Aux.scala导入代码,以便在SomeTest.java类中使用。它在我的IDE中运行良好,其中所有文件夹都标记为测试文件夹。但是,在用Maven构建这个项目时,我会从Java编译器那里得到一个导入错误。

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-09-29 14:24:20

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

下面的代码段应该可以做到这一点:

代码语言:javascript
运行
复制
<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项目的完整构建元素如下:

代码语言:javascript
运行
复制
<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>
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32845047

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档