java - 如何以编程方式将 Maven 特性添加到工作室项目

标签 java eclipse maven eclipse-plugin

我想通过 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/

相关文章:

Java算法查询

eclipse - 在Eclipse中使用Subversive同时处理多个存储库

java - 如何从 spring-boot-starter-parent 中排除特定的依赖项

java - 如何将 apache solr 与 eclipse 和 Apache tomcat 集成

java - Intellij IDEA SLF4J:无法加载类“org.slf4j.impl.StaticLoggerBinder”

java - 带有配置文件的 Spring Boot Maven 多模块项目 - 未找到其他模块的包

Java事务管理器

java - 在 Spring MVC Controller 中反序列化 json 数组

java - Spring websocket 与rabbitmq - 在订阅级别添加安全性

java - Eclipse 和 Java 3D 错误空图形配置