jenkins中jelly文件的使用

来源:互联网 发布:pscs3软件百度云 编辑:程序博客网 时间:2024/05/18 02:08

jelly文件为jenkins插件提供图形界面,后台的处理函数由对应的java代码进行处理。例如要在系统设置中为自己的插件添加参数设置等选项,就需要在~/jenkins-plugin/src/main/java/目录录下添加自己的java类来处理这个事情,例如我添加的类为Tools,则我在对应的~/jenkins-plugin/src/main/resources/Tools/下添加对应的jelly文件,名为global.jelly。这个jelly放在什么名字的文件夹下就会去找什么名字对应的类中的处理函数。

在pom.xml文件中,下面的代码定义了插件构建所依赖的jenkins版本。

 <parent>    <groupId>org.jenkins-ci.plugins</groupId>    <artifactId>plugin</artifactId>    <version>2.11</version>    <relativePath/>  </parent>

在对应的java文件中,我们可以通过拓展一个jenkins本身的拓展点来完成插件的设计。每个可以被拓展的类都实现了hudson.ExtensionPoint。可以通过类似@Extension@DataBoundConstructor这样的内嵌标签来告诉jenkins做一些操作。描述符(Descriptor)定义了jenkins是如何创建这个插件的实例和传递一些配置参数的。JavaDoc中对其的定义如下。

hudson.model.DescriptorMetadata about a configurable instance. Descriptor is an object that has metadata about a Describable object, and also serves as a factory (in a way this relationship is similar to Object/Class relationship. A Descriptor/Describable combination is used throughout in Hudson to implement a configuration/extensibility mechanism. Take the list view support as an example, which is implemented in ListView class. Whenever a new view is created, a new ListView instance is created with the configuration information. This instance gets serialized to XML, and this instance will be called to render the view page. This is the job of Describable — each instance represents a specific configuration of a view (what projects are in it, regular expression, etc.) For Hudson to create such configured ListView instance, Hudson needs another object that captures the metadata of ListView, and that is what a Descriptor is for. ListView class has a singleton descriptor, and this descriptor helps render the configuration form, remember system-wide configuration, and works as a factory. Descriptor also usually have its associated views. 

在描述符中,调用load()装载持久化保存在本地的配置数据,在configure函数中调用save()来持久化保存配置参数到本地。由于jenkins的机制问题,所以描述符只能内嵌声明在主要的插件类中。

jenkins插件中使用了一种机制,只要我们给描述符中的方法命名为一个特殊的的符合约定的名字,jenkins就会自动去调用它。如,load()调用过程中会把本地持久化的数据再存储到描述符的字段中,然后使用调用getXxx方法,这样就把字段中的值存储到xxx中了。

在系统设置界面,只要用户点击保存或者应用,就会调用描述符中定义的configure方法。我们可以在这个函数中读取用户输入的配置信息,进行处理,最后调用save方法保存这些参数。

我觉得学习jelly的编写和其与java的交互代码,更快的是阅读Github上的各种开源项目,可以寻找一个和你实现功能类似的,然后修改并测试测试其中的源码,总结各种变化的规律。也可以参考我最近写的一个jenkins插件hyper common plugin,这个插件实现了hyper credentials的存储,hypercli的下载和与其账户的连通性测试。

1 0
原创粉丝点击