Java Gradle 构建不会下载依赖项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38485819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 20:22:14  来源:igfitidea点击:

Gradle build doesn't download dependencies

javaspringgradle

提问by zero01alpha

After running gradle buildin the root directory of my my web app, the spring security dependency declared in build.gradledoes not get downloaded.

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

here is my 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'
}

instead, I just get this message

相反,我只是收到这条消息

: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

This is a spring mvc web app that I ran the gradle initcommand in

这是我在其中运行gradle init命令的 spring mvc Web 应用程序

采纳答案by JBirdVegas

System caches the dependent jars so it won't be downloaded again and again.

系统缓存依赖的 jars,所以它不会被一次又一次地下载。

If your goal is to just see the downloads of the dependencies then you can force it to redownload.

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

Remove any dependency caches stored locally [1]

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

$ rm -rf ~/.gradle/caches/

Then restart your build

然后重启你的构建

$ gradlew clean build

You could also force a dependency update with [2]

您还可以使用 [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

[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

回答by randikawann

If your project builds successfully some time it may be gradle download problem with a current proxy. Gradle has it's own dependency management system similar to maven. I think parts of the gradle publish plugin are backed by maven in some way (not verified). Regardless you shouldn't have to worry about that level of depth, gradle will handle it. Your problem is setting up the proxy. You just need to set some variables in $projectDir/gradle.properties, for example:

如果您的项目在一段时间内成功构建,则可能是当前代理的 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

This can be used to download dependencies without proxy. If you want to use a proxy for you can use the code as below instead of above code.

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

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

Proxy port and host can be changed as you want.

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

回答by Top-Master

had something like this problem while was building older react-nativeproject.

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

the react-native run-androidcommand just did print:

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

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

after lot of changes to the build.gradlefile noticed that it was okay and just opened the androiddirectory of my react-nativeproject in Android-Studioand all dependencies was downloaded.

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

but to prevent download of files again and again used GradleCopyto make them available offline and changed the build.gradlefile like below:

但是为了防止一次又一次地下载文件,使用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"
        }
    }
}

(i.e. did set offlinevariable to my m2repositorypath and used it like: maven { url uri(offline) })

(即没有设置offline变量,以我的m2repository路径,并用它喜欢:maven { url uri(offline) }

回答by Artem Botnev

The solution that helped in my case:

对我有帮助的解决方案:

File -> Invalidate Caches/Restart...

回答by Joman68

I'm using IntelliJ 2018.2.3 and Gradle was not downloading dependencies for me.

我正在使用 IntelliJ 2018.2.3 并且 Gradle 没有为我下载依赖项。

I found that I had to uncheck the 'Offline work' box in the Gradle settings to get it to download them. I'm not sure how this box became checked because I didn't check it (honest).

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