[英]Want to run a custom gradle task only when running Unit test
How to set up gradle to run a particular custom copy task, only when running unit tests? 如何设置gradle来运行特定的自定义复制任务,仅在运行单元测试时?
EDIT 编辑
I want to run these tasks when i press build, i. 当我按下构建时,我想运行这些任务,i。 e only in the flavor of the build with unit test execution included.
e仅在构建的风格中包含单元测试执行。
I've finally found the solution, with the help of this documentation which presents all the tasks that run during build
, test
, release
etc. in a very concise manner. 我终于在这个文档的帮助下找到了解决方案, 该文档以非常简洁的方式呈现了在
build
, test
, release
等过程中运行的所有任务。 So by making tasks clean
, preBuild
depend on my copyTask
I can ensure that the copy task is run every time the project is cleaned or built. 因此,通过使任务
clean
, preBuild
依赖于我的copyTask
我可以确保每次清理或构建项目时都运行复制任务。
But since I don't want to run this during building or cleaning process but want to run it when I only run tests, I identified a task that compiles release unit test sources called the compileReleaseUnitTestSources
but just mentioning it in build.gradle as 但是因为我不想在构建或清理过程中运行它,而是想在我只运行测试时运行它,所以我确定了一个编译发布单元测试源的任务,称为
compileReleaseUnitTestSources
但只是在build.gradle中提到它
compileReleaseUnitTestSources.dependsOn(myCopyTask)
doesn't actually work, because gradle will give an error saying it cannot find the task compileReleaseUnitTestSources
as for some reason that task is not available yet. 实际上并不起作用,因为gradle会给出一个错误,说它无法找到任务
compileReleaseUnitTestSources
,因为某些原因该任务尚未可用。 Instead by enclosing it in a afterEvaluate
block we can ensure that this block is executed after all tasks are evaluated, that way we have access to that task now, So finally I added this to my build.gradle 而是将它封装在
afterEvaluate
块中,我们可以确保在afterEvaluate
所有任务后执行此块,这样我们现在可以访问该任务,所以最后我将它添加到我的build.gradle
afterEvaluate {
compileReleaseUnitTestSources.dependsOn(copyResDirectoryToClasses)
}
All the answers here mention to use the dependsOn
keyword to attach my task to another task that is run during general build/test execution, but none of them mentioned how to go around the problem where gradle is not able to find the tasks even though you know for sure that these tasks were available and run during build/test execution. 这里的所有答案都提到使用
dependsOn
关键字将我的任务附加到在一般构建/测试执行期间运行的另一个任务,但是他们都没有提到如何解决gradle无法找到任务的问题,即使你确保这些任务可用并在构建/测试执行期间运行。
you have to set up a "customCopyTask" and make the "test-task" which does the unittests depend on the "customCopyTask" like this 你必须设置一个“customCopyTask”并使“测试任务”进行单元测试取决于像这样的“customCopyTask”
task customCopyTask(type: Copy) {
from sourceSets.test.resources
into sourceSets.test.output.classesDir
}
test.dependsOn customCopyTask
You can make some task finalizing another, in that case this task will run only if another one was called, right after it. 你可以让一些任务完成另一个任务,在这种情况下,只有在它之后调用另一个任务时,该任务才会运行。 This could be done as:
这可以这样做:
task runUnitTest << {
println 'running tests'
}
task copyTestResults << {
println 'copying results'
}
//make copyTestResults finalize runUnitTest
runUnitTest.finalizedBy copyTestResults
You can read about it in the official user guide . 您可以在官方用户指南中阅读相关内容。
Additionally, if your unit test could be up-to-date and you don't want to run you copy task in that case, you can check the test task status and skip copy-task, as: 此外,如果您的单元测试可能是最新的,并且您不想在这种情况下运行复制任务,则可以检查测试任务状态并跳过复制任务,如下所示:
task copyTestResults {
doFirst {
//chtck anothe task status and skip this one if it didn't actually work
if (!tasks.getByName("runUnitTest").getState().didWork) {
throw new StopExecutionException();
}
}
doLast{
println 'copying results'
}
}
Or, if you just need to run copy-task before unit tests, make the test task depending on copy-task by setting it's dependsOn
property, read about it with a number og examples here 或者,如果您只需要在单元测试之前运行copy-task,请通过设置它的
dependsOn
属性来使测试任务依赖于copy-task, 在此处使用数字og示例进行读取
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.