Dealing with ClassLoader issues in Gradle Plugins

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Dealing with ClassLoader issues in Gradle Plugins

johnrengelman
Hey everyone -
I'm trying to write a plugin that uses the Apache ZipOutputStream class in it and I can't seem to get it to work. The class appears to be available via 'gradleApi()', but when I publish the plugin and install it to a project, I end up with a 
NoClassDefFoundException for ZipOutputStream.
Then I explicitly added the org.apache.ant:ant dependency to the plugin and republished and now I get a ClassCastException:

Caused by: java.lang.ClassCastException: org.apache.tools.zip.ZipOutputStream cannot be cast to org.apache.tools.zip.ZipOutputStream.

I think what's happening is that the class in the Gradle Jars that are being used, but are being filtered out by the Gradle URLClassLoader structure (I believe some internal Gradle classes are hidden from the build script's class path?)

Anyway, is there a certain way I should be handling this?

-- John

Reply | Threaded
Open this post in threaded view
|

Re: Dealing with ClassLoader issues in Gradle Plugins

Adam Murdoch

On 29 Apr 2014, at 12:40 am, John Engelman <[hidden email]> wrote:

Hey everyone -
I'm trying to write a plugin that uses the Apache ZipOutputStream class in it and I can't seem to get it to work. The class appears to be available via 'gradleApi()', but when I publish the plugin and install it to a project, I end up with a 
NoClassDefFoundException for ZipOutputStream.
Then I explicitly added the org.apache.ant:ant dependency to the plugin and republished and now I get a ClassCastException:

Caused by: java.lang.ClassCastException: org.apache.tools.zip.ZipOutputStream cannot be cast to org.apache.tools.zip.ZipOutputStream.

Have you got a stack trace for this failure?


--
Adam Murdoch
Gradle Co-founder
http://www.gradle.org
VP of Engineering, Gradleware Inc. - Gradle Training, Support, Consulting
http://www.gradleware.com

Join us for Gradle Summit 2014, June 12th and 13th in Santa Clara, CA: http://www.gradlesummit.com

Reply | Threaded
Open this post in threaded view
|

Re: Dealing with ClassLoader issues in Gradle Plugins

johnrengelman
As I was getting this stack trace, it occurred to me what is probably happening. I think I was inadvertently using a class from org.gradle.internal and that's where the error is coming from:

Caused by: java.lang.ClassCastException: org.apache.tools.zip.ZipOutputStream cannot be cast to org.apache.tools.zip.ZipOutputStream
at com.github.jengelman.gradle.plugins.shadow.tasks.ShadowCopyAction$1.execute(ShadowCopyAction.groovy)
at org.gradle.internal.IoActions.withResource(IoActions.java:74)
at org.gradle.internal.IoActions$withResource.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:42)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:120)
at com.github.jengelman.gradle.plugins.shadow.tasks.ShadowCopyAction.execute(ShadowCopyAction.groovy:49)
at org.gradle.api.internal.file.copy.NormalizingCopyActionDecorator.execute(NormalizingCopyActionDecorator.java:52)
at org.gradle.api.internal.file.copy.DuplicateHandlingCopyActionDecorator.execute(DuplicateHandlingCopyActionDecorator.java:42)
at org.gradle.api.internal.file.copy.CopyActionExecuter.execute(CopyActionExecuter.java:38)
at org.gradle.api.tasks.AbstractCopyTask.copy(AbstractCopyTask.java:65)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:63)
at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.doExecute(AnnotationProcessingTaskFactory.java:219)
at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.execute(AnnotationProcessingTaskFactory.java:212)
at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.execute(AnnotationProcessingTaskFactory.java:201)
at org.gradle.api.internal.AbstractTask$TaskActionWrapper.execute(AbstractTask.java:533)
at org.gradle.api.internal.AbstractTask$TaskActionWrapper.execute(AbstractTask.java:516)
at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeAction(ExecuteActionsTaskExecuter.java:80)
at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:61)
... 47 more

-- John

On Monday, April 28, 2014 at 5:51 PM, Adam Murdoch [via Gradle] wrote:


On 29 Apr 2014, at 12:40 am, John Engelman <[hidden email]> wrote:

Hey everyone -
I'm trying to write a plugin that uses the Apache ZipOutputStream class in it and I can't seem to get it to work. The class appears to be available via 'gradleApi()', but when I publish the plugin and install it to a project, I end up with a 
NoClassDefFoundException for ZipOutputStream.
Then I explicitly added the org.apache.ant:ant dependency to the plugin and republished and now I get a ClassCastException:

