1

It is an example of a build.gradle set on an empty freshly created Gradle/Groovy project on IntellJ. I use projectDir here in two places: in task definition and in task class definition. IntelliJ shows me the first use as incorrect - cannot resolve symbol. And it sees the second use as a correct one.

group 'test'
version '1.0-SNAPSHOT'

apply plugin: 'java'
apply plugin: 'groovy'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    compile 'org.codehaus.groovy:groovy-all:2.3.11'
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

task ProjectDirTest(type: ProjectDirTestClass){
    println " -------------- $projectDir from task"
}

class ProjectDirTestClass extends DefaultTask {
    @TaskAction
    def greet() {
        println " -------------- $projectDir from class"
    }
}
configure(ProjectDirTest) {
    group = 'ProjectDirTest'
    description = 'ProjectDirTest'
}

But if I run the task, the first println works OK, with correct output:

12:55:41: Executing external task 'ProjectDirTest'...
 -------------- C:\Users\543829657\testWorkspace from task
:ProjectDirTest FAILED

FAILURE: Build failed with an exception.

* Where:
Build file 'C:\Users\543829657\testWorkspace\build.gradle' line: 28

* What went wrong:
Execution failed for task ':ProjectDirTest'.
> Could not get unknown property 'projectDir' for task ':ProjectDirTest' of type ProjectDirTestClass.

But, as you see, at the second println there is a problem - Gradle does not see the projectDir variable.

I am already accustomed to incorrect IntelliJ marking errors. But how can I make the task class to see the the projectDir variable in runtime?

In the real task class I also cannot use the exec() command - Gradle does not see it, too.

Gangnus
  • 24,044
  • 16
  • 90
  • 149

1 Answers1

1

I have found a temporary solution, to pass the project variable into the class and call all project variables with it:

task ProjectDirTest(type: ProjectDirTestClass){
    println " -------------- $projectDir from task"
    projectLoc = project
}

class ProjectDirTestClass extends DefaultTask {
    Project projectLoc
    @TaskAction
    def greet() {
        println " -------------- ${projectLoc.projectDir} from class"
    }
}

The negative side is that I have to pass the same project for every task using that task class. It is not a good style.

Gangnus
  • 24,044
  • 16
  • 90
  • 149