Skip to main content

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

My end goal is to be able to do the equivalent as "Run As->Maven Install", but to do this programmatically. I have got this to work:
public static void runMaven(IFile pomFile, List<String> goals, IProgressMonitor monitor) {

       

        InvocationRequest request = new DefaultInvocationRequest();
        try {
            File file = new File(pomFile.getLocationURI());
            request.setPomFile(file);
            request.setBaseDirectory(new File(pomFile.getProject().getLocationURI()));
            request.setGoals(goals);
            request.setShowErrors(true);

            

            Invoker invoker = new DefaultInvoker();
            invoker.setMavenHome(new File("/Users/danijoh2/maven3")); // This will not do!
            InvocationResult result = invoker.execute(request);
            if ( result.getExitCode() != 0 ){
                if ( result.getExecutionException() != null ) { //Exception }
                else //Exception }
            }
        } catch (MavenInvocationException e) { //Exception }
}

But the obvious problem here is that I have to specify a remote version of maven to use, I could use this if anyone can tell me if it is possible to query m2eclipse for the location of its embedded maven executable to use it instead of having to ask the user to download maven and point me to the installation.

Thanks,
Daniel

On Jan 25, 2011, at 6:53 PM, Igor Fedorenko wrote:

What are you trying to achieve?

m2eclipse-tycho configurators are not expected to do anything during
maven build... actually, project configurators in general are not
expected to do anything during Maven build.

m2eclipse-tycho configurators are only expected to setup PDE natures and
classpath but everything else is delegated to PDE.

--
Regards,
Igor

On 11-01-25 08:30 PM, Daniel Johnson wrote:
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



_______________________________________________
m2e-users mailing list
m2e-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/m2e-users
_______________________________________________
m2e-users mailing list
m2e-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/m2e-users


Back to the top