如何逐个在Gradle中执行任务

时间:2013-08-31 06:22:18

标签: android gradle

我有这个gradle构建文件。

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.5.+'
        classpath 'com.eriwen:gradle-css-plugin:1.2.1'
        classpath 'com.eriwen:gradle-js-plugin:1.5.1'
    }
}

apply plugin: 'android'
apply plugin: 'js'
apply plugin: 'css'

dependencies {
    compile files('libs/android-support-v4.jar')
    compile files('libs/cordova-2.7.0.jar')
    compile files('libs/libGoogleAnalyticsV2.jar')
}

task MinifyJSForRelease {

tasks.add(name:"copyTask2", type:Copy){
    from 'assets/'
    into 'build/minifiedassets/assets'
}
it.dependsOn copyTask2
def tester = fileTree(dir:"build/minifiedassets/assets", include:"**/*.js");
println tester
tester.eachWithIndex {File file, idx ->
    tasks.add(name: "makeMinifyJS${idx}", type: com.eriwen.gradle.js.tasks.MinifyJsTask) {
        source = file
        dest = "${file.path}"
        closure {

            compilerOptions.languageIn = "ECMASCRIPT5"
        }

    }
}

it.dependsOn tasks.matching { Task task -> task.name.startsWith("makeMinifyJS")}
}

build.dependsOn MinifyJSForRelease

android {
compileSdkVersion 17
buildToolsVersion "17"

signingConfigs {
    release {
        storeFile file('keystores/mobilecoe_android.keystore')
        storePassword "mobilecoe"
        keyAlias "MobileCoE"
        keyPassword "mobilecoe"
    }
}

defaultConfig {
    minSdkVersion 10
    targetSdkVersion 16
}
buildTypes {
    debug {

        sourceSets {
            main {
                manifest.srcFile 'AndroidManifest.xml'
                java.srcDirs = ['src']
                resources.srcDirs = ['src']
                aidl.srcDirs = ['src']
                renderscript.srcDirs = ['src']
                res.srcDirs = ['res']
                assets.srcDirs = ['assets']
            }

            instrumentTest.setRoot('tests')
        }
    }

    release {

        signingConfig signingConfigs.release
        zipAlign true
        sourceSets {
            main {
                manifest.srcFile 'AndroidManifest.xml'
                java.srcDirs = ['src']
                resources.srcDirs = ['src']
                aidl.srcDirs = ['src']
                renderscript.srcDirs = ['src']
                res.srcDirs = ['res']
                assets.srcDirs = ['build/minifiedassets/assets']
            }

            instrumentTest.setRoot('tests')
        }
    }
}
}

我想运行任务MinifyJSForRelease,就像第一件事就是将assets文件夹复制到我想要的地方,然后当它完成时我想缩小JS文件。 现在,当我运行任务时,它会复制文件夹,但没有到达结尾,下一个函数说没有什么可以缩小,但是如果我第二次运行任务(当文件夹已被复制时)它就会把它变成一切并完成所有事情。

我做错了什么? 我需要:

  1. 复制文件夹
  2. 获取文件夹并缩小其js文件
  3. 有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

我是Gradle的全部病毒,所以我只能为你提供有根据的猜测:

  1. 统一声明依赖项的方式

    taskB.dependsOn tasksA

  2. 强制执行任务的顺序(添加依赖项)

    taskB.mustRunAfter(任务A)

  3. 添加日志,查看任务是否已执行VS任务未达到预期效果。

  4. 向任务添加依赖关系/订购任务

    http://www.gradle.org/docs/current/userguide/more_about_tasks.html

    请在获得解决方案后向我们提供解决方案!