首页 > 解决方案 > Intellij gradle package name incorrect error

问题描述

I have just opened this (https://github.com/codetojoy/easter_eggs_for_gradle/tree/master/egg_StackOverflow_51685286) project in IntelliJ with the following files:

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

and build.gradle contains a configuration for sourceSets like so:

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

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

and On the FooServiceTestCase.java file, I am getting an error on the package line saying Package name 'net.codetojoy.tests' does not correspond to the file path.

I think it is because of the customised source and test set. But I am unsure how to fix it....

please help

标签: gradleintellij-ideabuild.gradleintellij-15

解决方案


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

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

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


推荐阅读