When do I need to use runtime at compile time in Java / Gradle?

From my understanding, Gradle transfers all dependencies compileas dependencies runtime.

What is an instance when you should use only runtime? All child dependencies are captured from compileand pulled into compilation when called gradle build.

For example, when I make a difference with what is printed when called

> gradle -q dependencies

The list printed for compilation and the runtime is identical. An example output can be displayed as follows:

+--- org.springframework.boot:spring-boot-starter-web: -> 1.5.4.RELEASE
|    +--- org.springframework.boot:spring-boot-starter:1.5.4.RELEASE
|    |    +--- org.springframework.boot:spring-boot:1.5.4.RELEASE
|    |    |    +--- org.springframework:spring-core:4.3.9.RELEASE
|    |    |    \--- org.springframework:spring-context:4.3.9.RELEASE
|    |    |         +--- org.springframework:spring-aop:4.3.9.RELEASE
|    |    |         |    +--- org.springframework:spring-beans:4.3.9.RELEASE
|    |    |         |    |    \--- org.springframework:spring-core:4.3.9.RELEASE
|    |    |         |    \--- org.springframework:spring-core:4.3.9.RELEASE
|    |    |         +--- org.springframework:spring-beans:4.3.9.RELEASE (*)
|    |    |         +--- org.springframework:spring-core:4.3.9.RELEASE
|    |    |         \--- org.springframework:spring-expression:4.3.9.RELEASE
|    |    |              \--- org.springframework:spring-core:4.3.9.RELEASE
|    |    +--- org.springframework.boot:spring-boot-autoconfigure:1.5.4.RELEASE
|    |    |    \--- org.springframework.boot:spring-boot:1.5.4.RELEASE (*)
|    |    +--- org.springframework.boot:spring-boot-starter-logging:1.5.4.RELEASE
|    |    |    +--- ch.qos.logback:logback-classic:1.1.11
|    |    |    |    +--- ch.qos.logback:logback-core:1.1.11
|    |    |    |    \--- org.slf4j:slf4j-api:1.7.22 -> 1.7.25
|    |    |    +--- org.slf4j:jcl-over-slf4j:1.7.25
|    |    |    |    \--- org.slf4j:slf4j-api:1.7.25

, , , - . , ?

+4
1

. :

package net.codetojoy;

public class App {
    public static void main(String[] args) throws Exception {
        Class c = Class.forName("org.apache.commons.lang3.StringUtils");
        Object object = c.getConstructor().newInstance();
        System.out.println("object is : " + object);
    }
}

StringUtils Apache Commons Lang. ( , , libA libB).

, , jar. . build.gradle . application, .

apply plugin: 'java'
apply plugin: 'application'

repositories {
    jcenter()
}

dependencies {
    runtime group: 'org.apache.commons', name: 'commons-lang3', version: '3.6'
}

mainClassName = 'net.codetojoy.App'

:

$ gradle run -q
object is : org.apache.commons.lang3.StringUtils@4aa298b7
+5

Source: https://habr.com/ru/post/1683916/


All Articles