Gradle 教程說明 用戶指南 第7章 構(gòu)建Java工程----快速入門
來源:程序員人生 發(fā)布時間:2014-12-20 08:59:39 閱讀次數(shù):3271次
官網(wǎng)地址:http://www.gradle.org/docs/2.1/userguide/tutorial_java_projects.html
A basic Java project 1個基礎(chǔ)的java工程
使用java插件在build.gradle:
apply plugin: 'java'
Building the project 構(gòu)建工程
這里使用gradle/samples/java/quickstart 示例工程。shell進該目錄。
> gradle build
命令所 運行的任務(wù)有 (你可使用gradle taskName 來單獨運行以下的1種任務(wù)):
:compileJava
:processResources
:classes
:jar
:assemble
:compileTestJava
:processTestResources
:testClasses
:test
:check
:build
任務(wù)后提示 UP-TO-DATE 表示文件無更新,已為最新
任何履行成功后,會在當前目錄下生成build目錄,build目錄下生成的部分目錄有:
libs/ JAR file in the directory
src/main/java/ source code
src/test/java/ test source code
src/main/resources/ will be include in the JAR file
src/test/resources/ will be include in the classpath used to run the tests
其他1些 有用的任務(wù):
clean 清除build目錄
< gradle clean
Deletes the build directory, removing all built files.
assemble 編譯打jar包
< gradle assemble
:compileJava
:processResources
:classes
:jar
:assemble
會調(diào)用這5個task。編譯,處理資源,生成字節(jié)碼,生成jar包
External dependencies 外部依賴
通常1個項目會援用1些外部的JAR包。那末就需要告知Gradle在哪里可以找到它們。
在Gradle中,比如JAR文件,位于1個存儲倉庫(repository)。
存儲倉庫可以 獲得依賴或發(fā)布它使之獨立(比如jar包,javadoc文檔)。
本例使用Maven倉庫。
build.gradle:
repositories {
mavenCentral()
}
本例需要添加1個.java源碼編譯時的依賴:commons-collections
和1個test源碼編譯時的依賴:junit
build.gradle:
dependencies {
compile group: 'commons-collections', name: 'commons-collections', version: '3.2'
testCompile group: 'junit', name: 'junit', version: '4.+'
}
Customizing the project 定制工程
Java插件添加了許多特性到您的項目。這些屬性通常有默許值。如果他們不合適,很容易改變這些值。
例子中,將為我們的Java項目指定版本號,和我們的源代碼的編譯級別。
在JAR-manifest中,我們還添加了1些屬性。
build.gradle:
//java compile version
sourceCompatibility = 1.5
//project version
version = '1.0'
jar {
manifest {
attributes 'Implementation-Title': 'Gradle Quickstart',
'Implementation-Version': version
}
}
添加1個test 系統(tǒng)屬性
build.gradle:
test {
systemProperties 'property': 'value'
}
Publishing the JAR file 發(fā)布JAR包
1般打JAR包需要指定1個位置。就是要告知Gradle這個位置。例子中打包到本地目錄。也能夠發(fā)布到遠程位置或多個位置
build.gradle:
uploadArchives {
repositories {
flatDir {
dirs 'repos' //publish to flat dir : repos
}
}
}
Creating an Eclipse project 創(chuàng)建1個Eclipse工程
要創(chuàng)建Eclipse獨有的描寫符文件,如.project文件,你需要添加另外1個插件到您的構(gòu)建文件:
build.gradle:
apply plugin: 'eclipse'
> gradle eclipse
ls -al 發(fā)現(xiàn)有了 .project
同理,發(fā)現(xiàn)gradle java 也能夠履行:
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:javadoc
結(jié)果是編譯并生成了javadoc文檔
小結(jié)
<span style="font-size:14px;"><span style="font-family:SimSun;font-size:14px;">apply plugin: 'java'
apply plugin: 'eclipse'
sourceCompatibility = 1.5
version = '1.0'
jar {
manifest {
attributes 'Implementation-Title': 'Gradle Quickstart',
'Implementation-Version': version
}
}
repositories {
mavenCentral()
}
dependencies {
compile group: 'commons-collections', name: 'commons-collections', version: '3.2'
testCompile group: 'junit', name: 'junit', version: '4.+'
}
test {
systemProperties 'property': 'value'
}
uploadArchives {
repositories {
flatDir {
dirs 'repos'
}
}
}</span></span>
Multi-project Java build 多Java工程構(gòu)建
本節(jié)采取gradle/samples/java/multiproject 示例, shell 進該目錄。
Build layout 構(gòu)建布局:
multiproject/
api/
services/webservice/
shared/
services/shared/
Defining a multi-project build 定義1個多工程的構(gòu)建
要定義1個多項目構(gòu)建,你需要創(chuàng)建1個設(shè)置文件。設(shè)置文件在源代碼樹的根目錄下。名為settings.gradle
settings.gradle:
include "shared", "api", "services:webservice", "services:shared"
說明 包括了 4個目錄
Common configuration 經(jīng)常使用配置
定義1些對所有項目都通用的配置。采取1種叫 配置反射(configuration injection) 的技術(shù)。
build.gradle:
subprojects {
apply plugin: 'java'
apply plugin: 'eclipse-wtp'
repositories {
mavenCentral()
}
dependencies {
testCompile 'junit:junit:4.11'
}
version = '1.0'
jar {
manifest.attributes provider: 'gradle'
}
}
利用java插件到每一個子項目。意味著可以在子項目中,使用上1節(jié)講的任務(wù)和配置屬性。如 gradle build。
注意所構(gòu)建出的文件都在子項目中,而不是在根目錄下。子項目里可以有自己的build.gradle 構(gòu)建文件
Dependencies between projects 項目間的依賴關(guān)系
1個項目依賴了另外一個項目的jar包。例子里,在api/build,聲明了 依賴項目 shared
api/build.gradle:
dependencies {
compile project(':shared’)
}
Creating a distribution 創(chuàng)建發(fā)布任務(wù)
api/build.gradle:
task dist(type: Zip) {
dependsOn spiJar
from 'src/dist'
into('libs') {
from spiJar.archivePath
from configurations.runtime
}
}
artifacts {
archives dist
}
> gradle dist
以zip類型發(fā)布api工程的Jar包,依賴了任務(wù)spiJar,從目錄src/dist 添加到(打包進)build/libs/
該命令還會生成1個發(fā)布目錄:build/distributions
生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對您的學(xué)習(xí)有所幫助,可以手機掃描二維碼進行捐贈