我想通过 java 代码将 maven 性质添加到我现有的工作室项目中。 在 eclipse 中,我们可以通过右键单击->配置->转换为 Maven 选项来实现。 我怎样才能通过java代码调用它? 我的场景是,我为项目添加了右键单击->菜单->生成 POM 选项,单击此选项我将为项目生成 POM 文件,然后我想在其中添加 Maven 特性同样的点击。 我可以调用 eclipses 默认代码以从我的 java 代码转换为 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[]{}));
}
关于java - 如何以编程方式将 Maven 特性添加到工作室项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41057745/