Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [m2e-users] Programmatically run maven goal

Hi,
By using executionContext/executionRequest API, you perform a so-called workspace build of the project, which m2e itself uses to execute various mojos/phases.
In this mode some (interesting) mojo executions are either replaced by a configurator - a special plugin that does its own stuff - or are run as-is (very rarely though) and many others (unknown/uninteresting) are simply ignored, which includes everything starting from package phase and beyond. [1]

What you really want is to create a new 'private' m2e launch configuration (private ones do no register themselves in launch config ui) and execute it. This way maven embedder is launched in a basically vanilla environment.
I've been using a pretty crude version this approach for some time now: [2]

[1] https://www.eclipse.org/m2e/documentation/m2e-execution-not-covered.html
[2] https://gist.github.com/atanasenko/ddcc08403488835fdcc7


2015-09-20 23:41 GMT+03:00 Marek Jagielski <marek.jagielski@xxxxxxxxx>:
I use:
eclipse mars where m2e has version 1.6.0
Linux Mint 17 - however solution should be multiplatform

2015-09-20 22:17 GMT+02:00 Marek Jagielski <marek.jagielski@xxxxxxxxx>:
Hi,

I am developing pde plugin where user projects are maven nature projects. My plugin requires m2e to handle project configuration from user perspective.

In some occasion I needed to run "package" goal programmatically. I was doing like this as below:

        String path = project.getLocation().toOSString();
        MavenCli cli = new MavenCli();
        int result = cli.doMain(new String[] { "package", "-T 4C", "-DskipTests" }, path, null, null);
        return result == 0 ? true : false;

However, I was using separate bundle:
      <dependency>
         <groupId>org.apache.maven</groupId>
         <artifactId>maven-embedder</artifactId> 
         <version>3.2.3</version>
      </dependency>
that was integrated using maven-bundle-plugin.

Doing further development I started using m2e and reached compatibility issues. I decided to use only m2e api.

With the same code, but using m2e I got exception:

-Dmaven.multiModuleProjectDirectory system propery is not set. Check $M2_HOME environment variable and mvn script match

So I started to look into m2e api. I tried code like this:

        IMavenProjectRegistry projectManager = MavenPlugin.getMavenProjectRegistry();
        IFile pomFile = project.getFile(new Path("pom.xml"));
        IMavenProjectFacade projectFacade = projectManager.create(pomFile, true, new NullProgressMonitor());
        MavenProject mp = projectFacade.getMavenProject();

        IMavenExecutionContext ec = MavenPlugin.getMaven().createExecutionContext();
        ec.getExecutionRequest().getGoals().add("package");
        ec.getExecutionRequest().setBaseDirectory(mp.getBasedir());
        ec.getExecutionRequest().setPom(mp.getFile());
        ec.getExecutionRequest().setBuilderId("multithreaded");
        ec.getExecutionRequest().setDegreeOfConcurrency(4);
        ec.execute(projectFacade.getMavenProject(),new ICallable<Boolean>() {
            @Override
            public Boolean call(IMavenExecutionContext mec, IProgressMonitor pm) throws CoreException {
                return true;
            }
        }, new NullProgressMonitor());

without errors, however without expected results. In console I got:

