Gradle How to include runtimeOnly dependencies in JavaExec classpath?

31.2k views Asked by At

Gradle How to include runtimeOnly dependencies in JavaExec classpath? For example,

subproject foo:

dependencies {
    runtimeOnly files('libs/hello.jar')
}

subproject bar:

dependencies {
    compile project(':foo')
}

task execHello(type: JavaExec, dependsOn: 'compileJava') {      
     classpath = configurations.runtime         
     main 'myPackage.Hello'
}

the main class myPackage.Hello is defined in the libs/hello.jar that is a runtimeOnly dependency for project foo.

configurations.runtime does not contain the runtimeOnly dependency hello.jar. If I changed the runtimeOnly dependency as api dependency in project foo, it will work.

classpath = configurations.runtime + configuration.runtimeOnly

Error: runtimeOnly can not be explicitly resolved. How to add the hello.jar in the JavaExec classpath?

1

There are 1 answers

7
Vampire On BEST ANSWER

runtime and runtimeOnly are for declaring the dependencies. To use the dependencies you should use the configuration runtimeClasspath as per the docs at https://docs.gradle.org/current/userguide/java_library_plugin.html#sec:java_library_configurations_graph.