Exec task not recognized

I am trying to run a shell script from my gradle build but it utterly fails to compile/run

import org.gradle.api.tasks.Exec
    ...
task generateProto(type: Exec)  {
      workingDir "${projectDir}"
      commandLine = './generate_protobuf.sh'
}

build.dependsOn generateProto

gives me the following error

Could not determine the dependencies of task ':generateProto'.
> Could not find method workingDir() for arguments [...

According to the latest documentation this should work. Also my IDE (IntelliJ) is marking the calls to workingDir and commandLine as not found. I am running the build from commandLine, just to be sure it is not a IDE bug.

I am on Gradle 4.7

Your script works as expected for me on Gradle 4.7. The only modification I made was to replace the ... with applying the the base plugin so that the build task referenced in the last line exists. There’s not enough information here to troubleshoot.

Something must be off with my installation then. I’ve installed Gradle via SDKMAN.

This is my full build

import org.gradle.api.tasks.Exec

plugins {
  id "scala"
}

ext {
  versionScala = '2.12.5'
  versionScalaTest = '3.0.5'
  versionScalaLogging = '3.5.0'
  versionAkka = '2.5.12'
  versionLogback = '1.1.8'
  versionLevelDb = '0.10'
}

version = '0.1.0'

repositories {
  mavenCentral()
}

sourceSets.main.scala.srcDirs += 'build/generated/source/proto/main/scala'

dependencies {
  compile group: 'com.typesafe.akka', name: 'akka-http_2.12', version: '10.1.1'
  compile group: 'com.typesafe.akka', name: 'akka-stream_2.12', version: versionAkka
  compile group: 'com.typesafe.akka', name: 'akka-persistence_2.12', version: versionAkka
  compile group: 'org.fusesource.leveldbjni', name: 'leveldbjni-all', version: '1.8'
  compile group: 'com.google.protobuf', name: 'protobuf-java', version: '3.1.0'
  compile group: 'com.thesamet.scalapb', name: 'scalapb-runtime_2.12', version: '0.7.2'
  testCompile group: 'junit', name: 'junit', version: '4.+'
  testCompile group: 'org.scalatest', name: 'scalatest_2.12', version: versionScalaTest
  testCompile group: 'com.typesafe.akka', name: 'akka-testkit_2.12', version: versionAkka
  testCompile group: 'com.typesafe.akka', name: 'akka-http-testkit_2.12', version: '10.1.1'
  testCompile "org.specs2:specs2-core_2.11:4.0.3"
}

task preBuild {
  new File("build/generated/source/proto/main/scala").mkdirs()
}

task generateProto(type: Exec) dependsOn(preBuild)  {
  workingDir "${projectDir}"
  commandLine = './generate_protobuf.sh'
}

build.dependsOn generateProto

You do actually have a problem with your buildscript that wasn’t shown in the original post:

The dependsOn(preBuild) needs to be inside the parenthesis, using the map notation:

task generateProto(type: Exec, dependsOn: preBuild)  {
    workingDir "${projectDir}"
    commandLine = './generate_protobuf.sh'
}

OR inside the closure:

task generateProto(type: Exec) {
    dependsOn(preBuild)
    workingDir "${projectDir}"
    commandLine = './generate_protobuf.sh'
}
1 Like

doh!

Well, the message was kind of misleading. Thank you very much