On Tue, Sep 06, 2005 at 12:52:01PM +0100, Ashley Williams wrote:
> I've been bashing my head against a brick wall trying to get the list  
> of artifacts back from the project object for my plugin but  
> project.getArtifacts() always returns an empty list whereas I expect  
> the list of artifacts corresponding to the target pom <dependency> tags.
> 
> 1. I recreated the problem by creating a default project like so:
> m2 archetype:create -DgroupId=acme -DartifactId=test - 
> DarchetypeArtifactId=maven-archetype-mojo
> 
> 2. then I replaced the generated mojo with this class that has a goal  
> called test and whose execute method just prints out the artifacts  
> size and it always prints out zero:
> 
> package com.mycompany;
> 
> import org.apache.maven.plugin.AbstractMojo;
> import org.apache.maven.plugin.MojoExecutionException;
> import org.apache.maven.project.MavenProject;
> 
> /**
>  * Test Mojo
>  *
>  * @goal test
>  *
>  * @execute phase="generate-sources"

I think you need to add 

 @requiresDependencyResolution [compile|test|runtime]

here. See [1] for the complete reference.

>  */
> public class MyMojo extends AbstractMojo {
>     /**
>      * @parameter expression="${project}"
>      * @required
>      */
>     private MavenProject project;
> 
>     public void execute() throws MojoExecutionException {
>         System.out.println("project.getName()=" + project.getName());
>         System.out.println("project.getArtifacts().size()="
>                 + project.getArtifacts().size());
>     }
> }
> 
> (So that it would compile I had to add a dependency in the pom:
>     <dependency>
>       <groupId>org.apache.maven</groupId>
>       <artifactId>maven-project</artifactId>
>       <version>2.0-beta-1-SNAPSHOT</version>
>     </dependency>)

You should not have a dependency on MavenProject (and thus maven-project)
because this makes your mojo Maven specific. Instead have two parameters
and expressions like this:

 @parameter expression="${project.name}

 @parameter expression="${project.artifacts}

> 
> 3. Finally I ran the plugin in the same directory as my target poms  
> like so:
> m2 acme:test:1.0-SNAPSHOT:test
> 
> and got the following results no matter which pom I ran it against:
> 
> project.getName()=MBean Demo
> project.getArtifacts().size()=0
> 
> 
> Notice that the name that is printed corresponds to the target pom so  
> the plugin does manage to pick up some details ok.
> 
> Maybe I should get the dependencies using a different api, but its  
> the same call used by the eclipse plugin at  
> EclipseWriter.writeEclipseClasspath just before the call to  
> addDependency().
> 
> Anyone shed any light?

[1]: http://maven.apache.org/maven2/developers/mojo-api-specification.html

--
Trygve

Attachment: signature.asc
Description: Digital signature

Reply via email to