Gradle构建不会下载依赖项

在我的Web应用程序的根目录中运行gradle build之后, gradle build中声明的spring安全性依赖build.gradle不会被下载。

这是我的build.gradle

 /* * This build file was auto generated by running the Gradle 'init' task * by 'hombod' at '7/19/16 4:19 PM' with Gradle 2.14.1 * * This generated file contains a commented-out sample Java project to get you started. * For more details take a look at the Java Quickstart chapter in the Gradle * user guide available at https://docs.gradle.org/2.14.1/userguide/tutorial_java_projects.html */ // Apply the java plugin to add support for Java apply plugin: 'java' // In this section you declare where to find the dependencies of your project repositories { // Use 'jcenter' for resolving your dependencies. // You can declare any Maven/Ivy/file repository here. jcenter() mavenCentral() } // In this section you declare the dependencies for your production and test code dependencies { // The production code uses the SLF4J logging API at compile time compile 'org.slf4j:slf4j-api:1.7.21' // Declare the dependency for your favourite test framework you want to use in your tests. // TestNG is also supported by the Gradle Test task. Just change the // testCompile dependency to testCompile 'org.testng:testng:6.8.1' and add // 'test.useTestNG()' to your build script. testCompile 'junit:junit:4.12' compile 'org.springframework.security:spring-security-web:4.1.1.RELEASE' } 

相反,我只是得到这个消息

 :compileJava UP-TO-DAT :processResources UP-T :classes UP-TO-DATE :jar UP-TO-DATE :assemble UP-TO-DATE :compileTestJava UP-TO :processTestResources :testClasses UP-TO-DAT :test UP-TO-DATE :check UP-TO-DATE :build UP-TO-DATE 

这是我运行gradle init命令的spring mvc web应用程序

系统缓存从属jar,因此不会一次又一次地下载。

如果您的目标只是查看依赖项的下载,那么您可以强制重新下载。

删除本地存储的所有依赖缓存[1]

 $ rm -rf ~/.gradle/caches/ 

然后重启你的构建

 $ gradlew clean build 

您还可以使用[2]强制执行依赖项更新

 $ gradlew --refresh-dependencies 

[1] https://docs.gradle.org/current/userguide/dependency_management.html#sec:dependency_cache
[2] https://docs.gradle.org/current/userguide/dependency_management.html#sub:cache_refresh

在我的案例中帮助的解决方案:

 File -> Invalidate Caches/Restart... 

如果您的项目成功构建一段时间,可能是当前代理的gradle下载问题。 Gradle拥有类似于maven的自己的依赖管理系统。 我认为gradle发布插件的某些部分以某种方式由maven支持(未经validation)。 无论你不应该担心这种程度的深度,gradle都会处理它。 您的问题是设置代理。 您只需要在$ projectDir / gradle.properties中设置一些变量,例如:

 #http proxy setup systemProp.http.proxyHost=www.somehost.org systemProp.http.proxyPort=8080 systemProp.http.proxyUser=userid systemProp.http.proxyPassword=password systemProp.http.nonProxyHosts=*.nonproxyrepos.com|localhost 

这可用于在没有代理的情况下下载依赖项。 如果您想使用代理,您可以使用下面的代码而不是上面的代码。

 systemProp.https.proxyPort=3128 systemProp.http.proxyHost=192.168.16.2 systemProp.https.proxyHost=192.168.16.2 systemProp.http.proxyPort=3128 

可以根据需要更改代理端口和主机。

在构建较旧的react-native项目时遇到类似这样的问题。

react-native run-android命令只是打印了:

 Could not find com.android.tools.build:gradle:2.3.3 

在对build.gradle文件进行大量更改之后,注意到它没问题,只是在Android-Studio打开了我的react-native项目的android目录,所有依赖项都被下载了。

但是为了防止再次下载文件而使用GradleCopy使它们脱机可用并更改了build.gradle文件,如下所示:

 // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { ext { //kotlin_version = '1.2.40' offline = 'D:/android/sdk/extras/m2repository' } repositories { try { maven { url uri(offline) } } catch (Throwable e) {} try { maven { url uri('C:/Program Files/Android/Android Studio/gradle/m2repository') } } catch (Throwable e) {} jcenter() maven { url 'https://maven.google.com' } mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:2.3.3' //was "2.3.3" with "gradle-3.4.1-all.zip" got "3.1.3" with "gradle-4.4-all.zip" ////below "kotlin" is required in root "build.gradle" else the "offline" repo will not get searched //classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { try { maven { url uri(offline) } } catch (Throwable e) {} jcenter() mavenLocal() maven { url 'https://maven.google.com/' name 'Google' } mavenCentral() maven { // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm url "$rootDir/../node_modules/react-native/android" } } } 

(即确实将offline变量设置为我的m2repository路径并使用它: maven { url uri(offline) }