build.gradle解析

1 providedCompile、testImplementation、implementation、api、runtime、testCompileclasspathnativesjava

這些都是和plugin相關的,不一樣的plugin定義了不一樣的dependency configurations。api

1.1 compileOnly和providedCompile的區別
app

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.ide

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.gradle

1.2 這些配置項和各個plugin是強相關的
ui

2 什麼是dependency configuration,有什麼用this

每一個項目都會依賴於其它的jar包,因此須要指定dependency,可是不是指定了dependency就能夠了的,根據狀況還須要更加細化的配置,好比該dependency的jar包是否是要打包進war文件,等等。因此,不光須要設置dependency,還須要設置該dependency的configuration。spa

3 transitive dependencycode

假如個人project中依賴於Hibernate,那麼Hibernate就是個人dependency,而Hibernate本身也是由dependencies的,這些dependencies就是個人transitive dependency。transitive dependency是由gradle自動負責的,假如出現問題,也能夠本身進行控制。server

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.

相關文章
相關標籤/搜索