Inside a build.gradle file, what's the most concise way to get a list of all packages in a SourceSet?
I can get it by parsing each source file, as below —
def sourceSet = project.sourceSets.findByName('test')
def pattern = Pattern.compile('package (\\S+);')
def packages = []
sourceSet.allSource.each {
for (def scan = new Scanner(it); scan.hasNext();) {
def matcher = scan.nextLine() =~ pattern
if (matcher.matches()) {
packages << matcher[0][1]
break
}
}
}
packages.unique(true)
// allows, e.g.
packages.each { println it }
— but it seems as though there ought to be a better way.
Edited to add: I need this information in order to pass additional arguments to the JVM at compile time, so I need to get it from the source, before compilation.