1

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

Jofbr
  • 455
  • 3
  • 23

1 Answers1

1

The test source set definition needs to look more like the main one:

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

The reason the previous answer worked in command line but not the IDE is that for the Java compiler, the location of a source file is irrelevant. The package instruction is used to correctly place the produced class file, but the source file does not have to be in a matching directory structure. But usually an IDE performs this check.

Louis Jacomet
  • 13,661
  • 2
  • 34
  • 43