I need to run a main method on a java class from my build.gradle file. But how do avoid to specify the whole classpath manually like I am doing below:
task(runSimple, dependsOn: 'classes', type: JavaExec) {
main = 'com.MyMain'
classpath=sourceSets.main.runtimeClasspath
classpath+=sourceSets.test.runtimeClasspath
classpath+=sourceSets.main.resources
classpath+=configurations.compile.dependencies // all dependencies for this project - is empty :-(
}
The above does not work because the classpath does not contain the projects dependencies. Is there a way to specify that the classpath should be the one used when building this project which also contains all necessary dependencies?
If I add the following to my build.gradle file:
configurations.compile.dependencies.each { println it }
nothing is printed even though running this from commandline:
return values of type Configuration. Reading the docs on this last link we can see that, quote:
Configuration is an instance of a FileCollection that contains all dependencies…
Ok, so what we need for the JavaExec.classpath is something of type FileCollection and that is exactly what the “configurations.compile” expression returns.
So to sum this up, I think the following should work for you:
Not really. I can only make it work if I do it in the afterEvaluate scope and on the main sourceSet:
afterEvaluate{
main = 'com.MyMain'
classpath=sourceSets.main.runtimeClasspath
//
classpath=configurations.runtime , does not work
println "the classpath: "
classpath.each { println it }
}
With the above it finds the class but I get another error that a .properties file cannot be found which is located the test resources (I know that this should only be used when running tests):
Therefore I have added the test resources like this:
afterEvaluate{
main = 'com.MyMain'
classpath=sourceSets.main.runtimeClasspath
classpath=+sourceSets.test.resources
//
classpath=configurations.runtime , does not work
println "the classpath: "
classpath.each { println it }
}
but it gives an error:
groovy.lang.MissingMethodException: No signature of method: org.gradle.api.internal.file.DefaultSourceDirectorySet.positive() is applicable for argument types: ()
Any suggestions on how to include resources from the test sourceSet when running the above class?