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