maven-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Baptiste Mathus ...@batmat.net>
Subject Re: During plugin development, what is the context classloader set to?
Date Tue, 14 Jan 2014 13:20:57 GMT
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 <ljnelson@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>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message