父項目添加:html
publishing { repositories { mavenLocal() maven { def repoKey = 'lib-release/libs-release-local' if (project.version.toString().endsWith("-SNAPSHOT")) repoKey = 'lib-snapshot/libs-snapshot-local' url "https://${xxx}/repository/${repoKey}" credentials { username "${xxx}" password "${xxx}" } } } }
須要publish的項目添加:java
publishing { publications { mavenJava(MavenPublication) { groupId "$project.group" version "$project.version" artifactId project.name from components.java } } }
II. Working with existing buildsspring
III. Writing Gradle build scripapi
IV. Extending the build緩存
V. Building JVM projectsapp
VI. The Software model - Next generation Gradle buildsmaven
VII. Appendixide
A. Gradle Samples
B. Potential Traps
C. The Feature Lifecycle
D. Gradle Command Line
Glossaryspring-boot
build.gradlepost
task compile << { println 'compiling source' } task compileTest(dependsOn: compile) << { println 'compiling unit tests' } task test(dependsOn: [compile, compileTest]) << { println 'running unit tests' } task dist(dependsOn: [compile, test]) << { println 'building the distribution' }
cmd cd build.gradle所在目錄
gradle compile compileTest
gradle -q task0
4.times { i -> task "task$i" << { println "I'm task number $i" } } task0.dependsOn task2, task3
gradle -q hello
task hello << { println 'Hello World' } hello.doFirst { println 'Hello doFirst' } hello.doLast { println 'Hello doLast' } hello << { println 'Hello Jude' }
Code
task test(dependsOn:'dist'){ ext.myName = "Jude" version = "1.0" } task dist << { setTestName('Jude Sheng') println test.myName + "$version" } String setTestName(String myName) { test.ext.myName = myName }
當前執行的task中是否有某個task
gradle.taskGraph.whenReady {taskGraph -> if (taskGraph.hasTask(release)) { version = '1.0' } else { version = '1.0-SNAPSHOT' } }
生成當前gradle版本wrapper文件
gradlew gradlew.bat gradle/wrapper/ gradle-wrapper.jar gradle-wrapper.properties
自定義wrapper所用gradle版本 task
task wrapper(type: Wrapper) { gradleVersion = '2.11' }
使用本地gradle zip文件代替gradle遠程下載,zip包須要放在 gradle\wrapper目錄下
task wrapper(type: Wrapper) { distributionPath = 'gradle-2.11-all.zip' }
直觀寫法
sourceSets { main { output.resourcesDir = "$buildDir/target" java { srcDirs = ['src'] } resources { srcDirs = ['ds'] } } }
依賴包設置
dependencies { compile fileTree('lib') }
task copyLib << { File scooby = file("$buildDir/target/scooby") int scoobySize = scooby.listFiles().length scoobySize.times { i -> String path = scooby.listFiles()[i].getPath() + "/lib" println 'start to copy libs into ' + path copy { from "lib" into path } copy { from "$buildDir/libs" into path } } }
task zip(type: Zip) { from "$buildDir/target/RatesDSCommonConfigs" archiveName "RatesDSCommonConfigs.zip" doFirst { println "Start to create the zip file for RatesDSCommonConfigs" } } task zipDS { File scooby = file("$buildDir/target/scooby") int scoobySize = scooby.listFiles().length scoobySize.times { i -> String scoobyDS = scooby.listFiles()[i].getPath() String scoobyDSName = scooby.listFiles()[i].getName() String tarFileName = scoobyDSName + ".zip" task "zip_$scoobyDSName" (type: Zip) { from scoobyDS archiveName tarFileName doFirst { println "Start to create the zip file for $scoobyDSName" } } zip.dependsOn "zip_$scoobyDSName" } }
apply plugin: 'java' archivesBaseName = 'cv-ratesds' sourceCompatibility = 1.6 sourceSets { main { output.resourcesDir = "$buildDir/target/RatesDSCommonConfigs" java { srcDirs = ['src'] } resources { srcDirs = ['resources'] } } } dependencies { compile fileTree('lib') } task copyDS(dependsOn: [build]) <<{ println 'start to copy DS processes' copy { from 'ds' into "$buildDir/target" } } task copyLib(dependsOn: [copyDS]) << { File scooby = file("$buildDir/target/scooby") int scoobySize = scooby.listFiles().length scoobySize.times { i -> String path = scooby.listFiles()[i].getPath() + "/lib" println 'start to copy libs into ' + path copy { from "lib" into path exclude "scooby" } copy { from "$buildDir/libs" into path } } } task tar(type: Tar) { File install = file("$buildDir/install") from "$buildDir/target/RatesDSCommonConfigs" destinationDir install archiveName "RatesDSCommonConfigs.tar" doFirst { println "Start to create the tar file for RatesDSCommonConfigs" } tar.dependsOn copyLib } task tarScoobyConfig(type: Tar) { File install = file("$buildDir/install") from "$buildDir/target/RatesDSCommonConfigs/ScoobyConfig" destinationDir install archiveName "ScoobyConfig.tar" doFirst { println "Start to create the tar file for ScoobyConfig" } tar.dependsOn tarScoobyConfig } task tarDS { File install = file("$buildDir/install") File scooby = file("ds/scooby") int scoobySize = scooby.listFiles().length scoobySize.times { i -> String scoobyDSName = scooby.listFiles()[i].getName() String tarFileName = scoobyDSName + ".tar" String scoobyPath = "$buildDir/target/scooby/" + scoobyDSName task "tar_$scoobyDSName" (type: Tar) { from scoobyPath destinationDir install archiveName tarFileName doFirst { println "Start to create the tar file for $scoobyDSName" } } tar.dependsOn "tar_$scoobyDSName" } } task install(dependsOn:[tar]) { println 'start to install RatesDS2.0' }