21:40:43.440 [Worker-37] DEBUG o.e.m.c.i.builder.MavenBuilder - Building project test1
21:40:43.443 [Worker-37] DEBUG o.e.a.i.i.DefaultLocalRepositoryProvider - Using manager EnhancedLocalRepositoryManager with priority 10.0 for /home/marek/.m2/repository
21:40:43.444 [Worker-37] DEBUG o.e.m.c.p.c.AbstractCustomizableLifecycleMapping - Build participants for MavenProject: org.examples:example:1.0.0-SNAPSHOT @ /home/marek/Projects/systemInCloudModeler/runtime-EclipseApplication/test1/pom.xml
21:40:43.444 [Worker-37] DEBUG o.e.m.c.p.c.AbstractCustomizableLifecycleMapping - Mojo execution key: org.apache.maven.plugins:maven-resources-plugin:2.6:resources (execution: default-resources, phase: process-resources)
21:40:43.444 [Worker-37] DEBUG o.e.m.c.p.c.AbstractCustomizableLifecycleMapping - Action: execute
21:40:43.444 [Worker-37] DEBUG o.e.m.c.p.c.AbstractCustomizableLifecycleMapping - Mojo execution key: org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (execution: default-compile, phase: compile)
21:40:43.444 [Worker-37] DEBUG o.e.m.c.p.c.AbstractCustomizableLifecycleMapping - Mojo execution key: org.apache.maven.plugins:maven-resources-plugin:2.6:testResources (execution: default-testResources, phase: process-test-resources)
21:40:43.445 [Worker-37] DEBUG o.e.m.c.p.c.AbstractCustomizableLifecycleMapping - Action: execute
21:40:43.445 [Worker-37] DEBUG o.e.m.c.p.c.AbstractCustomizableLifecycleMapping - Mojo execution key: org.apache.maven.plugins:maven-compiler-plugin:2.3.2:testCompile (execution: default-testCompile, phase: test-compile)
21:40:43.445 [Worker-37] DEBUG o.e.m.c.p.c.AbstractCustomizableLifecycleMapping - Mojo execution key: org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test (execution: default-test, phase: test)
21:40:43.445 [Worker-37] DEBUG o.e.m.c.p.c.AbstractCustomizableLifecycleMapping - Mojo execution key: org.apache.maven.plugins:maven-jar-plugin:2.4:jar (execution: default-jar, phase: package)
21:40:43.445 [Worker-37] DEBUG o.e.m.c.p.c.AbstractCustomizableLifecycleMapping - Action: configurator
21:40:43.445 [Worker-37] DEBUG o.e.m.c.p.c.AbstractCustomizableLifecycleMapping - Project configurator : id=org.sonatype.m2e.mavenarchiver.jararchiverconfigurator class=org.sonatype.m2e.mavenarchiver.internal.JarArchiverConfigurator
21:40:43.446 [Worker-37] DEBUG o.e.m.c.p.c.AbstractCustomizableLifecycleMapping - Build participant: org.sonatype.m2e.mavenarchiver.internal.AbstractMavenArchiverConfigurator$1
21:40:43.446 [Worker-37] DEBUG o.e.m.c.p.c.AbstractCustomizableLifecycleMapping - Mojo execution key: org.apache.maven.plugins:maven-install-plugin:2.4:install (execution: default-install, phase: install)
21:40:43.446 [Worker-37] DEBUG o.e.m.c.p.c.AbstractCustomizableLifecycleMapping - Mojo execution key: org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy (execution: default-deploy, phase: deploy)
21:40:43.446 [Worker-37] DEBUG o.e.m.c.i.builder.MavenBuilderImpl - Executing build participant org.eclipse.m2e.core.project.configurator.MojoExecutionBuildParticipant for plugin execution org.apache.maven.plugins:maven-resources-plugin:2.6:resources (execution: default-resources, phase: process-resources)
21:40:43.451 [Worker-37] INFO  o.e.m.c.i.embedder.EclipseLogger - Using 'UTF-8' encoding to copy filtered resources.
21:40:43.452 [Worker-37] INFO  o.e.m.c.i.embedder.EclipseLogger - Copying 0 resource
21:40:43.453 [Worker-37] DEBUG o.e.m.c.i.builder.MavenBuilderImpl - Finished executing build participant org.eclipse.m2e.core.project.configurator.MojoExecutionBuildParticipant for plugin execution org.apache.maven.plugins:maven-resources-plugin:2.6:resources (execution: default-resources, phase: process-resources) in 7 ms
21:40:43.453 [Worker-37] DEBUG o.e.m.c.i.builder.MavenBuilderImpl - Executing build participant org.eclipse.m2e.core.project.configurator.MojoExecutionBuildParticipant for plugin execution org.apache.maven.plugins:maven-resources-plugin:2.6:testResources (execution: default-testResources, phase: process-test-resources)
21:40:43.456 [Worker-37] INFO  o.e.m.c.i.embedder.EclipseLogger - Using 'UTF-8' encoding to copy filtered resources.
21:40:43.457 [Worker-37] INFO  o.e.m.c.i.embedder.EclipseLogger - Copying 0 resource
21:40:43.457 [Worker-37] DEBUG o.e.m.c.i.builder.MavenBuilderImpl - Finished executing build participant org.eclipse.m2e.core.project.configurator.MojoExecutionBuildParticipant for plugin execution org.apache.maven.plugins:maven-resources-plugin:2.6:testResources (execution: default-testResources, phase: process-test-resources) in 4 ms
21:40:43.457 [Worker-37] DEBUG o.e.m.c.i.builder.MavenBuilderImpl - Executing build participant org.sonatype.m2e.mavenarchiver.internal.AbstractMavenArchiverConfigurator$1 for plugin execution org.apache.maven.plugins:maven-jar-plugin:2.4:jar (execution: default-jar, phase: package)
21:40:43.457 [Worker-37] DEBUG o.e.m.c.i.builder.MavenBuilderImpl - Finished executing build participant org.sonatype.m2e.mavenarchiver.internal.AbstractMavenArchiverConfigurator$1 for plugin execution org.apache.maven.plugins:maven-jar-plugin:2.4:jar (execution: default-jar, phase: package) in 0 ms
21:40:43.458 [Worker-37] DEBUG o.e.m.c.i.builder.MavenBuilder - Built project test1 in 18 ms


I would be appreciated for any help with explaining how to use m2e api.
Thanks in advance,

Marek

--
Marek Jagielski
+48 513 402 596



--
Marek Jagielski
+48 513 402 596

_______________________________________________
m2e-users mailing list
m2e-users@xxxxxxxxxxx
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/m2e-users



--
Regards,
Anton.

Back to the top