4

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?

eastwater
  • 4,624
  • 9
  • 49
  • 118
  • Are you using the [Java plugin](https://docs.gradle.org/3.3/userguide/java_plugin.html) (`apply plugin: 'java'`) or the [Java Library plugin](https://docs.gradle.org/current/userguide/java_library_plugin.html#sec:java_library_configurations_graph) (`apply plugin: 'java-library'`)? – Lukas Körfer Jul 04 '17 at 20:42
  • java-library. compile project(":foo") is not forcing to build jar. – eastwater Jul 04 '17 at 20:51

1 Answers1

8

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.

Vampire
  • 35,631
  • 4
  • 76
  • 102
  • runtimeClasspath does include runtimeOnly dependencies, but some jars from dependent projects in the classpath do not get built. i.e. task jar is not called before the jar is being used. – eastwater Jul 04 '17 at 19:49
  • workaround: add dependsOn to build jar before used in runtimeClasspath. – eastwater Jul 04 '17 at 20:51
  • no, `dependsOn` is the wrong way. but you would need to show me an example to be able to tell you what you did wrongly. How do you depend on those jars? – Vampire Jul 04 '17 at 22:22
  • If project A depends on project B with dependencies { compile project(':B')}, the sequence of jar tasks is not guaranteed. Is there a nice way to say that task B.jar is called before A.jar without using dependsOn? – eastwater Jul 05 '17 at 20:21
  • I would need to see the build scripts. The order should be guaranteed if you made the build scripts correctly. – Vampire Jul 05 '17 at 20:56
  • There is no documentation for when to use `runtime` vs `runtimeOnly`, although based on my experience, their behaviors are very different, especially when used with plugins like Spring Boot Gradle plugin. The link in your answer doesn't even mention `runtime` configuration. – Abhijit Sarkar Feb 16 '18 at 23:52
  • Well, that's easy, because there is only one of them. If you use the java library plugin, there is `runtimeOnly`, if you use the java plugin, there is `runtime`: https://docs.gradle.org/current/userguide/java_plugin.html#N16B17 – Vampire Feb 17 '18 at 01:38