Why subproject sourceset dirs == project sourceset dirs?

(Fedor Belov) #1

Hello. I’m trying to copy subproject sources. The problem is that gradle returns same values for following properties:

println project.sourceSets.main.java.srcDirs
println project(":subProject").sourceSets.main.java.srcDirs

In both cases it returns path to parent project sources. What’s wrong?

(Peter Niederwieser) #2

Without more information, it’s impossible to say. Where exactly did you put these 'println’s?

(Fedor Belov) #3

Inside type: Copy task declaration

(Fedor Belov) #4

Ok, I’ve tested it with doLast and printed values are correct. I understand that ‘type: Copy’ executes in configuration stage. But I thought that sourceSets should be configured first. How can I correctly configure type: Copy task in this case (I need to copy static files of subproject)?

(Peter Niederwieser) #5

What gets configured first depends on what you configure where. For example, parent scripts get evaluated before child scripts. Wrapping the expression with a closure should help (e.g. ‘from { project(":subProject").sourceSets.main.java.srcDirs }’).

(Fedor Belov) #6

Thanks, it helped!

(Fedor Belov) #7

It’s funny… it works if I define sourceSet in subproject ‘build.gradle’. But it doesn’t work if I define it in ‘allprojects’ or ‘subprojects’: exception ‘Could not determine the dependencies of task ‘:subProject:buildStatic’.’ and only ‘main’ and ‘test’ source sets are configured. So much magic to understand =(

(Peter Niederwieser) #8

Impossible to say without knowing the details of your build. One thing that’s clear is that you need to apply the ‘java’ plugin for a particular project before accessing its ‘sourceSets’.

(Fedor Belov) #9

Yep, ‘apply plugin: ‘java’’ was applied in subproject’s ‘build.gradle’. Moved it to ‘subprojects’ and fixed problem. Looks like ‘java’ plugin overrides all sources sets applied before. Am I right?

(Peter Niederwieser) #10

The Java (base) plugin introduces the concepts of source sets. Before the plugin has been applied, there is no ‘project.sourceSets’.

(Fedor Belov) #11

ps. Thank you. Finally configured my build file as expected