Gradle脚本,如何在编译和部署后立即执行任务(文件复制)

时间:2015-12-04 13:27:51

标签: android gradle android-gradle build.gradle

以下是我的后端build.gradle for android app。 在构建和部署之后,我想将一些hibernate文件复制到build / explosion-app /下的文件夹中。而不是从终端运行gradlew cpyHibernate,如何在部署后立即从脚本执行cpyHibernate任务?

buildscript {
    repositories {
        jcenter()
}
dependencies {
    classpath 'com.google.appengine:gradle-appengine-plugin:1.9.18'
}
}

repositories {
    jcenter();
}

apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'appengine'

sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7

dependencies {
    appengineSdk 'com.google.appengine:appengine-java-sdk:1.9.18'
    compile 'com.google.appengine:appengine-endpoints:1.9.18'
    compile 'com.google.appengine:appengine-endpoints-deps:1.9.18'
    compile 'com.googlecode.objectify:objectify:5.1.5'
    compile 'javax.servlet:servlet-api:2.5'
    compile 'hibernate:hibernate:3.1rc2'
    compile 'mysql:mysql-connector-java:5.1.37'
}

appengine {
    downloadSdk = true
    appcfg {
        oauth2 = true
    }
endpoints {
    getClientLibsOnBuild = true
    getDiscoveryDocsOnBuild = true
    }
}

task cpyHibernate(type: Copy) {
    from 'src/main/java/hibernate.cfg.xml'
    into 'build/exploded-app/WEB-INF/classes/com/example/myapplication/backend/hbm/'
}

由于

2 个答案:

答案 0 :(得分:1)

你可以让一些任务完成另一个任务,在这种情况下,只有在调用另一个任务之后,此任务才会运行。您可以在官方user guide中阅读相关内容。这可以这样做:

task cpyHibernate(type: Copy) {
    doFirst {
        //check anothe task status and skip this one if it didn't actually work
        if (!tasks.getByName("deploy").getState().didWork) {
            throw new StopExecutionException();
        }
    }
    from 'src/main/java/hibernate.cfg.xml'
    into 'build/exploded-app/WEB-INF/classes/com/example/myapplication/backend/hbm/'
}

deploy.finalizedBy cpyHibernate

您只需知道部署任务名称即可。在上面的例子中,假设它的名字是deploy。请注意,您的cpyHibernate已被修改,以检查deploy任务是否在某种程度上有效并且不是最新的。如果您不需要,只需从中删除doFirst部分。

答案 1 :(得分:0)

您可以将builddeploy任务添加为cpyHibernate任务的依赖项。

宣布任务后:

cpyHibernate { ... }    
cpyHibernate.dependsOn build, deploy

或者在一个地方:

task cpyHibernate(type: Copy, dependsOn: [build, deploy]) {
    from 'src/main/java/hibernate.cfg.xml'
    into 'build/exploded-app/WEB-INF/classes/com/example/myapplication/backend/hbm/'
}