You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@maven.apache.org by Laird Nelson <lj...@gmail.com> on 2014/01/08 19:50:41 UTC

During plugin development, what is the context classloader set to?

I have a plugin that I'm writing that needs to do two things during the
course of its execution:
Load a resource from the current project's classpath
Load a resource from its own guts

This is a fallback kind of thing: if the plugin can't find anything
appropriate on the project classpath, then and only then do I want it to
examine its own .jar file for the resource in question.

My hunch is that there are probably already two classloaders set up for
just this purpose.

I am guessing (haven't tried yet) that the project classpath is probably
visible to Thread.currentThread().getContextClassLoader().  Is that right?

I'm also guessing (haven't tried yet) that from within my mojo
this.getClass().getClassLoader() will return me a ClassLoader that is set
up to be able to see my mojo's innards.  Is that right?

Best,
Laird

-- 
http://about.me/lairdnelson

Re: During plugin development, what is the context classloader set to?

Posted by Baptiste Mathus <ml...@batmat.net>.
Hi,
ClassWorlds is your friend if you need to do classloader fine-tuning.

For example (taken in an old poc, so this should be adapted to use
annotations etc.), something along the following would create a classpath
with test scope elements.

...
public class SomeMojo extends AbstractMojo
{
        /**
         * @parameter expression="${project.testClasspathElements}"
         */
        private
List<http://www.google.com/search?sitesearch=java.sun.com&q=allinurl%3Aj2se%2F1+5+0%2Fdocs%2Fapi+List>
<String <http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html>>
 testProjectArtifacts;
...
        protected
ClassLoader<http://java.sun.com/j2se/1.5.0/docs/api/java/lang/ClassLoader.html>
 getClassLoader()
        {
                try
                {
                        ClassWorld world = new ClassWorld();
                        ClassRealm realm = world.newRealm("project");
                        for
(String<http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html>
artifact
: testProjectArtifacts)
                        {
                                getLog().debug("Constituent : " + artifact);
                                realm.addConstituent(new
File<http://java.sun.com/j2se/1.5.0/docs/api/java/io/File.html>
(artifact).toURL());
                        }
                        return realm.getClassLoader();
                }
                catch
(Exception<http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Exception.html>
 e)
                {
                        throw new
RuntimeException<http://java.sun.com/j2se/1.5.0/docs/api/java/lang/RuntimeException.html>
("WTF", e);
                }
        }

HTH


2014/1/8 Laird Nelson <lj...@gmail.com>

> I have a plugin that I'm writing that needs to do two things during the
> course of its execution:
> Load a resource from the current project's classpath
> Load a resource from its own guts
>
> This is a fallback kind of thing: if the plugin can't find anything
> appropriate on the project classpath, then and only then do I want it to
> examine its own .jar file for the resource in question.
>
> My hunch is that there are probably already two classloaders set up for
> just this purpose.
>
> I am guessing (haven't tried yet) that the project classpath is probably
> visible to Thread.currentThread().getContextClassLoader().  Is that right?
>
> I'm also guessing (haven't tried yet) that from within my mojo
> this.getClass().getClassLoader() will return me a ClassLoader that is set
> up to be able to see my mojo's innards.  Is that right?
>
> Best,
> Laird
>
> --
> http://about.me/lairdnelson
>
> --
> Baptiste <Batmat> MATHUS - http://batmat.net
> Sauvez un arbre,
> Mangez un castor ! nbsp;! <http://about.me/lairdnelson>