无法从 gradle 解析 com.google.guava

could not resolve com.google.guava from gradle

来自 Netbeans 的奇怪警告:

尽管我从 gradle 本身看不到任何东西,至少到目前为止:

thufir@dur:~/NetBeansProjects/expectit$ 
thufir@dur:~/NetBeansProjects/expectit$ gradle clean

BUILD SUCCESSFUL in 748ms
1 actionable task: 1 up-to-date
thufir@dur:~/NetBeansProjects/expectit$ 
thufir@dur:~/NetBeansProjects/expectit$ tree
.
├── build.gradle
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── LICENSE
├── README.md
├── settings.gradle
└── src
    ├── main
    │   ├── java
    │   │   └── expectit
    │   │       └── App.java
    │   └── resources
    └── test
        ├── java
        │   └── expectit
        │       └── AppTest.java
        └── resources

11 directories, 10 files
thufir@dur:~/NetBeansProjects/expectit$ 

我只 运行 git init 在一个基本上是空的目录上,所以它只是一种好奇心。

但是我如何至少从 gradle 本身生成此警告(这大概是 IDE 正在做的事情)?

我根据说明添加了 guava,因此 cherry build file 是:

/*
 * This file was generated by the Gradle 'init' task.
 *
 * This generated file contains a sample Java project to get you started.
 * For more details take a look at the Java Quickstart chapter in the Gradle
 * User Manual available at https://docs.gradle.org/6.3/userguide/tutorial_java_projects.html
 */

plugins {
    // Apply the java plugin to add support for Java
    id 'java'

    // Apply the application plugin to add support for building a CLI application.
    id 'application'
}

repositories {
    // Use jcenter for resolving dependencies.
    // You can declare any Maven/Ivy/file repository here.
    jcenter()
    mavenCentral()
}

dependencies {
    // This dependency is used by the application.
    implementation 'com.google.guava:guava:28.2-jre'

    // Use TestNG framework, also requires calling test.useTestNG() below
    testImplementation 'org.testng:testng:7.0.0'
}

application {
    // Define the main class for the application.
    mainClassName = 'expectit.App'
}

test {
    // Use TestNG for unit tests
    useTestNG()
}

我只added maven central 如上;仍然从 IDE.

得到相同的警告

gradle版本:

thufir@dur:~/NetBeansProjects/expectit$ 
thufir@dur:~/NetBeansProjects/expectit$ gradle -v

------------------------------------------------------------
Gradle 6.3
------------------------------------------------------------

Build time:   2020-03-24 19:52:07 UTC
Revision:     bacd40b727b0130eeac8855ae3f9fd9a0b207c60

Kotlin:       1.3.70
Groovy:       2.5.10
Ant:          Apache Ant(TM) version 1.10.7 compiled on September 1 2019
JVM:          11.0.7 (AdoptOpenJDK 11.0.7+10)
OS:           Linux 5.4.0-29-generic amd64

thufir@dur:~/NetBeansProjects/expectit$ 

gradlew 版本:

thufir@dur:~/NetBeansProjects/expectit$ 
thufir@dur:~/NetBeansProjects/expectit$ ./gradlew --version
Downloading https://services.gradle.org/distributions/gradle-6.3-bin.zip
.........10%..........20%..........30%.........40%..........50%..........60%.........70%..........80%..........90%..........100%

------------------------------------------------------------
Gradle 6.3
------------------------------------------------------------

Build time:   2020-03-24 19:52:07 UTC
Revision:     bacd40b727b0130eeac8855ae3f9fd9a0b207c60

Kotlin:       1.3.70
Groovy:       2.5.10
Ant:          Apache Ant(TM) version 1.10.7 compiled on September 1 2019
JVM:          11.0.7 (AdoptOpenJDK 11.0.7+10)
OS:           Linux 5.4.0-29-generic amd64

thufir@dur:~/NetBeansProjects/expectit$ 

假设您的包装器版本与您安装的版本相同。问题源于 Netbeans 不支持 Gradle 6(参见 gradle plugin for netbeans)。

最好的选择就是切换到 Gradle 5(支持)。为此,您需要从 gradle/wrapper/gradle-wrapper.properties 更新包装器属性。 修改 distributionUrl 值,并将结尾从 gradle-6.x-bin.zip 更改为其他版本,例如 gradle-5.2.1-bin.zip.

然后 运行 再次构建,以便下载包装器并再次尝试 NetBeans。

据我所知,build.gradle 应该也适用于版本 5,因此无需修改它。