简体   繁体   English

有没有更好的方法在 netbeans 平台应用程序中获取项目类型?

[英]Is there any better way to get project type within netbeans platform application?

I am trying to create netbeans module which whould work upon standard maven java projects.我正在尝试创建 netbeans 模块,该模块适用于标准 maven java 项目。 Is there some better way to do that then how it was adviced in: How can I get a project type on Netbeans Platform?有没有更好的方法来做到这一点,然后是如何建议的:如何在 Netbeans 平台上获得项目类型? This seems to be pretty dependent on implmenetation (note the *Impl postfix of the implementing class found in lookup).这似乎非常依赖于实现(注意在查找中找到的实现 class 的 *Impl 后缀)。 I couldn't find any standard method in the Project API.我在 API 项目中找不到任何标准方法。 Any ideas?有任何想法吗? Or is it safe to rely on some "NbMavenProjectImpl" string?或者依赖一些“NbMavenProjectImpl”字符串是否安全?

Currently I am going this way:目前我正在这样做:

Project mainProject = OpenProjects.getDefault().getMainProject();

if (mainProject == null) {
    return;
}
String projectType = mainProject.getClass().getName();
if (projectType.equals("NbMavenProjectImpl")) {
     // do some action with the project here
}

One approach would be一种方法是

if (mainProject.getLookup().lookup(NbMavenProject.class) != null) {
  // do stuff
}

Another one, which should be preferred, is to register your business logic into the maven project lookup eg.另一个应该首选的方法是将您的业务逻辑注册到 maven 项目查找中,例如。 using an annotation使用注释

@ProjectServiceProvider(service=MyService.class, projectType="org-netbeans-modules-maven")
public class MyServiceImpl implements MyService {
  @Override
  public void doit() {
  }
}

and then accessing the business logic like然后访问业务逻辑

MyService ms = mainProject.getLookup().lookup(MyService.class);
if (ms != null) {
  ms.doit()
}

That way you can decouple your API from implementation and as a bonus you can share the same business logic between more project types, if appropriate.这样,您可以将 API 与实现分离,并且如果合适,您可以在更多项目类型之间共享相同的业务逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM