java 如何修复此 gradle 依赖项解析错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25908012/
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-11-02 08:55:42  来源:igfitidea点击:

How to fix this gradle dependency resolution error?

javabuildgradledependency-management

提问by mrt181

I am new to gradle and I am trying to use it for an existing java project.

我是 gradle 的新手,我正在尝试将它用于现有的 java 项目。

I have created settings.gradle file in my root directory:

我在我的根目录中创建了 settings.gradle 文件:

include ':datamodel'
include ':active'
include ':client'
include ':zero'
include ':toall'

and build.gradle

和 build.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
    }
}

The build.gradle in datamodel looks like this:

datamodel 中的 build.gradle 如下所示:

apply plugin: 'java'
sourceSets {
    main.java.srcDirs = ['src/main/java']
    main.resources.srcDirs = ['src/main/java']
    test.java.srcDirs = ['tests/java']
    test.resources.srcDirs = ['tests/resources']
}

dependencies {
    compile 'com.google.protobuf:protobuf-java:2.5.+'
}

When I run gradle build (version: 2.0) I get this failure

当我运行 gradle build (version: 2.0) 我得到这个失败

FAILURE: Build failed with an exception.

* What went wrong:
Could not resolve all dependencies for configuration ':datamodel:compile'.
> Could not find any version that matches com.google.protobuf:protobuf-java:2.5.+.
  Required by:
      MyRoot:datamodel:unspecified

How can I fix this?

我怎样才能解决这个问题?

采纳答案by Radim

Add

添加

repositories {
    mavenCentral()
}

to your build script. Not as a part of buildscriptclosure where you can define where to look for dependencies required to execute your build script. You can do it in your top-level build.gradleusing

到您的构建脚本。不是作为buildscript闭包的一部分,您可以在其中定义在哪里查找执行构建脚本所需的依赖项。你可以在你的顶级build.gradle使用

allprojects {
    repositories {
        mavenCentral()
    }
}

回答by Prabhakaran Ramaswamy

Try this

试试这个

compile 'com.google.protobuf:protobuf-java:2.5.0'

More info here

更多信息在这里

The maven repo looks like

Maven 回购看起来像

<dependency>
    <groupId>com.google.protobuf</groupId>
    <artifactId>protobuf-java</artifactId>
    <version>2.5.0</version>
</dependency>

回答by pavlini4

You can also try and add jcenter()to dependencies like this

您也可以尝试添加jcenter()到这样的依赖项

dependencies {
    mavenCentral()
    jcenter()
}

which will allow to search not only in repo1.maven.org

这将允许搜索不仅在 repo1.maven.org