You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@maven.apache.org by Mark Derricutt <ma...@talios.com> on 2008/10/13 12:40:04 UTC

Accessing an artifacts classpath from a mojo?

Hi all,

I have a maven plugin I'm working on and I want to include the classpath
(main, test, others...) of the artifact calling the plugin - am I going to
have to spawn/fork a new java process and include them on the classpath
somehow, or is their some mojo-magic that takes care of this?

Mark

-- 
"It is easier to optimize correct code than to correct optimized code." --
Bill Harlan

Re: Accessing an artifacts classpath from a mojo?

Posted by Mark Derricutt <ma...@talios.com>.
Cheers - I'll take a look.  Basically, in my instance, I have my library for
migrating databases [1] which I'm wrapping with a maven plugin, the plugin
walks the classpath finding classes with the @DataMigration annotation
(opening JarFile instances and walking File[] lists to determine available
classnames), then instantiating them and calling their methods where
appropriate.

As Brett says - I might be able to get away with just creating a Classloader
looking at the target/classes directory.

Mark

[1] http://code.google.com/p/dbng/wiki/IntroductionToDatabaseNG

On Tue, Oct 14, 2008 at 5:28 AM, Jason van Zyl <ja...@maven.org> wrote:

> Look at the Jetty plugin, Jan has done most of the work there and I'm sure
> you can lift it all.


-- 
"It is easier to optimize correct code than to correct optimized code." --
Bill Harlan

Re: Accessing an artifacts classpath from a mojo?

Posted by Mark Struberg <st...@yahoo.de>.
Hi Jason!

Thanks for spotting the jetty plugin.
Jetty is now using an own classloader, but I looked at old versions and managed to get classworlds working the way I need it.
There are a few hacks in the source, so maybe someone has a tip for making this code prettier.


LieGrue,
strub

PS: the full code of the new openjpa-maven-plugin version can be found here: 
git-clone http://ns1.backwork.net/git/openjpa-maven-plugin.git
PPS: thanks to Rahul for providing the first release of the openjpa plugin and doing some additional reviews now.

    /**
     * This will prepare the current ClassLoader and add all jars and local
     * classpaths (e.g. target/classes) needed by the OpenJPA task.
     * 
     * @throws MojoExecutionException
     */
    protected void extendRealmClasspath() 
        throws MojoExecutionException 
    { 
        ClassWorld world = new ClassWorld();
        ClassRealm jpaRealm;
        
        try 
        {
            jpaRealm = world.newRealm( "maven.plugin.openjpa"
                                     , Thread.currentThread().getContextClassLoader() );
        } 
        catch ( DuplicateRealmException e ) 
        {
            throw new MojoExecutionException( "problem while creating new ClassRealm", e );
        }

        Iterator itor = classpathElements.iterator();
        
        while (itor.hasNext()) 
        {
            File pathelem = new File( (String) itor.next() );
            getLog().debug( "adding classpathElement " + pathelem.getPath() );
            try 
            {
                // we need to use 3 slashes to prevent windoof from interpreting 'file://D:/path' as server 'D'
                // we also have to add a trailing slash after directory paths
                URL url = new URL("file:///" + pathelem.getPath() + ( pathelem.isDirectory() ? "/" : "" ) );
                jpaRealm.addConstituent( url );
            } 
            catch ( MalformedURLException e ) 
            {
                throw new MojoExecutionException( "error in adding the classpath " + pathelem, e );
            }
        }

        // set the new ClassLoader as default for this Thread
        Thread.currentThread().setContextClassLoader( jpaRealm.getClassLoader() );
    }
    



--- Jason van Zyl <ja...@maven.org> schrieb am Mo, 13.10.2008:

