在解决依赖项之前运行gradle任务 [英] Run a gradle task before resolving dependencies

查看:141
本文介绍了在解决依赖项之前运行gradle任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想运行gradle任务,该任务会获取其他源并在gradle尝试解决依赖关系之前进行设置.

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

在build.gradle中,有一个任务可提取子项目的源代码.该任务需要在Gradle尝试解决依赖项之前运行,因为子项目是依赖项的一部分.该任务涉及从远程存储库中获取源,并替换一些build.gradle文件以使构建成为可能.

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.

现在发生的是:

  • 我运行任务.
  • Gradle尝试在实际运行任务之前解决依赖关系.
  • 它失败了,因为其中一个依赖项需要子项目(我的任务应该获取的源).

当然,解决依赖关系是配置"构建阶段的一部分,因此很清楚为什么要在之后运行任务.问题是如何使其在运行之前.

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.

如果我用单独的bash脚本替换gradle任务并在gradle做任何事情之前手动运行它,我当然可以使其工作.但是,这意味着我在gradle和bash脚本中复制了一些变量(例如版本名称和git标记名称).这些变量在gradle中用于其他目的,将它们放在两个位置是不好的.还有其他一些我想避免的原因,其中之一是-使用bash脚本将意味着gradle从头到尾无法完成我们的构建...

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...

推荐答案

首先,您不正确:解决依赖关系是配置"阶段的一部分.如果您使用FileCollection的惰性求值,那么它实际上将在执行阶段得到解决. javadoc .使配置得到解决. AFAIK的核心gradle代码无法在配置"阶段解析配置,但是您的自定义代码可能会导致这种情况(如果是这种情况,我建议您重构)

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)

您可以执行以下操作:

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

这篇关于在解决依赖项之前运行gradle任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