2016-07-18 149 views
0

我被困在以下问题: 我有一个主项目有多个子项目,每个子项目彼此依赖。例如:Gradle:在依赖项目上也运行子项目任务

+---Main 
    +- Shared 
     +- Variant1 
      +- Variant1A 

如果我运行的Variant1A:build命令上项目3构建的任务,所有其他项目得到建得。

我现在加入了deploy任务,每一个子项目,如果我部署我的项目之一,我想每一个它取决于为好,像这样的项目部署:

+---Main:deploy 
    +- Shared:deploy 
     +- Variant1:deploy 
      +- Variant1A:deploy 

但是运行Variant1A:deploy只运行了Variant1A项目任务:

+---Main:build 
    +- Shared:build 
     +- Variant1:build 
      +- Variant1A:deploy 

由于我有很多子项目和许多复杂的依赖关系,我不想与dependsOn指定每个项目。
因此,我正在寻找一种方法来运行任务,像build这样的所有本地项目,但是我不想将逻辑添加到build任务中,因为它不应该在正常的构建过程中部署。

回答

0
从根项目的build.gradle

你也许能够做这样的事

// look for deploy as a task argument sent via command line 
def isDeploy 
// could be many tasks supplied via command line 
for (def taskExecutionRequest : gradle.startParameter.taskRequests) { 
    // we only want to make sure deploy was specified as a task request 
    if (taskExecutionRequest.args.contains('deploy')) { 
     isDeploy = true 
    } 
} 
// if deploy task was specified then setup dependant tasks 
if (isDeploy) { 
    // add the task to all subprojects of the `rootProject` 
    subprojects.each { 
     // get all projects that depend on this subproject 
     def projectDependencies = project.configurations.runtime.getAllDependencies().withType(ProjectDependency) 
     // the subproject's dependant projects may also have project dependencies 
     def dependentProjects = projectDependencies*.dependencyProject 
     // now we have a list of all dependency projects of this subproject 
     // start setting up the dependsOn task logic 
     dependentProjects.each { 
      // find the deploy task then make our dependant 
      // project's deploy task a dependant of the deploy task 
      tasks.findByName('deploy').dependsOn project(it.name).tasks.findByName('deploy') 
     } 
    } 
} 
相关问题