简体   繁体   中英

Gradle custom Type : Default Task “error: cannot find symbol”

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'

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()
}

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM