views:

176

answers:

0

I want to compile a project using the MavenEmbedder. Thus, I use this code:

    Configuration config = new DefaultConfiguration();
    config.setUserSettingsFile(new File("..."));
    config.setClassLoader(Thread.currentThread().getContextClassLoader());
    ConfigurationValidationResult validationResult = MavenEmbedder.validateConfiguration(config);
    if (validationResult.isValid()) {
        try {
            MavenEmbedder embedder = new MavenEmbedder(config);
            MavenExecutionRequest request = new DefaultMavenExecutionRequest();
            request.setBaseDirectory("...");
            request.setGoals(Arrays.asList(new String[] { "clean", "install" }));
            MavenExecutionResult result = embedder.execute(request);
            if (result.hasExceptions()) {
                List<Exception> exceptions = result.getExceptions();
                for (Exception e : exceptions) {
                    e.printStackTrace();
                }
            }
        } catch (MavenEmbedderException e) {
            e.printStackTrace();
        }
    }

When I run this code, I get this log, which show me an error:

[INFO] Scanning for projects...
[INFO] Reactor build order: 
[INFO]   MyApp
[INFO]   MyApp Commons
[INFO]   MyApp Client
[INFO]   MyApp Server
[INFO] ------------------------------------------------------------------------
[INFO] Building MyApp
[INFO] 
[INFO] Id: myapp:MyApp:pom:1.0
[INFO] task-segment: [clean, install]
[INFO] ------------------------------------------------------------------------
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to construct build plan for: MyApp
Id: myApp:MyApp:pom:1.0
task-segment: [clean, install]. Reason: Failed to resolve plugin for mojo binding: org.apache.maven.plugins:maven-install-plugin:2.2:install
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.getLifecycleBindings(DefaultLifecycleExecutor.java:408)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegmentForProject(DefaultLifecycleExecutor.java:233)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegments(DefaultLifecycleExecutor.java:201)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:164)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:207)
    at org.apache.maven.embedder.MavenEmbedder.execute(MavenEmbedder.java:846)
    at myapp.build.MavenBuilder.runMvnCommand(MavenBuilder.java:60)
    at myapp.build.ProjectBuilder.go(ProjectBuilder.java:62)
    at myapp.build.ProjectBuilder.doInBackground(ProjectBuilder.java:77)
    at myapp.build.ProjectBuilder.doInBackground(ProjectBuilder.java:1)
    at javax.swing.SwingWorker$1.call(SwingWorker.java:278)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at javax.swing.SwingWorker.run(SwingWorker.java:317)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:619)
Caused by: org.apache.maven.lifecycle.plan.LifecyclePlannerException: Failed to resolve plugin for mojo binding: org.apache.maven.plugins:maven-install-plugin:2.2:install
    at org.apache.maven.lifecycle.plan.DefaultBuildPlanner.loadPluginDescriptor(DefaultBuildPlanner.java:350)
    at org.apache.maven.lifecycle.plan.DefaultBuildPlanner.findForkModifiers(DefaultBuildPlanner.java:197)
    at org.apache.maven.lifecycle.plan.DefaultBuildPlanner.addForkedLifecycleModifiers(DefaultBuildPlanner.java:184)
    at org.apache.maven.lifecycle.plan.DefaultBuildPlanner.constructBuildPlan(DefaultBuildPlanner.java:122)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.getLifecycleBindings(DefaultLifecycleExecutor.java:394)
    ... 16 more
Caused by: org.apache.maven.plugin.loader.PluginLoaderException: Failed to load plugin. Reason: The PluginDescriptor for the plugin org.apache.maven.plugins:maven-install-plugin was not found. Should have been in realm: ClassRealm[/plugins/org.apache.maven.plugins:maven-install-plugin:2.2@48/thread:33, parent: null]
    at org.apache.maven.plugin.loader.DefaultPluginLoader.loadPlugin(DefaultPluginLoader.java:111)
    at org.apache.maven.plugin.loader.DefaultPluginLoader.loadPlugin(DefaultPluginLoader.java:54)
    at org.apache.maven.lifecycle.plan.DefaultBuildPlanner.loadPluginDescriptor(DefaultBuildPlanner.java:327)
    ... 20 more
Caused by: org.apache.maven.plugin.PluginManagerException: The PluginDescriptor for the plugin org.apache.maven.plugins:maven-install-plugin was not found. Should have been in realm: ClassRealm[/plugins/org.apache.maven.plugins:maven-install-plugin:2.2@48/thread:33, parent: null]
    at org.apache.maven.plugin.DefaultPluginManager.addPlugin(DefaultPluginManager.java:355)
    at org.apache.maven.plugin.DefaultPluginManager.verifyVersionedPlugin(DefaultPluginManager.java:224)
    at org.apache.maven.plugin.DefaultPluginManager.verifyPlugin(DefaultPluginManager.java:199)
    at org.apache.maven.plugin.loader.DefaultPluginLoader.loadPlugin(DefaultPluginLoader.java:81)
    ... 22 more

I've looked to my local repository, and the plugin org.apache.maven.plugins:maven-install-plugin:2.2 is present.

What is the problem with my code? How to solve it?


Edit: If I simulate the mvn clean package command instead of the mvn clean install, i.e. having:

request.setGoals(Arrays.asList(new String[] { "clean", "package" }));

then the compilation works without problems...