Eclipse 插件片段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/673908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 13:35:45  来源:igfitidea点击:

Eclipse Plugin Fragment

javaeclipseplugins

提问by SteveT

Does anyone have a "hello world" sample or tutorial for creating an Eclipse plugin fragment?

有没有人有创建 Eclipse 插件片段的“hello world”示例或教程?

I have a working host plugin that, for the sake of simplicity, is just this...

我有一个可以工作的主机插件,为了简单起见,就是这个......

public void start(BundleContext context) throws Exception {
    System.out.println("Hello....");
    super.start(context);
    plugin = this;
}

public void stop(BundleContext context) throws Exception {
    System.out.println("Goodbye...");
    plugin = null;
    super.stop(context);
}

Simple enough and works. Now I want to add a fragment to that host, which seems not as simple as creating a plugin host. I just don't see how to create a fragment project and add logic to it. Let's say I just want to do something simple and have the fragment to print a "Hello2" at start()and "Goodbye2" at stop(). Can someone give me a working example?

足够简单并且有效。现在我想向该主机添加一个片段,这似乎不像创建插件主机那么简单。我只是不知道如何创建片段项目并向其添加逻辑。假设我只想做一些简单的事情,让片段在 打印“Hello2”start()和“Goodbye2” stop()。有人可以给我一个有效的例子吗?

回答by Michal Bernhard

  1. Eclipse-> File-> New...-> Fragment project-> set the host plugin (which is either in your workspace or in plugins in target platform).

  2. Open Plugin manifest editor(you can do it by clicking on build.properties, manifest.mfor fragment.xml- if there is no such a file, create it by hand)

  3. In tab Extentionsclick Add..and add org.eclipse.ui.startupand browse class which implements org.eclipse.ui.IStartupclass.

  4. Create this class and implement it. You need to implement method earlyStartup()which is entry point to the fragment.

  1. Eclipse-> File-> New...-> Fragment project-> 设置宿主插件(在你的工作区或目标平台的插件中)。

  2. 打开插件清单编辑器(您可以通过单击来完成build.propertiesmanifest.mf或者fragment.xml- 如果没有这样的文件,请手动创建)

  3. 在选项卡扩展中单击添加..并添加org.eclipse.ui.startup和浏览实现org.eclipse.ui.IStartup类的类。

  4. 创建这个类并实现它。您需要实现earlyStartup()作为片段入口点的方法。

Note: The lines below are just example. I didn't test it so there might be errors...

注意:以下几行只是示例。我没有测试它所以可能有错误......

All you need is this (this is project structure / directory structure):

所有你需要的是这个(这是项目结构/目录结构):

  • Fragment-Project- root dir
    • /META-INF
      • MANIFEST.MF
    • /src(which is source directory)
      • FragmentStartClass.java(which implement org.eclipse.ui.IStartup interface and earlyStartup method)
    • build.properties
    • fragment.xml
  • 片段项目- 根目录
    • /元信息
      • 清单文件
    • / src(这是源目录)
      • FragmentStartClass.java(实现org.eclipse.ui.IStartup接口和earlyStartup方法)
    • 构建属性
    • 片段.xml

META-INF/MANIFEST.MFcontent:

META-INF/MANIFEST.MF内容:

Manifest-Version: 1.0 
Bundle-ManifestVersion: 2 
Bundle-Name: FragmentProject 
Bundle-SymbolicName: FragmentProject;singleton:=true 
Bundle-Version: 1.0.0 
Bundle-ClassPath: src/,. 
Fragment-Host: *HostPluginProjectSymbolicName*;bundle-version="1.0.0" 
Bundle-RequiredExecutionEnvironment: J2SE-1.5 
Require-Bundle:  

build.propertiescontent:

build.properties内容:

source.. = src,\
output.. = bin/
bin.includes = META-INF/,
              .,
                  fragment.xml

fragment.xmlcontent:

fragment.xml内容:

<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.2"?>
<fragment>
   <extension
         point="org.eclipse.ui.startup">
      <startup
            class="FragmentStartClass">
      </startup>
   </extension>
</fragment>

FragmentStartClass.javacontent:

FragmentStartClass.java内容:

import org.eclipse.ui.IStartup;


public class FragmentStartClass implements IStartup {

    public void earlyStartup() {
       System.out.println("Hello World From Fragment!");

    }


}

回答by Nick Veys

Well, first off what are you trying to accomplish? Are you sure fragments are the solution?

那么,首先你想完成什么?你确定碎片是解决方案吗?

Fragments are simply additions to an existing plugin. There is no Bundle-Activator to "startup" the fragment. You just gain the additional resources in the fragment. You could use an extension point to inform the host that some particular functionality was extended by a present fragment. Think of it as merging the two at runtime.

片段只是对现有插件的添加。没有 Bundle-Activator 来“启动”片段。您只需获得片段中的额外资源。您可以使用扩展点来通知主机某些特定功能已由当前片段扩展。将其视为在运行时合并两者。

See this notefrom the Eclipse Wiki.

请参见本说明Eclipse的维基

回答by Mike Houston

I'm not sure if you can do this without the host plugin knowing something about loading the fragment's extra implementation code.

我不确定您是否可以在主机插件不知道有关加载片段的额外实现代码的情况下执行此操作。

For example:

例如:

MyInterface o = null;
try {
    Class<?> c = getClass().getClassLoader().loadClass("my.fragment.Activator");
    o = (MyInterface) c.newInstance();
} catch (ClassNotFoundException e) {
}

if (o != null) {
    o.start();
}

where MyInterface is an interface defined in the host plugin, and implemented by your fragment.

其中 MyInterface 是主机插件中定义的接口,并由您的片段实现。

Another way might be to have the fragment provide an extension point identifying the extended class, but that is probably more than is needed.

另一种方法可能是让片段提供一个标识扩展类的扩展点,但这可能比需要的更多。

回答by Mario Ortegón

I think what you are trying to do is better served by using extension pointsand separate plugins, as you want to add functionality to the base plugin.

我认为通过使用扩展点和单独的插件可以更好地为您提供服务,因为您想向基本插件添加功能。

Fragments are only used to add resources or extra classes to the base plugin.

片段仅用于向基本插件添加资源或额外的类。

回答by mgaert

For the record, I just created an Eclipse Fragment that contains some PMD Java Rule classes.

作为记录,我刚刚创建了一个包含一些 PMD Java Rule 类的 Eclipse 片段。

Next to the class files, there is only one filerequired in that Jar: META-INF/MANIFEST.MF:

在类文件旁边,该Jar 中需要一个文件META-INF/MANIFEST.MF::

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: com.acme.tools.pmd
Bundle-SymbolicName: com.acme.tools.pmd;singleton:=true
Bundle-Version: 1.0.0
Fragment-Host: net.sourceforge.pmd.eclipse.plugin;bundle-version="3.2.6"
Bundle-RequiredExecutionEnvironment: J2SE-1.5

I tried this with current Eclipse 3.7. Deployment is by placing the Jar file into <eclipse root>/dropins/plugins(create that folder if it's not yet there).

我用当前的 Eclipse 3.7 试过这个。部署是通过将 Jar 文件放入<eclipse root>/dropins/plugins(如果该文件夹尚不存在,则创建该文件夹)。