> Von: Jason van Zyl <ja...@maven.org>
> Betreff: Re: Accessing an artifacts classpath from a mojo?
> An: "Maven Users List" <us...@maven.apache.org>
> Datum: Montag, 13. Oktober 2008, 18:28
> Look at the Jetty plugin, Jan has done most of the work
> there and I'm  
> sure you can lift it all.
> 
> On 13-Oct-08, at 8:23 AM, Mark Struberg wrote:
> 
> > Hi!
> >
> > I currently face a very similar problem: I'd like
> to check for a  
> > resource target/classes/META-INF/persistence.xml from
> my plugin via  
> > the plugins classloader (need the JDBC config from
> there).
> >
> > I'm currently messing around with Classworlds, but
> haven't succeed  
> > so far.
> >
> > It would be a great if you can point us to a plugin
> which has  
> > implemented such a mechanism already. I'd hate to
> call java via the  
> > CommandLine for this :(
> >
> >
> > txs and LieGrue,
> > strub
> >
> > --- Brett Porter <br...@gmail.com>
> schrieb am Mo, 13.10.2008:
> >
> >> Von: Brett Porter <br...@gmail.com>
> >> Betreff: Re: Accessing an artifacts classpath from
> a mojo?
> >> An: "Maven Users List"
> <us...@maven.apache.org>
> >> Datum: Montag, 13. Oktober 2008, 12:45
> >> How do you need to use it? It can be quite simple
> to create
> >> a
> >> classloader for this, but it depends on how
> it'll be
> >> used.
> >>
> >> Cheers,
> >> Brett
> >>
> >> On 13/10/2008, at 9:40 PM, Mark Derricutt wrote:
> >>
> >>> Hi all,
> >>>
> >>> I have a maven plugin I'm working on and I
> want to
> >> include the
> >>> classpath
> >>> (main, test, others...) of the artifact
> calling the
> >> plugin - am I
> >>> going to
> >>> have to spawn/fork a new java process and
> include them
> >> on the
> >>> classpath
> >>> somehow, or is their some mojo-magic that
> takes care
> >> of this?
> >>>
> >>> Mark
> >
> >
> >
> >
> >
> >
> ---------------------------------------------------------------------
> > To unsubscribe, e-mail:
> users-unsubscribe@maven.apache.org
> > For additional commands, e-mail:
> users-help@maven.apache.org
> >
> 
> Thanks,
> 
> Jason
> 
> ----------------------------------------------------------
> Jason van Zyl
> Founder,  Apache Maven
> jason at sonatype dot com
> ----------------------------------------------------------
> 
> You are never dedicated to something you have complete
> confidence in.
> No one is fanatically shouting that the sun is going to
> rise tomorrow.
> They know it is going to rise tomorrow. When people are
> fanatically
> dedicated to political or religious faiths or any other
> kind of
> dogmas or goals, it's always because these dogmas or
> goals are in doubt.
> 
>    -- Robert Pirzig, Zen and the Art of Motorcycle
> Maintenance
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> For additional commands, e-mail:
> users-help@maven.apache.org

__________________________________________________
Do You Yahoo!?
Sie sind Spam leid? Yahoo! Mail verfügt über einen herausragenden Schutz gegen Massenmails. 
http://mail.yahoo.com 

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


Re: Accessing an artifacts classpath from a mojo?

Posted by Jason van Zyl <ja...@maven.org>.
Look at the Jetty plugin, Jan has done most of the work there and I'm  
sure you can lift it all.

On 13-Oct-08, at 8:23 AM, Mark Struberg wrote:

> Hi!
>
> I currently face a very similar problem: I'd like to check for a  
> resource target/classes/META-INF/persistence.xml from my plugin via  
> the plugins classloader (need the JDBC config from there).
>
> I'm currently messing around with Classworlds, but haven't succeed  
> so far.
>
> It would be a great if you can point us to a plugin which has  
> implemented such a mechanism already. I'd hate to call java via the  
> CommandLine for this :(
>
>
> txs and LieGrue,
> strub
>
> --- Brett Porter <br...@gmail.com> schrieb am Mo, 13.10.2008:
>
>> Von: Brett Porter <br...@gmail.com>
>> Betreff: Re: Accessing an artifacts classpath from a mojo?
>> An: "Maven Users List" <us...@maven.apache.org>
>> Datum: Montag, 13. Oktober 2008, 12:45
>> How do you need to use it? It can be quite simple to create
>> a
>> classloader for this, but it depends on how it'll be
>> used.
>>
>> Cheers,
>> Brett
>>
>> On 13/10/2008, at 9:40 PM, Mark Derricutt wrote:
>>
>>> Hi all,
>>>
>>> I have a maven plugin I'm working on and I want to
>> include the
>>> classpath
>>> (main, test, others...) of the artifact calling the
>> plugin - am I
>>> going to
>>> have to spawn/fork a new java process and include them
>> on the
>>> classpath
>>> somehow, or is their some mojo-magic that takes care
>> of this?
>>>
>>> Mark
>
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> For additional commands, e-mail: users-help@maven.apache.org
>

Thanks,

Jason

----------------------------------------------------------
Jason van Zyl
Founder,  Apache Maven
jason at sonatype dot com
----------------------------------------------------------

You are never dedicated to something you have complete confidence in.
No one is fanatically shouting that the sun is going to rise tomorrow.
They know it is going to rise tomorrow. When people are fanatically
dedicated to political or religious faiths or any other kind of
dogmas or goals, it's always because these dogmas or
goals are in doubt.

   -- Robert Pirzig, Zen and the Art of Motorcycle Maintenance


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


Re: Accessing an artifacts classpath from a mojo?

Posted by Mark Struberg <st...@yahoo.de>.
Hi!

I currently face a very similar problem: I'd like to check for a resource target/classes/META-INF/persistence.xml from my plugin via the plugins classloader (need the JDBC config from there).

I'm currently messing around with Classworlds, but haven't succeed so far.

It would be a great if you can point us to a plugin which has implemented such a mechanism already. I'd hate to call java via the CommandLine for this :(


txs and LieGrue,
strub

--- Brett Porter <br...@gmail.com> schrieb am Mo, 13.10.2008:

> Von: Brett Porter <br...@gmail.com>
> Betreff: Re: Accessing an artifacts classpath from a mojo?
> An: "Maven Users List" <us...@maven.apache.org>
> Datum: Montag, 13. Oktober 2008, 12:45
> How do you need to use it? It can be quite simple to create
> a  
> classloader for this, but it depends on how it'll be
> used.
> 
> Cheers,
> Brett
> 
> On 13/10/2008, at 9:40 PM, Mark Derricutt wrote:
> 
> > Hi all,
> >
> > I have a maven plugin I'm working on and I want to
> include the  
> > classpath
> > (main, test, others...) of the artifact calling the
> plugin - am I  
> > going to
> > have to spawn/fork a new java process and include them
> on the  
> > classpath
> > somehow, or is their some mojo-magic that takes care
> of this?
> >
> > Mark



      

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


Re: Accessing an artifacts classpath from a mojo?

Posted by Brett Porter <br...@gmail.com>.
How do you need to use it? It can be quite simple to create a  
classloader for this, but it depends on how it'll be used.

Cheers,
Brett

On 13/10/2008, at 9:40 PM, Mark Derricutt wrote:

> Hi all,
>
> I have a maven plugin I'm working on and I want to include the  
> classpath
> (main, test, others...) of the artifact calling the plugin - am I  
> going to
> have to spawn/fork a new java process and include them on the  
> classpath
> somehow, or is their some mojo-magic that takes care of this?
>
> Mark
>
> -- 
> "It is easier to optimize correct code than to correct optimized  
> code." --
> Bill Harlan


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