什么时候使用 运行-time 而不是 Java/Gradle 中的编译时依赖项?
When is a time to use Run-time over Compile-time dependencies in Java/Gradle?
据我了解,Gradle 会将所有 compile
依赖项转为 runtime
依赖项。
应该只使用 runtime
的实例是什么?所有子依赖项都从 compile
中获取,并在调用 gradle build
时拉入编译。
例如,当我对调用时打印的内容进行比较时
> gradle -q dependencies
为 compile 和 runtime 打印的列表是相同的。示例输出可能显示以下两者:
+--- 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
我看过这个 answer 这有助于解释 compile 和 runtime 之间的区别,但它仅表明运行时是您的代码实际执行依赖项的时间。你什么时候会有 runtime 依赖,但没有 compile 时间?
一个典型的案例是通过反射动态创建类。作为一个人为的例子,考虑这个应用程序:
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);
}
}
它将从 Apache Commons Lang 创建一个 StringUtils
的对象。 (这个例子很愚蠢;考虑 libA
将有效地为 libB
中的 类 执行此操作的情况。
没有编译时依赖性,因此没有理由用 jar 来增加编译时类路径的负担。然而在 运行-time,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
据我了解,Gradle 会将所有 compile
依赖项转为 runtime
依赖项。
应该只使用 runtime
的实例是什么?所有子依赖项都从 compile
中获取,并在调用 gradle build
时拉入编译。
例如,当我对调用时打印的内容进行比较时
> gradle -q dependencies
为 compile 和 runtime 打印的列表是相同的。示例输出可能显示以下两者:
+--- 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
我看过这个 answer 这有助于解释 compile 和 runtime 之间的区别,但它仅表明运行时是您的代码实际执行依赖项的时间。你什么时候会有 runtime 依赖,但没有 compile 时间?
一个典型的案例是通过反射动态创建类。作为一个人为的例子,考虑这个应用程序:
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);
}
}
它将从 Apache Commons Lang 创建一个 StringUtils
的对象。 (这个例子很愚蠢;考虑 libA
将有效地为 libB
中的 类 执行此操作的情况。
没有编译时依赖性,因此没有理由用 jar 来增加编译时类路径的负担。然而在 运行-time,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