Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[m2e-users] Programmatically Build Maven Project

Hello m2e users,

I have been trying to build a maven project that needs to have tycho configurators executed programmatically but with not much luck. Usually I can use this code to build a maven pom.xml file:

public static BuildSummary runMaven(IFile pomFile, List<String> goals, IProgressMonitor monitor) throws CoreException{

        if (monitor == null){
            monitor = new NullProgressMonitor();
        try {
            monitor.beginTask("Building: " + pomFile.getName(), 3);
            MavenProjectManager projectManager = MavenPlugin.getDefault().getMavenProjectManager();
            IMaven maven = MavenPlugin.getDefault().getMaven();
            IMavenProjectFacade facade = projectManager.create(pomFile, true, new SubProgressMonitor(monitor, 1));
            ResolverConfiguration config = facade.getResolverConfiguration();
            MavenExecutionRequest request = projectManager.createExecutionRequest(pomFile, config, new SubProgressMonitor(monitor, 1));
            request.getUserProperties().setProperty("m2e.version", MavenPlugin.getVersion());
            MavenExecutionResult result = maven.execute(request, new SubProgressMonitor(monitor, 1));
            if ( result.hasExceptions()){
             // Throw CoreException


            BuildSummary summary = result.getBuildSummary(result.getProject());

            return summary;
        finally {

But when I try this on a project that needs tycho conifurations ran I found this doesn't work. I found that the m2eclipse auto builder was also failing on my project in the same way and after doing some googling I realized I need this in my project pom for the m2eclipse auto builder to work correctly:

                <configurator id="org.maven.ide.eclipse.jdt.javaConfigurator" />
                <configurator id="org.maven.ide.eclipse.modello.modelloConfigurator" />
                <configurator id="org.maven.ide.eclipse.plexus.annotations.plexusConfigurator" />
                <configurator id="org.maven.ide.eclipse.mavenarchiver.pomProperties" />
                <configurator id="maven-bundle-plugin" />


However, the function I have still does not work. I even tried the following two lines to be sure that the m2e profile would be picked up.

ResolverConfiguration config = facade.getResolverConfiguration();
config.setActiveProfiles("m2e"); // Added this, but when execute runs it says it can't be activated because it does not exist
MavenExecutionRequest request = projectManager.createExecutionRequest(pomFile, config, new SubProgressMonitor(monitor, 1));
request.getUserProperties().setProperty("m2e.version", MavenPlugin.getVersion()); // Added this in hopes of activating the m2e profile in the pom

But this doesn't seem to help either. Is there a better way to do this? Can anyone point me to the m2eclipse auto builder code so I can see what it is doing differently?
m2e version = 0.10.2, tycho=0.4.3

I have also tried this code with the same results:

IMaven maven = MavenPlugin.getDefault().getMaven();
File file = new File(pomFile.getLocationURI());
File projectDirectory = new File(pomFile.getProject().getLocationURI());
MavenExecutionRequest request;
request = maven.createExecutionRequest(monitor);
request.setBaseDirectory( projectDirectory );

MavenExecutionResult result = maven.execute(request, monitor);

Thanks in advance,

Back to the top