tags:

views:

69

answers:

1

Hi,

I am trying to call MavenCli from my OSGi plugin project. I have added the correct libraries to my build path and to the manifest. I believe that I have done everything correct so far because when I add a main method to the class that calls maven and run it in eclipse, everything works as expected. When I run it from my OSGi plugin, I get the following error (with the maven switches -X -e):

Apache Maven 3.0-beta-1 (r935667; 2010-04-19 10:00:39-0700) Java version: 1.6.0_16 Java home: C:\Java\jdk1.6.0_16\jre Default locale: en_US, platform encoding: Cp1252 OS name: "windows 7" version: "6.1" arch: "amd64" Family: "windows" [INFO] Error stacktraces are turned on. [ERROR] Error executing Maven.

org.codehaus.plexus.component.repository.exception.ComponentLookupException: Component descriptor cannot be found in the component repository role: org.apache.maven.Maven roleHint: classRealm: none specified at org.codehaus.plexus.DefaultComponentRegistry.getComponentManager(DefaultComponentRegistry.java:435) at org.codehaus.plexus.DefaultComponentRegistry.getComponent(DefaultComponentRegistry.java:353) at org.codehaus.plexus.DefaultComponentRegistry.lookup(DefaultComponentRegistry.java:178) at org.codehaus.plexus.DefaultPlexusContainer.lookup(DefaultPlexusContainer.java:383) at org.apache.maven.cli.MavenCli.container(MavenCli.java:363) at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:156) at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:131)

I am using maven 3, beta 1, and using the following method call:

int x = MavenCli.doMain(params.toArray(new String[] {}), null);

where params contains things like "compile", etc.

Any ideas? Thanks.

A: 

I got this working by passing passing a classloader (this.getClass().getClassLoader()) to a DefaultContainerConfiguration and passing my DefaultContainerConfiguration to a DefaultPlexusContainer. Using the plexus, I was able to load instantiate a MavenExecutionRequestPopulator and a Maven object.

Then when I try to execute any goals, I create a MavenExecutionRequest and set the Pom file, system and user properties. The system properties are obtained from the System and have my -Dfoo=x properties, etc.

After that, I use a method I shamelessly stole (cant remember from where), but adapted a little ...

public MavenExecutionResult execute(MavenExecutionRequest request) {
        if (request == null) {
            return null;
        }
        MavenExecutionResult result;
        try {
            populator.populateDefaults(request);
            result = maven.execute(request);
        } catch (MavenExecutionRequestPopulationException ex) {
            ex.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

Let me know if any further details are required. I would post all the code, but it is spread over numerous files. Good luck.

ekawas