zoukankan      html  css  js  c++  java
  • Jenkins插件开发(5)—— 向Jenkins注册自定义扩展点

    WIKI文档参照:https://wiki.jenkins-ci.org/display/JENKINS/Defining+a+new+extension+point

    片段如下:

    Implementing extension points
    Implementing an extension point defined in a plugin is no different from implementing an extension point defined in the core. See hudson.Extension for more details.
    
    @Extension
    public class Lion extends Animal { ... }

    “@Extension”的说明参考:http://javadoc.jenkins-ci.org/?hudson/Extension.html

    片段如下:

    hudson 
    Annotation Type Extension
    
    @Retention(value=RUNTIME)
    @Target(value={TYPE,FIELD,METHOD})
    @Documented
    public @interface Extension
    Marks a field, a method, or a class for automatic discovery, so that Hudson can locate implementations of ExtensionPoints automatically.
    
    (In contrast, in earlier Hudson, the registration was manual.)
    
    In a simplest case, put this on your class, and Hudson will create an instance of it and register it to the appropriate ExtensionList.
    (在Class上添加@Extension注解,是最简单的向Jenkins注册扩展点实现的方式。) If you'd like Hudson to call a factory method instead of a constructor, put this annotation on your static factory method. Hudson will invoke it and if the method returns a non-null instance, it'll be registered. The return type of the method is used to determine which ExtensionList will get the instance. Finally, you can put this annotation on a static field if the field contains a reference to an instance that you'd like to register. This is the default way of having your implementations auto-registered to Hudson, but Hudson also supports arbitrary DI containers for hosting your implementations. See ExtensionFinder for more details.

    验证扩展点是否注册成功:

    示例1: ExtensionList<ItemListener> extList = Jenkins.getInstance()
                    .getExtensionList(ItemListener.class); 
    示例2: ExtensionList<ItemListener> extList = Jenkins.getInstance()
                    .getExtensionList(SaveableListener.class); 

    以ItemListerner为例进行了测试:

    创建一个helloworld插件框架,在HelloWorldBuilder.java中添加如下内部类:

        @Extension
        public static final class MyProjectRenameListener extends ItemListener {
            @Override
            public void onRenamed(Item item, String oldName, String newName) {
                super.onRenamed(item, oldName, newName);
            }
        }

    在perform方法中检测扩展点是否已经被注册了:

        @Override
        public boolean perform(AbstractBuild build, Launcher launcher,
                BuildListener listener) {
            // This is where you 'build' the project.
            // Since this is a dummy, we just say 'hello world' and call that a
            // build.
    
            ExtensionList<ItemListener> extList = Jenkins.getInstance().getExtensionList(ItemListener.class);
    
            // This also shows how you can consult the global configuration of the
            // builder
            if (getDescriptor().getUseFrench())
                listener.getLogger().println("Bonjour, " + name + "!");
            else
                listener.getLogger().println("Hello, " + name + "!");
            return true;
        }

    Debug之后,extList的值为:

    [com.yihaodian.plugin.jenkins.jobsync.HelloWorldBuilder$MyProjectRenameListener@84c1c3, hudson.diagnosis.OldDataMonitor$2@1cd000a, hudson.model.DisplayNameListener@1bdb13f, hudson.model.Fingerprint$ProjectRenameListener@bfb588, hudson.tasks.BuildTrigger$DescriptorImpl$ItemListenerImpl@270dbf, org.jenkinsci.main.modules.sshd.ItemListenerImpl@1f79b9a]

    PS:后面的几章节将会记录我将要开发的一个Jenkins插件的整个开发过程。

  • 相关阅读:
    20200601:百万级int数据量的一个array求和。
    20200602:千万级数据量的list找一个数据。
    20200531:假如Redis里面有1亿个key,其中有10w个key是以某个固定的已知的前缀开头的,如何将它们全部找出来?
    20200530:主从数据库不一致如何解决?
    [USACO06DEC]Milk Patterns G
    [HAOI2016]找相同字符
    [AHOI2013]差异
    [SCOI2012]喵星球上的点名
    [APIO2014]回文串
    [TJOI2015]弦论
  • 原文地址:https://www.cnblogs.com/zhangqingsh/p/3029600.html
Copyright © 2011-2022 走看看