You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@maven.apache.org by Stevo Slavić <ss...@gmail.com> on 2009/12/31 12:25:01 UTC

Accessing MavenSession from a plugin component

Hello Maven users,

Can anyone please point me to some docs or example on how one can access a
maven session from a plugin plexus.component?

Regards,
Stevo.

Re: Accessing MavenSession from a plugin component

Posted by Stevo Slavić <ss...@gmail.com>.
Thanks to both for feedback!

I'm experimenting a bit with maven archetype (plugin), trying to implement
support for importing remote archetype catalog in local archetype catalog.
While at it I noticed couple of potential issues. E.g.
org.apache.maven.archetype.Archetype.getDefaultLocalCatalog() method javadoc
states that that method "Gives the catalog of archetypes located in
$user.home/.m2/repository/archetype-catalog.xml" but default implementation
(org.apache.maven.archetype.DefaultArchetype) actually looks for the local
catalog using $user.home/.m2/archetype-catalog.xml path. One error is that
implementation and javadoc are not aligned.
org.apache.maven.archetype.source.CatalogArchetypeDataSource does the dirty
work of replacing "${user.home}" in path with user.home system property
value.

Other issue is that this behavior of looking for local archetype catalog
from ${user.home} based path is contrary to what I as maven user would
expect - local archetype catalog lists archetypes in local repository, so
IMO local catalog should be looked up relative to local repository path; if
one changes local repository path, different and not the same local
archetype catalog should be used to avoid having local archetype catalog
corrupt (pointing to archetypes which are not in local repository). Each
system user can configure her own localRepository path to achieve user
specificness of local archetype catalog location, where and when needed.

To be able to resolve ${settings.localRepository} expression value in
CatalogArchetypeDataSource (component) I needed expression evaluator
(PluginParameterExpressionEvaluator) reference, which for its construction
requires maven session and maven mojo execution references. Obtaining maven
session reference in CatalogArchetypeDataSource component via code you
suggested, and obtaining mojo execution reference via following declaration

    /**
     * @parameter expression="${mojoExecution}"
     * @readonly
     * @required
     *
     */
    protected MojoExecution execution;

in CatalogArchetypeDataSource is always null - injection via @parameter
seems to work only for mojo classes, not for plain components they reference
(note: needles to say, I still have to learn plexus basics).

Then I tried replacing settings.localRepository expression evaluation
through expression evaluator, with simple string replacement of expression
with localRepository.getBasedir(  ), where localRepository is declared in
CatalogArchetypeDataSource as

    /**
     * Local maven repository.
     *
     * @parameter expression="${localRepository}"
     * @required
     * @readonly
     */
    protected ArtifactRepository localRepository;

but in vain, that would result in null reference too, likely for the same
reason.

Jason van Zyl mentions in his maven 3
presentation<http://www.sonatype.com/people/2009/04/jason-van-zyl-on-the-future-of-maven-maven-3/>that
"expression evaluator will become part of the execution environment"
(I
assume this means one will be able in maven 3 to just declare reference to
ExecutionEvaluator with @component and have it injected in any other
component, like CatalogArchetypeDataSource). Can one already do that in
maven 2 manually, e.g. by declaring some plexus context (or equivalent)
instructing it to instantiate and manage an ExecutionEvaluator
implementation instance?


Btw, it is odd to me too that it is allowed (no NPE thrown) to construct
PluginParameterExpressionEvaluator with null as mojoExecution, and there is
no setter for mojoExecution, but mojoExecution is always being accessed/used
in evaluate method call (there is a line in evaluate method with call
"mojoExecution.getMojoDescriptor();"). I've looked in maven 3' maven-core,
API there has been somewhat changed even to support just passing maven
session to PluginParameterExpressionEvaluator constructor with maven
execution defaulting to null, to get same effect of getting NPE when calling
evaluate method. Maybe that new constructor
("PluginParameterExpressionEvaluator( MavenSession session )") is never
used. If so, I wonder why does it exist.

Regards,
Stevo.

On Fri, Jan 1, 2010 at 5:44 PM, Tony Chemit <ch...@codelutin.com> wrote:

