How to programmatically add a maven project to a studio project

I want to add maven nature to my existing studio project through java code. In eclipse, we can do this with the right click-> configure-> convert To Maven option. How can I call it through java code? My scenario is that I added the right click-> menu-> generate POM parameter for the project, and by clicking this I will generate the POM file for the project, and then I want to add the maven character to it with the same click. Can I call the default eclipse code to convert to maven from my java code?

+6
source share
1 answer

I understood the solution. We can add the maven nature to the project as follows - I have an eclipse project.

import org.eclipse.core.resources.ICommand; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IProjectDescription; import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.ui.IWorkbenchWindow; private void addMavenNature( IProject project){ IProjectDescription desc = project.getDescription(); String[] prevNatures = desc.getNatureIds(); //it takes all previous natures of project ie studioNature,javanature String[] newNatures = new String[prevNatures.length + 1]; System.arraycopy(prevNatures, 0, newNatures, 0, prevNatures.length); newNatures[prevNatures.length] = "org.eclipse.m2e.core.maven2Nature"; //add maven nature to the project desc.setNatureIds(newNatures); project.setDescription(desc, new NullProgressMonitor()); ICommand[] commands = desc.getBuildSpec(); List<ICommand> commandList = Arrays.asList( commands ); ICommand build = new BuildCommand(); build.setBuilderName("org.eclipse.m2e.core.maven2Builder"); //add maven builder to the project List<ICommand> modList = new ArrayList<>( commandList ); modList.add( build ); desc.setBuildSpec( modList.toArray(new ICommand[]{})); } 
+7
source

Source: https://habr.com/ru/post/1013027/


All Articles