There is the following structure

MainProject settings.gradle build.gradle Proj1 build.gradle Proj2 build.gradle 

In the MainProject/settings.gradle I prescribe projects

 include 'Proj1', 'Proj2' 

Proj2 depends on Proj1 .

If I write in Proj2/build.gradle

 dependencies { compile project(':Proj1'); } 

Gradl does not see classes from Proj . If I describe the same thing in MainProject/build.gradle

 project(':Proj2') { dependencies { compile project(':Proj1') } } 

Then everything will compile correctly (from the account that the Proj2/gradle.build will not be).

Why option 1 does not work? This is better in the sense that all the logic will be contained in its own build.gradle build.gradle , and not in one general.

PS Update

In the comments asked to make a minimal gitovogo project .

If you directly clone and run gradle compileJava from the root, everything will be compiled.

If you do the following:

  1. In the root build.gradle comment out from 5 to 10 lines project(':Proj2') {...}

  2. Rename Proj2/b2uild.gradle2 to Proj2/build.gradle and run gradle compileJava - then the error described above will occur.

my @ ubuntu: ~ / Projects / TempGradle $ gradle -version


Gradle 3.0

Build time: 2016-08-15 13:15:01 UTC

Revision: ad76ba00f59ecb287bd3c037bd25fc3df13ca558

Groovy: 2.4.7

Ant: Apache Ant (TM) version 1.9.6 compiled on June 29 2015

JVM: 1.8.0_102 (Oracle Corporation 25.102-b14)

OS: Linux 3.13.0-96-generic amd64

  • My first option works fine. Show exactly what commands you run the build in both versions. And if you can, make a minimal project on github that demonstrates your problem. - Roman
  • I use gradle compileJava from MainProject. And what version do you have? I have 3.0. Concerning the githaba project, I'll do it a little later. - Andrew Bystrov
  • I have gradle 2.14, but I do not think it affects. - Roman
  • @Roman updated the question - Andrew Bystrov

1 answer 1

The project is not going to Proj2/build.gradle due to the fact that Proj2/build.gradle is different from the project(':Proj2') { ... } block of the root project. Proj2/build.gradle has a section

 sourceSets { main { java { srcDir('src') } } } 

which is not in MainProject/build.gradle . Because of it (more precisely, see below) and an error occurs. Without this section, you are not Proj2/src/ compile java-sources from Proj2/src/ (by default, gradle searches for java-sources in Proj2/src/main/java/ , see Java project layout ).

Due to the incorrect location of the sources, Proj1 and Proj2 are actually empty projects, therefore no compilation errors occur. After adding the above section, Proj2 becomes non-empty and a compilation error occurs, because Proj1 is still empty.

To fix it, move the sources to the right place and remove the sourceSets { ... } from Proj2/build.gradle . If the source cannot be moved for any reason, configure the sourceSets for all projects.

  • So, if I register the sourceSets for all projects, then I will be able to declare the dependency in Proj2 and the like? - Andrew Bystrov
  • Yes, thank you very much for the clarification. - Andrew Bystrov
  • @AndrewBystrov Yes, dependencies can be declared in Proj2/build.gradle . - Roman