Java.lang.NoClassDefFoundError: org/apache/commons/io/FileUtils

I use a custom Gradle plugin which utilises the commons-io library from a Gradle distribution via gradleApi(). Currently my project build behaves in a very strage way:

  1. I run a task from the plugin and build failes with an expected exception from inside the plugin;
  2. I run the same task and build failes with java.lang.NoClassDefFoundError: org/apache/commons/io/FileUtils thrown from the plugin. This behaviour repeats over and over until I run the clean task.
    How to debug this?