[GRADLE-2963] "org.apache.commons.io.FileUtils" class is available for compile, but isn't available in runtime Created: 26/Nov/13  Updated: 27/Jan/17  Resolved: 24/Jan/17

Status: Resolved
Project: Gradle
Affects Version/s: None
Fix Version/s: 2.14-rc-6

Type: Bug
Reporter: Gradle Forums Assignee: Unassigned
Resolution: Fixed Votes: 2


 Description   

This issue is similar to [1]http://forums.gradle.org/gradle/topic...

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;
}
}
----------------------------------------------------------------------------------------
[1] http://forums.gradle.org/gradle/topics/classes_in_buildsrc_throws_noclassdeffounderror_on_dependencies_defined_in_gradle_script



 Comments   
Comment by Gradle Forums [ 26/Nov/13 ]

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

Comment by Gradle Forums [ 26/Nov/13 ]

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

Comment by Gradle Forums [ 26/Nov/13 ]

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.

Comment by Gradle Forums [ 26/Nov/13 ]

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.

Comment by Gradle Forums [ 26/Nov/13 ]

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.

Comment by Gradle Forums [ 26/Nov/13 ]

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; } }
Comment by Per Agerbæk [ 18/Mar/14 ]

Same problem in 1.11. Had to add commons-io to classpath in buildscript.

Comment by Benjamin Muschko [ 15/Nov/16 ]

As announced on the Gradle blog we are planning to completely migrate issues from JIRA to GitHub.

We intend to prioritize issues that are actionable and impactful while working more closely with the community. Many of our JIRA issues are inactionable or irrelevant. We would like to request your help to ensure we can appropriately prioritize JIRA issues you’ve contributed to.

Please confirm that you still advocate for your JIRA issue before December 10th, 2016 by:

  • Checking that your issues contain requisite context, impact, behaviors, and examples as described in our published guidelines.
  • Leave a comment on the JIRA issue or open a new GitHub issue confirming that the above is complete.

We look forward to collaborating with you more closely on GitHub. Thank you for your contribution to Gradle!

Comment by Benjamin Muschko [ 24/Jan/17 ]

Gradle 2.14 changed the behavior. For more details see the release notes.

Generated at Wed Jun 30 12:36:09 CDT 2021 using Jira 8.4.2#804003-sha1:d21414fc212e3af190e92c2d2ac41299b89402cf.