Caused by: java.lang.ClassCastException: org.apache.tools.zip.ZipOutputStream cannot be cast to org.apache.tools.zip.ZipOutputStream.

Have you got a stack trace for this failure?


--
Adam Murdoch
Gradle Co-founder
http://www.gradle.org
VP of Engineering, Gradleware Inc. - Gradle Training, Support, Consulting
http://www.gradleware.com

Join us for Gradle Summit 2014, June 12th and 13th in Santa Clara, CA: http://www.gradlesummit.com




If you reply to this email, your message will be added to the discussion below:
http://gradle.1045684.n5.nabble.com/Dealing-with-ClassLoader-issues-in-Gradle-Plugins-tp5712621p5712626.html
To start a new topic under gradle-dev, email [hidden email]
To unsubscribe from gradle-dev, click here.
NAML

Reply | Threaded
Open this post in threaded view
|

Re: Dealing with ClassLoader issues in Gradle Plugins

johnrengelman
In reply to this post by Adam Murdoch
Hmm, that seems to be the problem, but I can't seem to figure out a way around this. What I'm trying to do is extend the Jar/Zip tasks in Gradle to copy the contents of Jar/Zip files using JarInputStream/JarOutputStreams instead of decompressing the files to a temporary directory copying them and then decompressing. I am doing this by overriding the 'createCopyAction' method to return a custom class that implements CopyAction. However, I cannot use the protected methods in the class else I run into the cast exceptions below.

Is there a better way that I could do this or am I just going to be limited at this point?

-- John

On Monday, April 28, 2014 at 6:14 PM, John Engelman wrote:

As I was getting this stack trace, it occurred to me what is probably happening. I think I was inadvertently using a class from org.gradle.internal and that's where the error is coming from:

Caused by: java.lang.ClassCastException: org.apache.tools.zip.ZipOutputStream cannot be cast to org.apache.tools.zip.ZipOutputStream
at com.github.jengelman.gradle.plugins.shadow.tasks.ShadowCopyAction$1.execute(ShadowCopyAction.groovy)
at org.gradle.internal.IoActions.withResource(IoActions.java:74)
at org.gradle.internal.IoActions$withResource.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:42)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:120)
at com.github.jengelman.gradle.plugins.shadow.tasks.ShadowCopyAction.execute(ShadowCopyAction.groovy:49)
at org.gradle.api.internal.file.copy.NormalizingCopyActionDecorator.execute(NormalizingCopyActionDecorator.java:52)
at org.gradle.api.internal.file.copy.DuplicateHandlingCopyActionDecorator.execute(DuplicateHandlingCopyActionDecorator.java:42)
at org.gradle.api.internal.file.copy.CopyActionExecuter.execute(CopyActionExecuter.java:38)
at org.gradle.api.tasks.AbstractCopyTask.copy(AbstractCopyTask.java:65)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:63)
at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.doExecute(AnnotationProcessingTaskFactory.java:219)
at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.execute(AnnotationProcessingTaskFactory.java:212)
at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.execute(AnnotationProcessingTaskFactory.java:201)
at org.gradle.api.internal.AbstractTask$TaskActionWrapper.execute(AbstractTask.java:533)
at org.gradle.api.internal.AbstractTask$TaskActionWrapper.execute(AbstractTask.java:516)
at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeAction(ExecuteActionsTaskExecuter.java:80)
at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:61)
... 47 more

-- John

On Monday, April 28, 2014 at 5:51 PM, Adam Murdoch [via Gradle] wrote:


On 29 Apr 2014, at 12:40 am, John Engelman <[hidden email]> wrote:

Hey everyone -
I'm trying to write a plugin that uses the Apache ZipOutputStream class in it and I can't seem to get it to work. The class appears to be available via 'gradleApi()', but when I publish the plugin and install it to a project, I end up with a 
NoClassDefFoundException for ZipOutputStream.
Then I explicitly added the org.apache.ant:ant dependency to the plugin and republished and now I get a ClassCastException:

Caused by: java.lang.ClassCastException: org.apache.tools.zip.ZipOutputStream cannot be cast to org.apache.tools.zip.ZipOutputStream.

Have you got a stack trace for this failure?


--
Adam Murdoch
Gradle Co-founder
http://www.gradle.org
VP of Engineering, Gradleware Inc. - Gradle Training, Support, Consulting
http://www.gradleware.com

Join us for Gradle Summit 2014, June 12th and 13th in Santa Clara, CA: http://www.gradlesummit.com




