簡體   English   中英

如何以編程方式將maven性質添加到工作室項目中

[英]How to add maven nature to studio project programatically

我想通過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[]{}));
}  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM