我如何告诉gradle从存储库中重新下载依赖项?


当前回答

你可以告诉Gradle重新下载构建脚本中的依赖项,方法是将依赖项标记为“正在更改”。Gradle将每24小时检查一次更新,但是这可以使用resoltionstrategy DSL进行配置。我发现在SNAPSHOT或NIGHTLY构建中使用它很有用。

configurations.all {
    // Check for updates every build
    resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
}

扩展:

dependencies {
    implementation group: "group", name: "projectA", version: "1.1-SNAPSHOT", changing: true
}

压缩:

implementation('group:projectA:1.1-SNAPSHOT') { changing = true }

我在这个论坛找到了这个解决方案。

其他回答

就我而言,上述方法都没用,我所做的是:

在构建。gradle,注释与未解决的导入相关的依赖项 点击“立即同步” 取消我刚才注释的内容 再次点击“立即同步”

然后我的导入又被正确地解决了。

对于Windows……为了让gradle重新下载特定的依赖项:

从下面的目录中删除要重新下载的依赖项: C:\Users\ %用户名% \模块2 \ \ .gradle \缓存文件- 2.1 删除该路径下的所有元数据目录: C:\Users\ %用户名% \模块2 \ \ .gradle \缓存元数据- * 在项目的根目录下运行gradle build(如果使用gradle wrapper,则运行gradlew build)。

注意:上述文件路径中的数字可能与您不同。

我认为gradle 2.14.1解决了这个问题。接受的答案是正确的,但有一个bug在gradle与-refresh-dependencies。2.14.1修复了这个问题。

参见https://discuss.gradle.org/t/refresh-dependencies-should-use-cachechangingmodulesfor-0s/556

有两种方法:

使用命令行选项刷新依赖项现金。 您可以删除本地缓存,其中artefast是由Gradle和触发构建缓存

使用——refresh-dependencies选项:

./gradlew build --refresh-dependencies

简单解释——refresh-dependencies选项告诉Gradle忽略已解析模块和工件的所有缓存条目。

长explanantion

WIth –refresh-dependencies’ Gradle will always hit the remote server to check for updated artifacts: however, Gradle will avoid downloading a file where the same file already exists in the cache. First Gradle will make a HEAD request and check if the server reports the file as unchanged since last time (if the ‘content-length’ and ‘last-modified’ are unchanged). In this case you’ll get the message: "Cached resource is up-to-date (lastModified: {})." Next Gradle will determine the remote checksum if possible (either from the HEAD request or by downloading a ‘.sha1’ file).. If this checksum matches another file already downloaded (from any repository), then Gradle will simply copy the file in the cache, rather than re-downloading. In this case you’ll get the message: "“Found locally available resource with matching checksum: [{}, {}]”.

使用删除: 当你删除缓存

rm -rf $HOME/.gradle/caches/

你只需要清理所有缓存的jar和sha1和,Gradle在你的机器上没有工件的情况下,必须下载所有东西。是的,第一次它将100%工作,但是当另一个快照被释放并且它是依赖树的一部分时,您将再次面临刷新或清除缓存的选择。

如果你使用的是最新版本的Gradle,你可以使用——refresh-dependencies选项。

./gradlew build --refresh-dependencies

你可以参考Gradle手册。

——refresh-dependencies选项告诉Gradle忽略已解析模块和工件的所有缓存条目。将对所有配置的存储库执行新的解析,重新计算动态版本,刷新模块,并下载工件。