"org.apache.commons.io.FileUtils" class is available for compile, but isn't available in runtime


(avadhuta) #1

This issue is similar to http://forums.gradle.org/gradle/topics/classes_in_buildsrc_throws_noclassdeffounderror_on_dependencies_defined_in_gradle_script

I try to use class from “commons-io-1.4.jar” library in my custom task. If I don’t define any dependencies, I get successfull compile and “ClassNotFoundException” error on execution. If I copy “commons-io-1.4.jar” into “lib” project folder and define it as dependency, then all works fine. But if I set dependency on library file from standard Gradle instalation, then I get the same error “ClassNotFoundException”. Look at details below.

If you can’t give access to library classes in my code, then you need to raise error, when I try to use this library in dependencies.

c:\Temp\GradleBug\build.gradle task MakeError(type: IOTask){

from ‘clean-here’ }

c:\Temp\GradleBug\buildSrc\build.gradle dependencies {

compile files(‘c:/Java/Gradle/lib/commons-io-1.4.jar’) // PATH TO GRADLE INSTALATION }

c:\Temp\GradleBug\buildSrc\src\main\java\IOTask.java import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; import org.gradle.api.DefaultTask; import org.gradle.api.tasks.TaskAction; import org.gradle.api.internal.file.FileResolver;

public class IOTask

extends DefaultTask {

private Object from;

@TaskAction

public void doSomething() {

try {

FileResolver resolver = this.getServices().get(FileResolver.class);

File clean = resolver.resolve(this.from);

FileUtils.cleanDirectory(clean);

} catch (IOException e) {

e.printStackTrace();

}

}

public IOTask from(Object sourcePath) {

this.from = sourcePath;

return this;

} }


(Peter Niederwieser) #2

Please enclose code and output in HTML code tags. Which exception do you get?


(avadhuta) #3

c:\Temp\GradleBug\build.gradle

task MakeError(type: IOTask){
     from 'clean-here'
 }

c:\Temp\GradleBug\buildSrc\build.gradle

dependencies {
     compile files('c:/Java/Gradle/lib/commons-io-1.4.jar') // PATH TO GRADLE INSTALATION
 }

c:\Temp\GradleBug\buildSrc\src\main\java\IOTask.java

import java.io.File;
 import java.io.IOException;
 import org.apache.commons.io.FileUtils;
 import org.gradle.api.DefaultTask;
 import org.gradle.api.tasks.TaskAction;
 import org.gradle.api.internal.file.FileResolver;
  public class IOTask
 extends DefaultTask {
      private Object from;
      @TaskAction
      public void doSomething() {
          try {
          FileResolver resolver = this.getServices().get(FileResolver.class);
          File clean = resolver.resolve(this.from);
              FileUtils.cleanDirectory(clean);
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
      public IOTask from(Object sourcePath) {
          this.from = sourcePath;
          return this;
      }
  }

I get “ClassNotFoundException” on “org.apache.commons.io.FileUtils” class.

Caused by: java.lang.NoClassDefFoundError: org/apache/commons/io/FileUtils
 at IOTask.doSomething(IOTask.java:19)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.io.FileUtils
 ... 58 more

(Peter Niederwieser) #4

You should never set an explicit dependency on a Jar in the Gradle distribution. Gradle plugins should declare a ‘compile gradleApi()’ dependency. For ‘buildSrc’, this dependency is declared automatically.


(avadhuta) #5

You contradict to yourself. If “commons-io-1.4.jar” library is part of “Gradle API”, then it must be available to my code without any error. If it’s not part, then why I can’t make dependency on already existing file? Why I must make copy of this library and make dependency on this copy? It have so sense.

I think, the root of this problem lie in Gradle classloader, which put “commons-io-1.4.jar” library in some classloader’s tree node, where it is not available for my plugin code.


(Peter Niederwieser) #6

Looking at this again, the problem may be related to your use of an internal Gradle class (‘org.gradle.api.internal.file.FileResolver’). Please try without using that (or any other) internal class, and see if it solves the problem.


(avadhuta) #7

The same problem.

c:\Temp\GradleBug\buildSrc\src\main\java\IOTask.java

import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.gradle.api.DefaultTask;
import org.gradle.api.tasks.TaskAction;
  public class IOTask
        extends DefaultTask {
      private Object from;
   @TaskAction
    public void doSomething() {
        try {
            FileUtils.cleanDirectory(new File(from.toString()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
      public IOTask from(Object sourcePath) {
        this.from = sourcePath;
        return this;
    }
  }

(Peter Niederwieser) #8

Verified against 1.9 and raised GRADLE-2963.


(avadhuta) #9

Thanks.