“Gradle init项目无法向’app’子项目添加依赖项”

huangapple go评论122阅读模式
英文:

Gradle init project not able to add dependencies to "app" subproject

问题

我使用Gradle 8.1.1创建了一个新项目,选择了应用程序(2)、Java(3)、没有多个子项目、Groovy DSL、JUnit 5、相同的项目和源包名称以及Java 11。

我将Apache Commons添加到app/build.gradle,并希望在App.java中调用StringUtils,但失败了。起初我以为是VS Code有问题,但运行./gradlew build也失败了,错误如下:

gradle-basic-structure-dep-error\app\src\main\java\gradle\basic\structure\dep\error\App.java:10: error: incompatible types: boolean cannot be converted to String
        return StringUtils.isBlank("  ");
                                  ^
1 error

FAILURE: Build failed with an exception.

这是完整的示例链接:https://github.com/cwansart/gradle-basic-structure-dep-error

为什么会失败?这可能是生成的代码中的错误吗?

英文:

I created a new Gradle 8.1.1 project with gradle init chosing the options application (2), Java (3), no multiple subprojects, Groovy DSL, JUnit 5,the same project and source package name and Java 11.

I added Apache Commons to app/build.gradle and wanted called StringUtils in App.java but it failed. I first thought VS Code had an issue, but running ./gradlew build fails also with:

gradle-basic-structure-dep-error\app\src\main\java\gradle\basic\structure\dep\error\App.java:10: error: incompatible types: boolean cannot be converted to String
        return StringUtils.isBlank("  ");
                                  ^
1 error

FAILURE: Build failed with an exception.

Here is the full example: https://github.com/cwansart/gradle-basic-structure-dep-error

Why is it failing? Might this be an error in the generated code?

答案1

得分: 1

以下是已翻译的内容:

因为 getGreeting() 的返回类型是 String,而 StringUtils.isBlank(" ") 的返回类型是 boolean
所以你应该像下面这样更改返回类型:

public boolean getGreeting() {
    return StringUtils.isBlank("  ");
}

此外,你应该使用像Intelij或Eclipse这样的IDE来编写Java代码。它会为你突出显示和报告这种类型的错误。

英文:

It's because the return type of getGreeting() is String, and return type of StringUtils.isBlank(" ") is a boolean.
So you should change the return type like below

public boolean getGreeting() {
    return StringUtils.isBlank("  ");
}

Beside, you should write java code using IDE like Intelij or Eclipse. It will highlight and report this kind of error for you

答案2

得分: 0

To clarify the issue: in VS Code the imports were marked red and not found, regardless of the error I did mention by Jeremie.

I tried it on a different computer on which it worked. I tried it again on my working machine in a new profile with a clean checkout and it worked again. It seems that the Java or Gradle VSCode plugin had an issue, I guess?

英文:

To clarify the issue: in VS Code the imports were marked red and not find, regardless of the error I did mentioned by Jeremie.

I tried it on a different computer on which it worked. I tried it again on my working machine in a new profile with a clean checkout and it worked again. It seems that the Java or Gradle VSCode plugin had an issue, I guess?

huangapple
  • 本文由 发表于 2023年5月10日 23:44:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76220372.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定