英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论