Gradle

一、应用示例

 plugins {
    id 'java'
}


sourceCompatibility = 1.8
targetCompatibility = 1.8

//对所有项目生效
allprojects {
    group 'com.examples'
    version '1.0.0-SNAPSHOT'
    repositories {
        maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
        maven{ url 'http://maven.aliyun.com/nexus/content/repositories/jcenter'}
        mavenCentral()
        jcenter()
    }
}
dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
}


//buildscript中的声明是gradle脚本自身需要使用的资源。可以声明的资源包括依赖项、第三方插件、maven仓库地址等。
buildscript {
    mavenCentral()
    dependencies {
        classpath 'org.apache.commons:commons-csv:1.0'
    }
}



sourceSets{
    xzb{}
}

//创建src下的目录
task createDirs {
    sourceSets.all{
        println "$name"
    }
    println "${project.projectDir}"
    println "${sourceSets}"
    println "${sourceSets.main.java.srcDirs}"
    println "${sourceSets.test.java.srcDirs}"
      doLast {
        sourceSets*.java.srcDirs*.each {
            it.mkdirs()
        }
        sourceSets*.resources.srcDirs*.each {
            it.mkdirs()
        }
    }
}
// 自定义打包设置
task packageJar(type: Jar){
    //设置jar包名称
    archivesBaseName="MyJava"
    //设置版本号
    archiveVersion="v1.0.0"
    //设置打包jar的存放位置
    //destinationDirectory = file("${project.projectDir}/jar")
    //打包来源
    from sourceSets.main.output
}


//生成JavaDoc
task javaDoc(type: Javadoc){
    destinationDir = file("${project.projectDir}/doc")
    source sourceSets.main.allJava
    title  sourceSets.main.name
}
tasks.withType(Javadoc) {
    options.encoding = 'UTF-8'
}



//任务间的依赖
task task1{
    doFirst {
        println "I AM NO1"
    }
}

task task2(dependsOn:task1){
    doFirst {
        println "I AM NO2"
    }
}

二、构建多项目

Gradle_第1张图片
image.png

总:build.gradle


allprojects {
    group 'com.examples'
    version '1.0.0-SNAPSHOT'
    //定义通用任务:所有子模块都执行
    task commonTask {
        doLast{
            task -> println "I'm common $task.project.name"
        }
    }
        repositories {
        maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
        maven{ url 'http://maven.aliyun.com/nexus/content/repositories/jcenter'}
        mavenCentral()
        jcenter()
    }
}

//将子工程中重复配置抽取
subprojects {
    apply plugin: 'java'
    //所有子模块都执行
    task subAllTask {
        doLast{
            println "$project.name"
        }
    }
}


//指定子模块执行
project(':service'){
    task serviceTask {
        doLast{
            task -> println "serviceTask"
        }
    }
}

service的build.gradle

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    compile project(":entry")
}

entry的build.gradle

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

三、项目发布

将service发布:

apply plugin: 'maven-publish'
dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    compile project(":entry")
}
publishing {
    publications {
        mavenpub(MavenPublication) {
            from(components.java)
        }
    }
    repositories {
        maven {
            url =  "$buildDir/repo"
        }
    }
}

你可能感兴趣的:(Gradle)