I got that if I want to run a main from a Main class, by using the sourceSets.main.runtimeClasspath classpath, I have to put the Main class inside of src/main/java and use something like:
apply plugin: 'java'
dependencies {
}
task myTask (type: JavaExec){
    dependsOn classes
    classpath sourceSets.main.runtimeClasspath
    main = 'Main'
}
What I want is understand how I can specify a different classpath from which to retrieve the class containing the main(). 
What if I want to run the main from a class which is not in src/main/java but it is in the same folder as the build.gradle?
I'm aware that it has no sense to do something like that, but I wish to find a solution as an exercise to learn Gradle.
As you still need to compile such class and in the case the class is not in the standard src/main/java directory, you will need to define additional SourceSet to that path and use the same approach as you described:
sourceSets {
    main {
        custom {
            srcDirs = ['custom/path']
        }
    }
}
task myTask (type: JavaExec){
    dependsOn classes
    classpath sourceSets.custom.runtimeClasspath
    main = 'Main'
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With