【问题标题】:Is there any better way to get project type within netbeans platform application?有没有更好的方法在 netbeans 平台应用程序中获取项目类型?
【发布时间】:2011-04-29 11:27:01
【问题描述】:

我正在尝试创建适用于标准 maven java 项目的 netbeans 模块。有没有更好的方法来做到这一点,然后是如何建议的:How can I get a project type on Netbeans Platform? 这似乎非常依赖于实现(注意在查找中找到的实现类的 *Impl 后缀)。我在 Project API 中找不到任何标准方法。有任何想法吗?或者依赖一些“NbMavenProjectImpl”字符串是否安全?

目前我正在走这条路:

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
}

【问题讨论】:

    标签: java netbeans netbeans-platform


    【解决方案1】:

    一种方法是

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

    另一个应该首选的方法是将您的业务逻辑注册到 maven 项目查找中,例如。使用注释

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

    然后像这样访问业务逻辑

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

    通过这种方式,您可以将 API 与实现分离,并且如果合适的话,您还可以在更多项目类型之间共享相同的业务逻辑。

    【讨论】:

    • 最后我不得不使用我上面提到的解决方案(这是为了一些迫不及待的工作)。但是感谢您的解决方案,因为它们非常干净简洁!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-13
    • 2019-03-20
    • 1970-01-01
    • 2011-02-10
    • 1970-01-01
    • 2023-02-23
    相关资源
    最近更新 更多