首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Java/Gradle中,何时使用运行时而不是编译时依赖关系?

在Java/Gradle中,何时使用运行时而不是编译时依赖关系?
EN

Stack Overflow用户
提问于 2017-08-17 16:29:59
回答 1查看 2.7K关注 0票数 7

据我的理解,Gradle将把所有compile依赖项作为runtime依赖项进行处理。

什么是您应该只使用runtime的实例?当调用compile时,所有子依赖项都从gradle build中抓取并拖到编译中。

例如,当我在调用时对打印的内容做了区分时

代码语言:javascript
运行
复制
> gradle -q dependencies

、编译、运行时打印的列表是相同的。示例输出可以为这两种情况显示如下:

代码语言:javascript
运行
复制
+--- 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,它帮助解释了编译运行时之间的一些区别,但它只表明运行时实际上是代码执行依赖项的时候。什么时候有运行时依赖项,而不是编译时间?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-17 18:25:35

典型的情况是通过反射动态创建类。作为一个精心设计的例子,考虑一下这个应用程序:

代码语言:javascript
运行
复制
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插件,它很好地将依赖项捆绑到可运行的可交付版本中。

代码语言:javascript
运行
复制
apply plugin: 'java'
apply plugin: 'application'

repositories {
    jcenter()
}

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

mainClassName = 'net.codetojoy.App'

示例输出:

代码语言:javascript
运行
复制
$ gradle run -q
object is : org.apache.commons.lang3.StringUtils@4aa298b7
票数 11
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45740544

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档