Define a Gradle task with project dependency inputs?

Background: I have a multi-project Gradle layout, and I defined a Gradle task that runs JavaScript unit tests in the Exec task. The inputs to this task are JavaScript files in the project, so they are restarted only when one of the source files changes. The task is added to all JavaScript projects from the main project.

Question: I want to expand this so that the tests can be re-run if the JavaScript files in the project or in any of its dependencies between the projects are changed. What is the best way to do this?

The code below works if it is placed in each subproject assembly file (after the dependency declaration), but we have 20+ JavaScript subprojects, and I would like to remain 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 command 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 do not need to solve all the file dependencies on my own?

+4
source share
3 answers

As mentioned in the answer, adding the jsTest task to closing subprojects will make it very easy to add jstesting support for each subproject. I think you can make it easier to configure your inputs by declaring the 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" commandLine ... } } 
+2
source

Is it possible to do something like this?

 allprojects {project -> task testJavaScript(type: Exec, dependsOn: configurations.js) { inputs.files resolveJavascriptDependenciesFor(project) // Run tests in JSTestDriver using command line call... } } def resolveJavascriptDependenciesFor(project) { def files = project.jsSourceFiles project.configurations.js.allDependencies.each { files = files + resolveJavascriptDependenciesFor(it.dependencyProject) } return files } 

Thus, the task is performed in all projects and will be called recursively. Not quite sure if this works, but I think this is the way to go

0
source

I found a solution that works, but not very well, using the same dependency specification as in the question. It loads gradle files in a slightly different order in the master project.

build.gradle :

 subprojects { configurations { js } // Apply the subproject 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 with the previous, broken build.gradle

 subprojects { configurations { js } apply from: project.parent.file('javaScriptProjectTasks.gradle') // The subproject build.gradle is automatically loaded } 
0
source

Source: https://habr.com/ru/post/1437219/


All Articles