If you reply to this email, your message will be added to the discussion below:
http://gradle.1045684.n5.nabble.com/Dealing-with-ClassLoader-issues-in-Gradle-Plugins-tp5712621p5712626.html
To start a new topic under gradle-dev, email [hidden email]
To unsubscribe from gradle-dev, click here.
NAML


Reply | Threaded
Open this post in threaded view
|

Re: Dealing with ClassLoader issues in Gradle Plugins

johnrengelman
In reply to this post by Adam Murdoch
I was able to work around the issue by adding the following to my plugin. It's dirty, but it at least proves my suspicion:

FilteringClassLoader filter = getServices().get(ClassLoaderRegistry).gradleApiClassLoader.parent
filter.allowPackage('org.apache.tools.zip')
filter.allowPackage('org.apache.commons.io')

-- John

On Monday, April 28, 2014 at 8:08 PM, John Engelman wrote:

Hmm, that seems to be the problem, but I can't seem to figure out a way around this. What I'm trying to do is extend the Jar/Zip tasks in Gradle to copy the contents of Jar/Zip files using JarInputStream/JarOutputStreams instead of decompressing the files to a temporary directory copying them and then decompressing. I am doing this by overriding the 'createCopyAction' method to return a custom class that implements CopyAction. However, I cannot use the protected methods in the class else I run into the cast exceptions below.

Is there a better way that I could do this or am I just going to be limited at this point?

-- John

On Monday, April 28, 2014 at 6:14 PM, John Engelman wrote:

As I was getting this stack trace, it occurred to me what is probably happening. I think I was inadvertently using a class from org.gradle.internal and that's where the error is coming from:

Caused by: java.lang.ClassCastException: org.apache.tools.zip.ZipOutputStream cannot be cast to org.apache.tools.zip.ZipOutputStream
at com.github.jengelman.gradle.plugins.shadow.tasks.ShadowCopyAction$1.execute(ShadowCopyAction.groovy)
at org.gradle.internal.IoActions.withResource(IoActions.java:74)
at org.gradle.internal.IoActions$withResource.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:42)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:120)
at com.github.jengelman.gradle.plugins.shadow.tasks.ShadowCopyAction.execute(ShadowCopyAction.groovy:49)
at org.gradle.api.internal.file.copy.NormalizingCopyActionDecorator.execute(NormalizingCopyActionDecorator.java:52)
at org.gradle.api.internal.file.copy.DuplicateHandlingCopyActionDecorator.execute(DuplicateHandlingCopyActionDecorator.java:42)
at org.gradle.api.internal.file.copy.CopyActionExecuter.execute(CopyActionExecuter.java:38)
at org.gradle.api.tasks.AbstractCopyTask.copy(AbstractCopyTask.java:65)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:63)
at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.doExecute(AnnotationProcessingTaskFactory.java:219)
at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.execute(AnnotationProcessingTaskFactory.java:212)
at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.execute(AnnotationProcessingTaskFactory.java:201)
at org.gradle.api.internal.AbstractTask$TaskActionWrapper.execute(AbstractTask.java:533)
at org.gradle.api.internal.AbstractTask$TaskActionWrapper.execute(AbstractTask.java:516)
at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeAction(ExecuteActionsTaskExecuter.java:80)
at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:61)
... 47 more

-- John

On Monday, April 28, 2014 at 5:51 PM, Adam Murdoch [via Gradle] wrote:


On 29 Apr 2014, at 12:40 am, John Engelman <[hidden email]> wrote:

Hey everyone -
I'm trying to write a plugin that uses the Apache ZipOutputStream class in it and I can't seem to get it to work. The class appears to be available via 'gradleApi()', but when I publish the plugin and install it to a project, I end up with a 
NoClassDefFoundException for ZipOutputStream.
Then I explicitly added the org.apache.ant:ant dependency to the plugin and republished and now I get a ClassCastException:

Caused by: java.lang.ClassCastException: org.apache.tools.zip.ZipOutputStream cannot be cast to org.apache.tools.zip.ZipOutputStream.

Have you got a stack trace for this failure?


--
Adam Murdoch
Gradle Co-founder
http://www.gradle.org
VP of Engineering, Gradleware Inc. - Gradle Training, Support, Consulting
http://www.gradleware.com

Join us for Gradle Summit 2014, June 12th and 13th in Santa Clara, CA: http://www.gradlesummit.com




If you reply to this email, your message will be added to the discussion below:
http://gradle.1045684.n5.nabble.com/Dealing-with-ClassLoader-issues-in-Gradle-Plugins-tp5712621p5712626.html
To start a new topic under gradle-dev, email [hidden email]
To unsubscribe from gradle-dev, click here.
NAML