Programmatically import a Maven project from your own Eclipse plugin

How to import Maven project from Eclipse plug-in.

The m2e plug-in is required to import the Maven project, so install it separately in Eclipse.

This method imports the folder containing pom.xml, so it does not create a new folder under the workspace folder.

import org.apache.maven.model.Model
import org.eclipse.m2e.core.MavenPlugin;
import org.eclipse.m2e.core.project.IProjectConfigurationManager;
import org.eclipse.m2e.core.project.MavenProjectInfo;
import org.eclipse.m2e.core.project.ProjectImportConfiguration;
import org.eclipse.core.runtime.NullProgressMonitor;

String groupId =Group ID
String artifactId =Artifact ID
String version =version
File pomXml = pom.xml file object

Model model = new Model();
model.setGroupId(groupId);
model.setArtifactId(artifactId);
model.setVersion(version);
model.setPomFile(pomXml);

MavenProjectInfo info = new MavenProjectInfo(artifactId, pomXml, model, null);

Collection<MavenProjectInfo> infoList = new ArrayList<>();
infoList.add(info);

IProjectConfigurationManager manager = MavenPlugin.getProjectConfigurationManager();
ProjectImportConfiguration config = new ProjectImportConfiguration();

try {
  manager.importProjects(infoList, config, new NullProgressMonitor());
} catch (CoreException e) {
  //Exception handling
}

The group ID and artifact ID are also written in pom.xml, so I have to set them in the Model object, so it seems a bit verbose, but I can do it for the time being.

Recommended Posts

Programmatically import a Maven project from your own Eclipse plugin
Create a base for your batch processing project in Eclipse.
Make your own Elasticsearch plugin
Create a Jetty project using Eclipse
Create a tomcat project using Eclipse
Create a Java project using Eclipse
How to make a Maven project
Create a Maven project with a command
Import nablarch-example project on github into Eclipse
Added slf4J + logback to Eclipse Maven project
Relationship between Eclipse m2e plugin and Maven
Deploy a Tomcat-based Eclipse project on Heroku