> Le Fri, 1 Jan 2010 01:25:21 +0100,
> Olivier Lamy <ol...@apache.org> a écrit :
>
> > Hi,
> >
> > Maybe :
> >
> >     /**
> >      * The Maven Session Object
> >      *
> >      * @parameter expression="${session}"
> >      * @required
> >      * @readonly
> >      */
> >     protected MavenSession session;
> >
> > ?
> >
> and for sure adding the maven-core as a dependency of your mojo artifact ?
> > HTH,
> > --
> > Olivier
> >
> > 2009/12/31 Stevo Slavić <ss...@gmail.com>:
> > > Hello Maven users,
> > >
> > > Can anyone please point me to some docs or example on how one can
> access a
> > > maven session from a plugin plexus.component?
> > >
> > > Regards,
> > > Stevo.
> > >
> >
> >
> >
>
>
>
> --
>
> Tony Chemit
> --------------------
> tél: +33 (0) 2 40 50 29 28
> email: chemit@codelutin.com
> http://www.codelutin.com
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> For additional commands, e-mail: users-help@maven.apache.org
>
>

Re: Accessing MavenSession from a plugin component

Posted by Olivier Lamy <ol...@apache.org>.
2010/1/1 Tony Chemit <ch...@codelutin.com>:
> Le Fri, 1 Jan 2010 01:25:21 +0100,
> Olivier Lamy <ol...@apache.org> a écrit :
>
>> Hi,
>>
>> Maybe :
>>
>>     /**
>>      * The Maven Session Object
>>      *
>>      * @parameter expression="${session}"
>>      * @required
>>      * @readonly
>>      */
>>     protected MavenSession session;
>>
>> ?
>>
> and for sure adding the maven-core as a dependency of your mojo artifact ?


Why not ?
If you use public api and not private implementation, it's not a problem

>> HTH,
>> --
>> Olivier
>>
>> 2009/12/31 Stevo Slavić <ss...@gmail.com>:
>> > Hello Maven users,
>> >
>> > Can anyone please point me to some docs or example on how one can access a
>> > maven session from a plugin plexus.component?
>> >
>> > Regards,
>> > Stevo.
>> >
>>
>>
>>
>
>
>
> --
>
> Tony Chemit
> --------------------
> tél: +33 (0) 2 40 50 29 28
> email: chemit@codelutin.com
> http://www.codelutin.com
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> For additional commands, e-mail: users-help@maven.apache.org
>
>



-- 
Olivier

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


Re: Accessing MavenSession from a plugin component

Posted by Tony Chemit <ch...@codelutin.com>.
Le Fri, 1 Jan 2010 01:25:21 +0100,
Olivier Lamy <ol...@apache.org> a écrit :

> Hi,
> 
> Maybe :
> 
>     /**
>      * The Maven Session Object
>      *
>      * @parameter expression="${session}"
>      * @required
>      * @readonly
>      */
>     protected MavenSession session;
> 
> ?
> 
and for sure adding the maven-core as a dependency of your mojo artifact ?
> HTH,
> --
> Olivier
> 
> 2009/12/31 Stevo Slavić <ss...@gmail.com>:
> > Hello Maven users,
> >
> > Can anyone please point me to some docs or example on how one can access a
> > maven session from a plugin plexus.component?
> >
> > Regards,
> > Stevo.
> >
> 
> 
> 



-- 

Tony Chemit
--------------------
tél: +33 (0) 2 40 50 29 28
email: chemit@codelutin.com  
http://www.codelutin.com 

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


Re: Accessing MavenSession from a plugin component

Posted by Olivier Lamy <ol...@apache.org>.
Hi,

Maybe :

    /**
     * The Maven Session Object
     *
     * @parameter expression="${session}"
     * @required
     * @readonly
     */
    protected MavenSession session;

?

HTH,
--
Olivier

2009/12/31 Stevo Slavić <ss...@gmail.com>:
> Hello Maven users,
>
> Can anyone please point me to some docs or example on how one can access a
> maven session from a plugin plexus.component?
>
> Regards,
> Stevo.
>



-- 
Olivier

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