【问题标题】:Gradle build doesn't download dependenciesGradle 构建不下载依赖项
【发布时间】:2016-11-23 23:01:25
【问题描述】:

在我的 web 应用的根目录中运行 gradle build 后,build.gradle 中声明的 spring 安全依赖项没有被下载。

这是我的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 网络应用程序

【问题讨论】:

  • 已经下载了。检查相关文件夹。
  • @SkyWalker 但我在我的网络应用程序中的任何地方都看不到它,
  • @iMassakre 'don't see it' == 无法在您的代码中引用它?
  • @iMassakre 你用什么IDE,如果有的话?你有编译时或运行时错误吗?
  • @yonisha 我正在使用 Eclipse。没有编译或运行时错误。我只是想为我的网络应用下载 Spring Security。

标签: java spring gradle


【解决方案1】:

系统会缓存依赖的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

【讨论】:

    【解决方案2】:

    对我有帮助的解决方案:

    File -> Invalidate Caches/Restart...
    

    【讨论】:

    • 也许值得一提的是,这个答案假设您使用的是 Intellij 或其他缓存依赖项的 IDE。也就是说,这对我有用。
    【解决方案3】:

    我使用的是 IntelliJ 2018.2.3,而 Gradle 没有为我下载依赖项。

    我发现我必须取消选中 Gradle 设置中的“离线工作”框才能下载它们。我不确定这个框是如何被选中的,因为我没有选中它(老实说)。

    编辑:在 IntelliJ 2021.2.1 离线模式现在可以在 Gradle 工具窗口中切换,如下所示:

    【讨论】:

    • 你拯救了我的一天。在我的情况下,检查“切换离线模式”没有触发库下载。取消选中按钮并..从文件>无效缓存中删除缓存文件...解决了问题。
    【解决方案4】:

    如果您的项目在一段时间内成功构建,则可能是当前代理的 gradle 下载问题。 Gradle 有自己的依赖管理系统,类似于 maven。我认为 gradle 发布插件的某些部分以某种方式得到了 maven 的支持(未验证)。无论您不必担心这种深度,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
    

    代理端口和主机可以随意更改。

    【讨论】:

      【解决方案5】:

      在构建较旧的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) }

      【讨论】:

        猜你喜欢
        • 2013-01-30
        • 2023-04-08
        • 2023-03-15
        • 2020-10-09
        • 2014-04-05
        • 2015-03-20
        • 2015-11-30
        • 2014-02-21
        • 2014-03-08
        相关资源
        最近更新 更多