如何强制更新 intellij 中的所有快照 Gradle 依赖项

我有一个项目与 SNAPSHOT 的依赖关系使用它的建设工具在 intellij 的 gradle。

问题是 intellij 使用的 SNAPSHOTS 现在已经过时了。 enter image description here

当我在命令行上生成项目时

gradle build or
gradle clean build --refresh-dependencies

在命令行上获取最新的依赖项。我还设置了我的等级文件,总是下载快照依赖项 根据这个答案

如何强制 intellij 下载所有依赖项?

93382 次浏览

I have run into some very sticky snapshots. There are a few options you can try:

  • On the Gradle tab (right side of UI), click the blue circling arrows icon, which should refresh the dependencies (works in most cases)
  • If that does not work, try running the gradle command in IntelliJ using the Green "run Gradle command" icon - this runs the command in IntelliJs environment not that of your local machine.
  • If both of those fail, you can modify your Gradle resolutionStrategy settings to something like: configurations.all { resolutionStrategy.cacheDynamicVersionsFor 4, 'hours' resolutionStrategy.cacheChangingModulesFor 4, 'hours' } This config change is a last-ditch option and should be used sparingly. It basically tells Gradle to refresh the local cache more often. You should click the IntelliJ Gradle refresh button after making these changes.

Another option is to open up the Project Structure, and under Project Settings, Libraries, find the dependency in the list and remove it. Then press the Gradle refresh blue circling arrows icon and IntelliJ should fetch the latest version.

enter image description here

enter image description here

In IntelliJ 2017.2 you can right-click on the project name in the Gradle Tool Window and select Refresh dependencies from the context menu.

Refresh Gradle dependencies in IntelliJ 2017.2

This will refresh all your dependencies, not only the SNAPSHOTS, so it might take a while. I don't know if other versions of IntelliJ also have this feature.

EDIT

On Gradle 6+, snapshots are changing by default. So, no need to set changing = true anymore.


Previous answer:

Gradle caches changing modules for 24 hours by default. We can tell Gradle to refresh or redownload dependencies in the build script by marking those as 'changing'.

Follow these steps:

Step #1: Tell Gradle not to cache changing modules by set the value of the cacheChangingModulesFor property to 0 second:

configurations.all {
resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
}

Step #2: Mark dependencies which are needed to be refreshed or redownloaded, as changing module:

dependencies {
implementation("com.howtoprogram.buysell:payment-api:0.0.1-SNAPSHOT") {
changing = true
}
}

Source: Refresh or Redownload Dependencies in Gradle

IntelliJ IDEA ULTIMATE 2020.1

Right-click on the project name in the Gradle Tool Window and select Refresh Gradle Dependencies from the context menu.

enter image description here

The easiest way to ensure that IntelliJ adds all dependencies is to invalidate the project caches and restart. It will take about a minute to reload the project.

INTELLIJ IDE

File -> Invalidate Caches/Restart...

Now as for ensuring that the latest snapshot is used, you must specify the version of the dependency you are using in your gradle.build file.

Typically there is no 'latest' anchor version in most repositories. You'll simply have to look and see what the latest version is.

Theoretically if you knew the release dates and versioning system you could write a Groovy function that returned the dependency URL and pass it to implementation.

def getLatestVersion(){
...build version string
return version //As string
}


implementation getLatestVersion()

But it would not be recommended even if you referencing a CI/CD pipeline in case there were any changes outside of your control