Bra*_*don 6 java runtime compile-time gradle
根据我的理解,Gradle将继承所有compile
依赖项作为runtime
依赖项.
你应该只使用runtime
什么实例?调用compile
时,所有子依赖项都会从编译中获取并进入编译gradle build
.
例如,当我打电话给我打印的内容时,我会做差异
> gradle -q dependencies
Run Code Online (Sandbox Code Playgroud)
打印出来用于编译和运行时的列表是相同的.两个示例输出可能会显示以下内容:
+--- 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
Run Code Online (Sandbox Code Playgroud)
我看过这个答案,它有助于解释编译和运行时之间的区别,但它只表明运行时是代码实际执行依赖的时候.你什么时候有运行时依赖,但不是编译时?
典型案例涉及通过反射动态创建类.作为一个人为的例子,请考虑以下应用:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
它将创建一个StringUtils
来自Apache Commons Lang 的对象.(这个例子很愚蠢;考虑一个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'
Run Code Online (Sandbox Code Playgroud)
示例输出:
$ gradle run -q
object is : org.apache.commons.lang3.StringUtils@4aa298b7
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1327 次 |
最近记录: |