Run a gradle task before resolving dependencies

随声附和 提交于 2019-12-24 07:51:34

问题


I want to run a gradle task that fetches additional sources and sets them up before gradle tries to resolve dependencies.

In build.gradle there is a task that fetches a sub project's source code. The task needs to be run before Gradle tries to resolve dependencies, because the sub project is part of the dependencies. The task involves fetching sources from a remote repository and replacing a few build.gradle files to make the build possible.

What happens now is that:

  • I run the task.
  • Gradle tries to resolve dependencies before actually running the task.
  • It fails because one of the dependencies requires the sub project (the sources that my task is supposed to fetch).

Of course, resolving dependencies is part of the "Configuration" build phase, so it's pretty clear why the task is run after. The question is how to make it run before.

Of course I can make it work if I replace my gradle task with a separate bash script and run it manually before gradle does anything. However, that would mean that I duplicate some variables in the gradle and the bash scripts (like version names and git tag names). Those variables are used for other purposes in gradle, and having them in two places is bad. There are other reasons I want to avoid that, one of them being - using a bash script would mean that gradle fails at doing our build from start to finish...


回答1:


Firstly you are incorrect that resolving dependencies is part of the "Configuration" phase. If you make use of the lazy evaluation of FileCollection then it will actually be resolved in the execution phase. A configuration will be resolved the first time that resolve() is invoked. Please see the javadoc for the methods which cause a Configuration to be resolved. AFAIK the core gradle code won't resolve a configuration in the "Configuration" phase but your custom code may cause this (I suggest you refactor if this is the case)

You can do something like this:

dependencies {
    // this is lazy evaluated
    compile fileTree(dir: "$buildDir/dynamicJars", include: "*.jar") 
}

task getDynamicJars(type: Copy) {
    from zipTree('path/to/somefile.zip')
    into "$buildDir/dynamicJars"
}

compile.dependsOn getDynamicJars


来源:https://stackoverflow.com/questions/40512117/run-a-gradle-task-before-resolving-dependencies

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!