You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@maven.apache.org by David Whitehurst <dl...@gmail.com> on 2006/11/11 22:29:05 UTC

Problem referencing a resource in a plugin jar

Can someone tell me how to get the maven plugin classloader?  Or tell me if
that's I can't obtain the reference file as such ... from a plugin.java file
e.g.

            ClassLoader classLoader = this.getClass().getClassLoader();
            File file = new File(classLoader.getResource
(getLocalProperties().getProperty("templateName")).toString());
            FileReader reader = new FileReader(file);

The templateName is a String e.g.

webstuff/mytemplate.ftl

And it resides in the plugin jar at /webstuff/mytemplate.ftl .  I checked
the jar in the .m2 repo.  Is my FileNotFoundException because I am using the
wrong classloader?  And, if so, what classloader, and how do I get it?


Thanks,

David

Re: Problem referencing a resource in a plugin jar

Posted by David Whitehurst <dl...@gmail.com>.
Tom:

That was it!  I'm in business now with my templates inside the plugin.

Thanks,

David

On 11/11/06, Tom Huybrechts <to...@gmail.com> wrote:
>
> Check the value of classLoader.getResource(...). This should be a URL
> in the form to a resource inside a jar. I don't think turning this
> into a string and passing that to new File() will help. Instead try
> using getting the resource as a stream.
>
> InputStream is = getClass().getClassLoader().getResourceAsStream(...);
> Reader reader = new InputStreamReader(is);
>
>
> On 11/11/06, David Whitehurst <dl...@gmail.com> wrote:
> > Can someone tell me how to get the maven plugin classloader?  Or tell me
> if
> > that's I can't obtain the reference file as such ... from a plugin.javafile
> > e.g.
> >
> >             ClassLoader classLoader = this.getClass().getClassLoader();
> >             File file = new File(classLoader.getResource
> > (getLocalProperties().getProperty("templateName")).toString());
> >             FileReader reader = new FileReader(file);
> >
> > The templateName is a String e.g.
> >
> > webstuff/mytemplate.ftl
> >
> > And it resides in the plugin jar at /webstuff/mytemplate.ftl .  I
> checked
> > the jar in the .m2 repo.  Is my FileNotFoundException because I am using
> the
> > wrong classloader?  And, if so, what classloader, and how do I get it?
> >
> >
> > Thanks,
> >
> > David
> >
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
> For additional commands, e-mail: dev-help@maven.apache.org
>
>

Re: Problem referencing a resource in a plugin jar

Posted by Tom Huybrechts <to...@gmail.com>.
Check the value of classLoader.getResource(...). This should be a URL
in the form to a resource inside a jar. I don't think turning this
into a string and passing that to new File() will help. Instead try
using getting the resource as a stream.

InputStream is = getClass().getClassLoader().getResourceAsStream(...);
Reader reader = new InputStreamReader(is);


On 11/11/06, David Whitehurst <dl...@gmail.com> wrote:
> Can someone tell me how to get the maven plugin classloader?  Or tell me if
> that's I can't obtain the reference file as such ... from a plugin.java file
> e.g.
>
>             ClassLoader classLoader = this.getClass().getClassLoader();
>             File file = new File(classLoader.getResource
> (getLocalProperties().getProperty("templateName")).toString());
>             FileReader reader = new FileReader(file);
>
> The templateName is a String e.g.
>
> webstuff/mytemplate.ftl
>
> And it resides in the plugin jar at /webstuff/mytemplate.ftl .  I checked
> the jar in the .m2 repo.  Is my FileNotFoundException because I am using the
> wrong classloader?  And, if so, what classloader, and how do I get it?
>
>
> Thanks,
>
> David
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
For additional commands, e-mail: dev-help@maven.apache.org