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?
Without more information, it’s impossible to say. Where exactly did you put these 'println’s?
Inside type: Copy task declaration
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)?
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 }’).
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 =(
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’.
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?
The Java (base) plugin introduces the concepts of source sets. Before the plugin has been applied, there is no ‘project.sourceSets’.
ps. Thank you. Finally configured my build file as expected