Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the main class classpath for JavaExec task in Gradle?

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.

like image 828
acejazz Avatar asked Oct 27 '25 11:10

acejazz


1 Answers

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'
}
like image 140
Crazyjavahacking Avatar answered Oct 29 '25 07:10

Crazyjavahacking



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!