1

I have the below in my build.gradle file

static def getVersionName() {
    return new File("version").getText().trim()
}

When I use AdoptOpenJDK, it compiles well. However, when I use AzulOpenJDK, it complains:

* What went wrong:
A problem occurred evaluating project ':app'.
> version (No such file or directory)

Is there a difference in how AdoptOpenJDK and AzulOpenJDK handle the above command?

dreamcrash
  • 47,137
  • 25
  • 94
  • 117
Elye
  • 53,639
  • 54
  • 212
  • 474
  • I'm almost 100% sure that there's no difference in how they handle this code. The problem has to be in something else about the environment: What the current directory is or *maybe* something like how they treat setting "unsettable" system properties like `user.dir`. Can you provide a minimal, reproducible example? If that code itself was behaving different, then a trivial Java class should be able to demonstrate that. – Joachim Sauer Apr 20 '21 at 12:01
  • 1
    Don't rely on the working directory in a Gradle script, use e.g. `rootDir` for the root project directory. – Clashsoft Apr 20 '21 at 12:02
  • @Joachim Sauer, I'm using the same machine, just switch over the different JDK. So the environment is exactly the same – Elye Apr 20 '21 at 12:03
  • Thanks, @Clashsoft. Unfortunately `rootProject.rootDir` only available in non static function. Any advice? – Elye Apr 20 '21 at 12:06
  • 1
    Make it not static, or pass the project as a parameter. – Clashsoft Apr 20 '21 at 12:07
  • Nice one. That works! – Elye Apr 20 '21 at 12:11

1 Answers1

2

Making it

def getVersionName() {
    return new File(rootProject.rootDir, "version").getText().trim()
}

now works for both. Thanks @Clashsoft

Elye
  • 53,639
  • 54
  • 212
  • 474