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());
            request.setGoals(goals);
            MavenPlugin.getDefault().getConsole().showConsole();
            MavenExecutionResult result = maven.execute(request, new SubProgressMonitor(monitor, 1));
            if ( result.hasExceptions()){
             // Throw CoreException
            }

            

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

            return summary;
        } 
        finally {
            monitor.done();
        }
}

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:

<profiles>
    <profile>
      <id>m2e</id>
      <activation>
        <property>
          <name>m2e.version</name>
        </property>
      </activation>
      <build>
        <plugins>
          <plugin>
            <groupId>org.maven.ide.eclipse</groupId>
            <artifactId>lifecycle-mapping</artifactId>
            <version>0.10.0</version>
            <configuration>
              <mappingId>customizable</mappingId>
              <configurators>
                <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" />
              </configurators>
              <mojoExecutions>
                <mojoExecution>org.apache.maven.plugins:maven-resources-plugin::</mojoExecution>
                <mojoExecution>org.apache.maven.plugins:maven-plugin-plugin::descriptor</mojoExecution>
              </mojoExecutions>
            </configuration>
          </plugin>

          <plugin>
            <groupId>org.sonatype.tycho</groupId>
            <artifactId>tycho-maven-plugin</artifactId>
            <version>0.7.0</version>
            <extensions>true</extensions>
          </plugin>
        </plugins>
      </build>
    </profile>
</profiles>

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();
MavenPlugin.getDefault().getConsole().showConsole();
File file = new File(pomFile.getLocationURI());
File projectDirectory = new File(pomFile.getProject().getLocationURI());
MavenExecutionRequest request;
request = maven.createExecutionRequest(monitor);
request.setBaseDirectory( projectDirectory );
request.setPom(file);
request.setGoals(goals);
request.setShowErrors(true);

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


Thanks in advance,
Daniel

Back to the top