Gradle插件經過引入特定領域的約定和任務來構建你的項目。Java插件是Gradle自身裝載的一個插件。Java插件提供的基本功能遠比源代碼編譯和打包多。它爲你的項目創建了一個標準的項目佈局,並確保有意義,有順序地執行任務。如今,爲你的項目建立一個構建腳本並使用Java插件。html
使用Java插件java
每一個Gradle項目都是以建立名字爲build.gradle的文件開始的。建立這個文件,而後像下面這樣告訴它要使用Java插件:shell
apply plugin: 'java'
一行代碼足夠構建你的Java代碼,可是Gradle怎麼知道去哪裏找源文件呢?Java插件引入的約定之一就是源代碼的位置。在默認狀況下,插件會到 src/main/java 目錄下查找。apache
自動化生成項目結構api
可是,咱們要手工來建立build.gradle文件與源代碼目錄嗎?顯示不是,gradle提供了初始化項目目錄的命令init:服務器
gradle init --type [java-library | scala-library | groovy-library | basic | pom]app
type參數當前只支持如下類型:框架
basic:缺省值,僅僅爲咱們建立好構建腳本maven
pom:將一個maven構建的項目轉換成一個gradle構建的項目。若是pom.xml存在,這個類型值會被自動指定。ide
java-library:初始化建立一個gradle構建的java項目
scala-library:初始化建立一個gradle構建的scala項目
groovy-library:初始化建立一個gradle構建的groovy項目
這裏以建立java項目爲例:
$ gradle init --type java-library
上圖顯示,gradle構建的完整的java項目結構就完成了。注意,大家還帶了包裝器的,後面再講。
構建項目
你能夠開始構建項目了。java插件提供的一個任務叫做build。這個build任務會以正確的順序編譯你的源代碼,運行測試,組裝JAR文件。運行gradle build命令,你應該能夠獲得相似於下面的輸出:
$ gradle build
每一行輸出都表明着java插件提供的一個可執行任務。你也許注意到某些任務被標記爲 UP-TO-DATE 消息。這意味着這個任務被跳過了。Gradle的增量式構建支持自動鑑別不須要被運行的任務。特別是在大型的企業級項目中,這個特性是節省時間的好幫手。在上面的例子中,你能夠看到有測試任務執行,測試源代碼默認的位置爲:src/test/java。
運行構建後,在項目的根目錄下,你會看到有一個build目錄,裏面包含了構建運行的全部輸出,包括class文件,測試報告和JAR文件,還有一些像清單(manifest)同樣的對構建有用的臨時文件。注:1. 構建輸出目錄的名字是可配置的屬性。2. JAR文件的名字是繼承自項目的名字。
運行項目
運行一個Java應用程序是很是簡單的。由於項目是經過命令:
$ gradle init --type java-library
建立的,會產生一個java源文件類的示例和一個java測試類的示例,但並無帶有main函數入口的java類。這裏咱們建立一個測試用:
public class Main { public static final void main(String[] args){ System.out.println("test File"); } }
再從新構建一下:
$ gradle build
下面,咱們來運行項目,執行下面的命令:
$ java -cp build/classes/main/ Main
就是這樣——使用Gradle,你不費吹灰之力就實現和構建了一個Java應用。只要使用標準約定,你所須要作的只是一行腳本
定製你的項目
Java插件是一個很靈活的框架。它會給項目的許多方面設置有意義的默認值,好比項目結構。若是你看待開發世界的方式不一樣,Gradle給予你定製這些約定的選項。如何知道什麼可配置?有一個好地方,就是Gradle構語言指導:http://www.gradle.org/docs/current/dsl/ 還記得前面講的命令行選項 properties 嗎? 運行:
$ gradle properties
它會給你一個可配置標準和插件屬性的列表,同時還會顯示它們的默認值。你能夠經過擴展初始構建腳原本定製你的項目。
修改項目和插件屬性
下面的例子中,你將給項目指定一個版本號,而且指定Java源代碼的兼容性。另外,以前你經過java命令運行應用,經過classpath命令行選項 -cp build/classes/main 告訴Java運行時去哪裏找class。爲了可以從JAR文件啓動應用,清單文件MANIFEST.MF須要包含信息頭Main-Class。示例(用前面gradle init --type java-library命令生成的build.gradle,在上面添 加配置項):
/* * This build file was auto generated by running the Gradle 'init' task * by 'fuhd' at '15-10-22 下午8:14' with Gradle 2.7 * * This generated file contains a sample Java project to get you started. * For more details take a look at the Java Quickstart chapter in the Gradle * user guide available at https://docs.gradle.org/2.7/userguide/tutorial_java_projects.html */ // Apply the java plugin to add support for Java apply plugin: 'java' version = 0.1 sourceCompatibility = 1.8 jar{ manifest{ attributes 'Main-Class':'Main' } } // In this section you declare where to find the dependencies of your project repositories { // Use 'jcenter' for resolving your dependencies. // You can declare any Maven/Ivy/file repository here. jcenter() } // In this section you declare the dependencies for your production and test code dependencies { // The production code uses the SLF4J logging API at compile time compile 'org.slf4j:slf4j-api:1.7.12' // Declare the dependency for your favourite test framework you want to use in your tests. // TestNG is also supported by the Gradle Test task. Just change the // testCompile dependency to testCompile 'org.testng:testng:6.8.1' and add // 'test.useTestNG()' to your build script. testCompile 'junit:junit:4.12' }
而後運行:
$ gradle build
你會看到版本號添加到了JAR文件的名字中。如今名字是abc-0.1.jar。如今生成的JAR文件包含了主類頭屬性,你能夠經過下面這條命令運行應用:
$ java -jar build/libs/abc-0.1.jar
改造遺留項目
和一個遺留系統集成,遷移已有項目的技術棧,或者堅持內部標準或者限制,實在太常見了。構建工具必須足夠靈活,能夠經過改變默認配置來適應來自外部的限制。
讓咱們假設你是在一個徹底不同的目錄結構下開始這個項目的。你須要把源代碼放置在src目錄下,而不是 src/main/java 。一樣的道理,也適用於改變默認的測試代碼目錄。另外,你想要讓Gradle將輸出結果放置在out目錄下, 而不是build。下面代碼展現瞭如何讓你的構建適應一個定製的項目結構:
/* * This build file was auto generated by running the Gradle 'init' task * by 'fuhd' at '15-10-22 下午8:14' with Gradle 2.7 * * This generated file contains a sample Java project to get you started. * For more details take a look at the Java Quickstart chapter in the Gradle * user guide available at https://docs.gradle.org/2.7/userguide/tutorial_java_projects.html */ // Apply the java plugin to add support for Java apply plugin: 'java' version = 0.1 sourceCompatibility = 1.8 jar{ manifest{ attributes 'Main-Class':'Main' } } sourceSets{ main{ java{ srcDirs = ['src'] //用不一樣目錄的列表代替約定的源代碼 } } test{ java{ srcDirs = ['test'] //用不一樣目錄的列表代替約定的測試代碼目錄 } } } buildDir = 'out' //改變項目輸出屬性(路徑)到out目錄 // In this section you declare where to find the dependencies of your project repositories { // Use 'jcenter' for resolving your dependencies. // You can declare any Maven/Ivy/file repository here. jcenter() } // In this section you declare the dependencies for your production and test code dependencies { // The production code uses the SLF4J logging API at compile time compile 'org.slf4j:slf4j-api:1.7.12' // Declare the dependency for your favourite test framework you want to use in your tests. // TestNG is also supported by the Gradle Test task. Just change the // testCompile dependency to testCompile 'org.testng:testng:6.8.1' and add // 'test.useTestNG()' to your build script. testCompile 'junit:junit:4.12' }
配置和使用外部依賴
Gradle如何引用外部庫?咱們來看看兩個DSL配置元素:repositories和dependencies。
定義倉庫
在Java世界,依賴都是以JAR文件的形式發佈和使用的。許多類庫均可以在倉庫中找到,倉庫能夠是一個文件系統或者一箇中心服務器。Gradle要求你定義至少一個倉庫來使用依賴。爲此,你須要使用公共的可訪問的倉庫Maven Central:
repositories { mavenCentral() //配置對Maven Central2倉庫訪問的快捷方式 }
定義好倉庫以後,你就能夠聲明類庫了。讓咱們看看依賴是如何定義的!
定義依賴
一個依賴是經過group標識符,名字和一個指定版本來肯定的。如例:
dependencies { compile group:'org.apache.commons',name:'commons-lang3',version:'3.1' }
在Gradle中,依賴是由configuration分組的。Java插件引入的一種configuration是compile。你能夠經過configuration的名字看出它是給編譯源代碼使用的。
解析依賴
Gradle會自動檢測到一個新的依賴添加到項目中。若是依賴沒有被成功解析,那麼就會在下一個須要使用該依賴的任務啓動時去下載它。