据我的理解,Gradle将把所有compile
依赖项作为runtime
依赖项进行处理。
什么是您应该只使用runtime
的实例?当调用compile
时,所有子依赖项都从gradle build
中抓取并拖到编译中。
例如,当我在调用时对打印的内容做了区分时
> gradle -q dependencies
为、编译、和运行时打印的列表是相同的。示例输出可以为这两种情况显示如下:
+--- 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,它帮助解释了编译和运行时之间的一些区别,但它只表明运行时实际上是代码执行依赖项的时候。什么时候有运行时依赖项,而不是编译时间?
发布于 2017-08-17 18:25:35
典型的情况是通过反射动态创建类。作为一个精心设计的例子,考虑一下这个应用程序:
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创建一个StringUtils
对象。(这个示例很愚蠢;考虑一下libA
将有效地为libB
中的类执行此操作的情况)。
不存在编译时依赖关系,因此没有理由用jar来负担编译时类路径。然而,在运行时,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
https://stackoverflow.com/questions/45740544
复制相似问题