美文网首页
Gradle笔记

Gradle笔记

作者: 百花鱼藤 | 来源:发表于2018-06-25 15:42 被阅读0次

    任务创建

    1、 task hello{

      }

      task hello<< {

      }

      task hello(dependsOn:hello1)<<{

      }

      task(hello)<<{

      }

      task('hello')<<{

      }

      动态创建

        4.times {

        counter->

            task "task$counter" <<{

            }

    }

    tasks.create(name:'hello')<<{

            println "hello"

    }

    2、扩展task属性/自定义task属性

      task myTask<<{

      ext.myproperties ="properties"

      }

      efaultTasks 'clean','run'

      task distribution <<{

            println "We build the zip with version = $version"

    }

      task release (dependsOn :'distribution')<<{

            println 'We release now'

    }

      gradle.taskGraph.whenReady {

            taskGraph->

            if(taskGraph.hasTask(release)){

                    version = '1.0'

            }else {

                    version = '1.0-SNAPSHOT'

            }

    }

    3、排除任务 -X

      gradle task1 -x task2

    4、列举依赖和属性

      gradle -q dependencies api:dependencies 

      gradle -q app:properties

    5、gradle属性和系统属性

    gradle -q printProps -P commandLineProjectProp="this is a commandline prop" -D org.gradle.project.systemProjectProp="this a gradle property"

    task printProps <<{

    println commandLineProjectProp

    println systemProjectProp

    println gradleFileProperties

    }

    6、使用tasks容器来定位

    task hello

    println tasks.hello.name

    println tasks['hello'].name

    rintln tasks.getByPath('hello').path

    println tasks.getByPath(':hello').path

    7、配置任务

    task myCopy(type:Copy)

    println myCopy.name

    task copy(type:Copy){

            from 'resources'

            into 'target'

            include('**/*.txt','**/*.xml','**/*.properties')

    task copyTaskWithPaths(type:Copy){

    from 'src/main/webapp'

    into 'build/explodedWar'

    include '**/*.html'

    include '**/*.jsp'

    exclude{

    details ->details.file.name.endsWith('/html')&&

    details.file.text.contains('staging')

    }

    8、使用闭包的方式配置依赖

    task taskX << {

            println 'taskX'

    }

    taskX.dependsOn{

            tasks.findAll{

                    task -> task.name.startsWith('lib')

            }

    }

    task lib1<<{

            println 'lib1'

    }

    task lib2 <<{

            println 'lib2'

    }

    9、任务覆盖

    task copy << {

    println "this is a first"

    }

    task copy(overwrite:true)<<{

    println "this is a second"

    }

    10、跳过任务

    task hello <<{

    println 'hello world'

    }

    hello.onlyIf{!project.hasProperty('skipHello')}

    可以通过抛出异常,来跳过某个任务的执行

    task compile <<{

    println "We are doing the compile"

    }

    compile.doFirst{

    if(true){throw new StopExecutionException()}

    }

    task myTask(dependsOn:'compile')<<{

    println 'I am not affected'

    }

    任务的enabled属性

    task disableMe <<{

    println 'ni dou bu hui zhi xing ,ni hai shuo zhe me duo hua'

    }

    disableMe.enabled = false

    11、任务规则

    tasks.addRule("Pattern:ping"){

        String taskName ->

        if(taskName.startsWith("ping")){

            task(taskName)<<{

                println taskName

                println "Pinging: " + (taskName-'ping')

                }

            }

        }

    12、文件集合-FileCollection

    task list << {

    File srcDir

    // Create a file collection using a closure

    FileCollection collection = files { srcDir.listFiles() }

    srcDir = file('src')

    println "Contents of $srcDir.name"

    collection.collect { relativePath(it) }.sort().each { println it }

    srcDir = file('src2')

    println "Contents of $srcDir.name"

    collection.collect { relativePath(it) }.sort().each { println it }

    }

    FileTree tree = fileTree(dir: 'src/main')

    tree.include '**/*.java'

    tree.exclude '**/Abstract*'

    tree = fileTree('src').include('**/*.java')

    tree = fileTree('src') {

    include '**/*.java'

    }

    tree = fileTree(dir: 'src', include: '**/*.java')

    tree = fileTree(dir: 'src', includes: ['**/*.java', '**/*.xml'])

    tree = fileTree(dir: 'src', include: '**/*.java', exclude: '**/*test*/**')

    task findTree(dependsOn:'create') <<{

    FileTree filtered = tree.matching{

    include 'org/gradle/api/**'

    }

    FileTree sum = tree + fileTree(dir:'src/test')

    tree.visit{

    element ->

    println "$element.relativePath =>$element.file"

    }

    }

    task create {

    File testFile = file('src/test/java')

    testFile.mkdirs()

    }

    FileTree zip = zipTree('someFile.zip')

    FileTree tar = tarTree('someFile.tar')

    FileTree someTar = tarTree(resources.gzip('someTar.ext'))

    tasks.withType(JavaCompile){

    }

    project.afterEvaluate {

    }

    tasks.whenTaskAdded {

        thetask->

          if (thetask.name.equals(lib1)){

              thetask.dependsOn lib2

          }

    }

    添加规则:project.getTasks().addRule("Pattern:incrementversion"){

        String taskName->

        if (taskName.startsWith("increment")&& taskName.endsWith("version")){

            task(taskName)<<{

                String classifier= taskName-'increment'-'version'

                switch (classifier){

                    default:

                        break

                }

            }

        }

    }

    gralde 部分编译:

    gradlew :app:build -a 或者 --no-rebuild

    相关文章

      网友评论

          本文标题:Gradle笔记

          本文链接:https://www.haomeiwen.com/subject/lsyyyftx.html