API: How can one programmatically obtain the location of the source files in a (Java) build?

Hi all,

I’m new to Gradle and have been tasked with developing a plugin to run some static analysis (HP Fortify) on a set of sources (Only Java at the moment).

I have a working prototype which simply executes the command line behind the scenes. The trouble is that all of the options the command line needs have to be set in the build script. It has been requested that I make it more gradle like and assume some reasonable conventions by defult. So if nothing on the task is configured, I want to run a scan against whatever the Java plugin feels is the source code and place the results in some known location relative to the top level dir of the project.

So, my question is, how can I programmatically retrieve this information (the location of the sources and the root directory of the project) from within my custom task?

For bonus points: Is there any way to determine which languages are being used in the build and extract the source location for each of them?

I have found a method called getSource() in both:

org.gradle.api.tasks.SourceTask
org.gradle.language.nativeplatform.tasks.AbstractNativeCompileTask

Perhaps that will inspire some ideas?

Cheers!!!

Robert

I might have found a way:

    File rootDir = this.getProject().getRootDir();
    TaskContainer tasks = this.getProject().getTasks();
    for (JavaCompile task: tasks.withType(JavaCompile)) {
            FileTree sources = task.getSource();
            // Do something here
        }
    }

Does this look reasonable? Any better way?

Cheers,

Robert

Hey you can simplify your code a bit:

File rootDir = this.getProject().getRootDir();
TaskContainer tasks = this.getProject().getTasks();
for(JavaCompile task: tasks.withType(JavaCompile)) {
    FileTree sources = task.getSource();
        // Do something here
}

Awesome! Thanks! I just noticed that moments before your reply :slight_smile: