Gradle自定义类型:默认任务 “error: 无法找到符号”

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

Gradle custom Type : Default Task "error: cannot find symbol"

问题

我想要创建一个使用JAVA的SimpleTask Gradle。然而,我无法编译我的项目,因为找不到 "import org.gradle.api.DefaultTask"。
Gradle版本:6.3

如文档所述,我在 root/buildSrc/src/main/java/SimpleTask.java 中创建了一个类。

使用以下代码,我得到一个错误: "error: cannot find symbol",符号:class DefaultTask。

```java
import org.gradle.api.DefaultTask;
import org.gradle.api.tasks.TaskAction;

public class SimpleTask extends DefaultTask {

}

我尝试过添加依赖(但效果相同)。

dependencies {
    // https://mvnrepository.com/artifact/org.gradle/api
    implementation group: 'org.gradle', name: 'api', version: '1.0'
}
英文:

I want to create a SimpleTask Gradle with JAVA. However I cannot compile my projet because "import org.gradle.api.DefaultTask' is not found.
GradleVersion : 6.3

As said on documentation I create a class on root/buildSrc/src/main/java/SimpleTask.java

With this code I have an : "error: cannot find symbol" symbol: class DefaultTask.

import org.gradle.api.DefaultTask;
import org.gradle.api.tasks.TaskAction;

public class SimpleTask extends DefaultTask {

}

I've tried to add the dependency (but same effect)

dependencies {
    // https://mvnrepository.com/artifact/org.gradle/api
    implementation group: 'org.gradle', name: 'api', version: '1.0'

答案1

得分: 5

我对那个依赖项不太熟悉,但如果存在的话,它是五年前的Gradle 1.0版本的。那绝对不是你想要的。

我不认为官方的Gradle API 以依赖项的形式发布。但你可以通过使用 DependencyHandler.gradleApi() 方法,声明对当前版本Gradle的API的依赖,如下所示:

dependencies {
   implementation gradleApi()
}
英文:

I am not familiar with that dependency, but if it exists, it is for Gradle 1.0 from five years ago. That is definitely not what you want.

I don't believe the official Gradle API is published as a dependency. But you can declare a dependency on the API of the current version of Gradle by using the DependencyHandler.gradleApi() method like this:

dependencies {
   implementation gradleApi()
}

答案2

得分: 1

添加这个依赖是不必要的。
实际上,我已经更新了IntelliJ、Gradle和Java插件。当IntelliJ扫描buildSrc/src/main/java目录时,它会将该目录转换为源代码文件夹,并且编译正常。

英文:

Adding the dependency is not necessary.
Actually I have update intelliJ and and the gradle/java plugin. When intelliJ scan a buildSrc/src/main/java directory it transform the directory as a source folder. and compilation are ok.

答案3

得分: 0

我在这里找到了一个很好的教程:https://www.jrebel.com/blog/using-buildsrc-custom-logic-gradle-builds

我的问题是,我在buildSrc中使用IntelliJ定义了一个“源根”,这导致了编译错误。

英文:

I found a good tutorial here : https://www.jrebel.com/blog/using-buildsrc-custom-logic-gradle-builds

My problem was that I defined a "source root" with intelliJ in buildSrc. And it causes compilation error.

huangapple
  • 本文由 发表于 2020年6月5日 20:22:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/62215288.html
匿名

发表评论

匿名网友

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

确定