build.gradle解析

1 providedCompile、testImplementation、implementation、api、runtime、testCompileclasspathnatives

这些都是和plugin相关的,不同的plugin定义了不同的dependency configurations。

1.1 compileOnly和providedCompile的区别

compileOnly is only there for dependencies that you really don't need at runtime, e.g because you shade them inside your JAR or because they only contain some annotations that are only needed at compile time. That means if I depend on you, I should not need to provide those.

providedCompile on the other hand is a dependency that you do need at compile and runtime, but which you also declare is "to be provided by the app server", so it is removed from the WAR.

1.2 这些配置项和各个plugin是强相关的

2 什么是dependency configuration,有什么用

每个项目都会依赖于其它的jar包,所以需要指定dependency,但是不是指定了dependency就可以了的,根据情况还需要更加细化的配置,比如该dependency的jar包是不是要打包进war文件,等等。所以,不光需要设置dependency,还需要设置该dependency的configuration。

3 transitive dependency

假如我的project中依赖于Hibernate,那么Hibernate就是我的dependency,而Hibernate自己也是由dependencies的,这些dependencies就是我的transitive dependency。transitive dependency是由gradle自动负责的,假如出现问题,也可以自己进行控制。

4 gradle plugin

gradle本身只是一个平台,可以说一无是处,它什么也不会做,它将所具有的功能都是由gradle plugin提供的,比如编译java代码、c代码等的能力。

4.1 plugin为gradle定义了task

4.2 plugin可以进行配置

比如java plugin的配置source set、生成的jar包的存储目录等等。

4.3 dependency的configuration也是对gradle plugin的配置。

5 compile和runtime的区别

如下:

In the most common case, the artifacts needed at compile time are a subset of those needed at runtime. For example, let's say that a program called app uses library foo, and library foo internally uses library bar. Then only foo is needed to compile app, but both foo and bar are needed to run it. This is why by default, everything that you put on Gradle's compile configuration is also visible on its runtime configuration, but the opposite isn't true.

就是说,transitive dependencies是runtime的,而直接的dependencies是compile的。

一个jar包如果指定为compile的话,那么compile时需要,运行时也需要,那么gradle会把它编译进最终的生成文件中。这个还需要在实际项目中验证。

对于runtime的话,例子如下:

A common example is a JDBC driver, in which the application code is compiled against interfaces in the Java standard libraries, while the implementation is provided by a vendor. The java plug-in provides the runtime and testRuntime configurations for this purpose. These are used to create deployment archives and execute test code, but do not add to the compile-time classpath at all. The runtime configuration does contain the output of the compileJava task, of course, since a project’s .class files are necessarily required to run the project.

 

转载于:https://www.cnblogs.com/hustdc/p/8067539.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值