Intellij gradle package name incorrect 错误

Intellij gradle package name incorrect error

我刚刚在 IntelliJ 中打开了这个 (https://github.com/codetojoy/easter_eggs_for_gradle/tree/master/egg_Whosebug_51685286) 项目,其中包含以下文件:

src/net/codetojoy/Foo.java
src/net/codetojoy/service/FooService.java
src/net/codetojoy/tests/FooServiceTestCase.java

build.gradle 包含 sourceSets 的配置,如下所示:

sourceSets {
    main {
        java {
            srcDir 'src'
            exclude 'net/codetojoy/tests/*'
        }
    }
}

sourceSets.test.java.srcDir 'src/net/codetojoy/tests'

并且在 FooServiceTestCase.java 文件中,我在包行上收到一条错误消息 Package name 'net.codetojoy.tests' does not correspond to the file path

我认为是因为自定义的源和测试集。但我不确定如何修复它....

请帮忙

test 源集定义需要看起来更像 main 一个:

sourceSets {
  main {
    java {
        srcDir 'src'
        exclude 'net/codetojoy/tests/*'
    }
  }
  test {
    java {
      srcDir 'src'
      include 'net/codetojoy/tests/*
    }
}

之前的答案在命令行中有效但在 IDE 中无效的原因是对于 Java 编译器,源文件的位置无关紧要。 package 指令用于正确放置生成的 class 文件,但源文件不必位于匹配的目录结构中。但通常 IDE 会执行此检查。