What is the difference between sourceSets.main.java and sourceSets.main.allJava

I do not exactly understand the diffference between the following two expressions:

sourceSets.main.java
sourceSets.main.allJava

This snippets gives a different output:

task testTask << {
 println ""
 ext.allJavaCount = 0
 sourceSets.main.allJava.include("**/beans/**").each {
  println it
  allJavaCount ++
 }
   println ""
 ext.javaCount = 0
 sourceSets.main.java.include("**/beans/**").each {
  println it
  javaCount++
 }
   println sourceSets.main.java.includes
 println sourceSets.main.allJava.includes
   println "allJava count: $allJavaCount"
 println "java count:
  $javaCount"
}

The output looks like this

[**/beans/**]
[**/beans/**]
allJava count: 200
java count:
  33

So where is the difference ?

Thanks for the clarification in advance :slight_smile:

sourceSets.main.java returns all java files that are supposed to be compiled by the java compiler. sourceSets.main.allJava also returns the java files that are located in the e.g. scala or groovy source directories and are normally compiled in joint compilation by the scala/groovy compiler.

hope that helps.

cheers, René

Sounds reasonable, but in my project there are only java files. But by adding the ‘include’ i would expect to see only the matching files. Since both the ‘java’ and ‘allJava’ are both of type ‘SourceDirectorySet’

Now it gets a bit weird, when i add a second task which depends on the first one, like so:

task testTask2(dependsOn: testTask) << {
 println ""
 ext.allJavaCount = 0
 sourceSets.main.allJava.each {
  println it
  allJavaCount ++
 }
   println ""
 ext.javaCount = 0
 sourceSets.main.java.each {
  println it
  javaCount++
 }
   println sourceSets.main.java.includes
 println sourceSets.main.allJava.includes
   println "allJava count: $allJavaCount"
 println "java count:
  $javaCount"
}

Then the output is different from the first task:

[**/beans/**]
[**/beans/**]
allJava count: 33
java count:
  33

The ‘testTask’ (like in the first post) redefines the sourceSets by adding an include pattern and is then used in the second task which gives the expected result.

When i add an includes to a sourceSet i modify it, is there a way to select a subset of a sourceSet without modifying it?