You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让IntelliJ使用Maven中scala-maven-plugin配置的编译选项?

Scala + Testcontainers + Spring Boot集成测试:IntelliJ编译报错,Maven命令行正常

环境

  • IDE:IntelliJ IDEA
  • 构建工具:Maven
  • 技术栈:Scala、Spring Boot、Testcontainers(PostgreSQL)

问题情况

我基于Maven搭建了Scala项目,想添加Testcontainers和Spring Boot的集成测试,编写的测试代码如下:

object IntegrationTest:
    private val database = new PostgreSQLContainer(DockerImageName.parse("postgres"))

    if DockerClientFactory.instance().isDockerAvailable then database.start()

    @DynamicPropertySource
    def databaseProperties(registry: DynamicPropertyRegistry): Unit =
        registry.add("spring.datasource.url", () => database.getJdbcUrl)
        registry.add("spring.datasource.username", () => database.getUsername)
        registry.add("spring.datasource.password", () => database.getPassword)

编译时Scala编译器报出数据竞争相关错误,示例如下:

scalac: possible data race involving globally reachable variable parameters in class JdbcDatabaseContainer: java.util.Map[String, String]
use -Ylog:checkReentrant+ to find out more about why the variable is reachable.
(共5条类似错误)

已做的尝试

我在pom.xml的测试编译阶段添加了-Ysafe-init编译选项,配置如下:

<plugins>
  <plugin>
    <groupId>net.alchim31.maven</groupId>
    <artifactId>scala-maven-plugin</artifactId>
    <version>4.8.1</version>
    <executions>
      <execution>
        <id>scala-test-compile</id>
        <phase>process-test-resources</phase>
        <goals>
          <goal>testCompile</goal>
        </goals>
        <configuration>
          <args>
            <arg>-Ysafe-init</arg>    <!-- 解决编译错误的选项 -->
          </args>
        </configuration>
      </execution>
    </executions>
  </plugin>
</plugins>

执行mvn test命令时,项目可以正常编译并运行测试,但在IntelliJ IDEA内直接运行测试或主应用时,还是会出现之前的编译错误。

我尝试在IntelliJ的Settings > Build, Execution, Deployment > Compiler > Scala Compiler > Additional compiler options中添加-Ysafe-init,但这个设置没有生效。

求助

怎么让IntelliJ IDEA正确应用-Ysafe-init编译选项,解决这个编译报错的问题?

内容的提问来源于stack exchange,提问作者miracoly

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 14:14:55