javascript - Define a Gradle task with inputs from project dependencies? - Stack Overflow

Background: I have a multi-project Gradle build, and I've defined a Gradle task which runs JavaScr

Background: I have a multi-project Gradle build, and I've defined a Gradle task which runs JavaScript unit tests in an Exec task. The inputs to this task are the JavaScript files in the project, so it's only re-run if one of the source files are modified. The task is added to all JavaScript projects from a master project.

Question: I want to extend this so that the tests are re-run if JavaScript files in the project, or in any of its project dependencies are changed. How is this best done?

The code below works if placed in each subproject build file (after the dependency declaration), but we have 20+ JavaScript subprojects and I'd like to stay DRY.

project.ext.jsSourceFiles = fileTree("src/").include("**/*.js*")

task testJavaScript(type: Exec, dependsOn: configurations.js) {
    inputs.files resolveJavascriptDependenciesFor(project)
    outputs.file "report.xml"

    // Run tests in JSTestDriver using mand line call...    
}

def resolveJavascriptDependenciesFor(project) {
    def files = project.jsSourceFiles
    project.configurations.js.allDependencies.each {
        files = files + resolveJavascriptDependenciesFor(it.dependencyProject)
    }
    return files
}

Is there a better solution? Maybe where I don't have to resolve all file dependencies myself?

Background: I have a multi-project Gradle build, and I've defined a Gradle task which runs JavaScript unit tests in an Exec task. The inputs to this task are the JavaScript files in the project, so it's only re-run if one of the source files are modified. The task is added to all JavaScript projects from a master project.

Question: I want to extend this so that the tests are re-run if JavaScript files in the project, or in any of its project dependencies are changed. How is this best done?

The code below works if placed in each subproject build file (after the dependency declaration), but we have 20+ JavaScript subprojects and I'd like to stay DRY.

project.ext.jsSourceFiles = fileTree("src/").include("**/*.js*")

task testJavaScript(type: Exec, dependsOn: configurations.js) {
    inputs.files resolveJavascriptDependenciesFor(project)
    outputs.file "report.xml"

    // Run tests in JSTestDriver using mand line call...    
}

def resolveJavascriptDependenciesFor(project) {
    def files = project.jsSourceFiles
    project.configurations.js.allDependencies.each {
        files = files + resolveJavascriptDependenciesFor(it.dependencyProject)
    }
    return files
}

Is there a better solution? Maybe where I don't have to resolve all file dependencies myself?

Share Improve this question edited Oct 2, 2012 at 7:45 David Pärsson asked Oct 1, 2012 at 15:45 David PärssonDavid Pärsson 6,2693 gold badges39 silver badges52 bronze badges 1
  • Test folders have been excluded from the example source for the sake of simplicity. – David Pärsson Commented Oct 1, 2012 at 15:47
Add a ment  | 

3 Answers 3

Reset to default 2

As written in the answer before, adding the jsTest task within a subprojects closure would make it very easy to add jstesting support for every subproject. I think you can ease your inputs setup by declaring source files as dependencies:

dependencies {
     js filetree("src/main").include("**/*.js")
}

and

subprojects { subproj ->
    task testJavaScript(type: Exec, dependsOn: configurations.js) {
        inputs.files subproj.configurations.js
        outputs.file "report.xml"

        mandLine  ...
    }    
}

Would it be possible to do something like this?

allprojects {project ->
   task testJavaScript(type: Exec, dependsOn: configurations.js) {
      inputs.files resolveJavascriptDependenciesFor(project)
     // Run tests in JSTestDriver using mand line call...    
   }
}

def resolveJavascriptDependenciesFor(project) {
   def files = project.jsSourceFiles
   project.configurations.js.allDependencies.each {
      files = files + resolveJavascriptDependenciesFor(it.dependencyProject)
   }
   return files
}

Tha way the task is on all projects, and wil be called recursively. Not pletely sure this works, but I think its the way to go

I've found a solution that works but isn't great, using the same dependency specification as in the question. It's to load the gradle files in a slightly different order in the master project.

Master build.gradle:

subprojects {
    configurations {
        js
    }

    // Apply the subproject's build.gradle, but with another name (that isn't automatically loaded)
    if (project.file('depends.gradle').exists()) {
        apply from: project.file('depends.gradle')
    }

    apply from: project.parent.file('javaScriptProjectTasks.gradle')
}

Compare to the previous, non working master build.gradle

subprojects {
    configurations {
        js
    }

    apply from: project.parent.file('javaScriptProjectTasks.gradle')
    // The subproject's build.gradle is automatically loaded
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745300697a4621418.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信