Dis*_*sha 6 java eclipse eclipse-plugin maven
我想通过java代码将maven性质添加到我现有的工作室项目中.在eclipse中,我们可以通过右键单击 - > configure-> convert to Maven选项来实现.我如何通过java代码调用它?我的方案是,我已经为项目添加了 右键单击- >菜单 - >生成POM选项,点击此项我将为项目生成POM文件,然后我想在同一个单击中添加maven性质.我可以从我的java代码中调用eclipses默认代码转换为maven吗?
我找到了解决方案.我们可以将maven性质添加到项目中,如下所示 - 我有一个eclipse项目.
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 i.e 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[]{}));
}
Run Code Online (Sandbox Code Playgroud)