持续集成(CI)是将多个团队成员贡献的代码频繁持续的构建并给与反馈,而不必将寻找和修复缺陷的过程放在开发后期。在典型的持续集成周期中,代码首先被周期性的从版本控制服务器(如CVS或Subversion)中更新下来,随后调用自动化编译脚本(如Ant或Maven)编译,并运行所有测试用例,给出结果分析的报告。

java.net上的开源工具Hudson便是一款优秀的持续集成工具,目前的发展速度很快,并且在吸收了众多CI服务器的优点和长处。在自动构建工具支持方面,Hudson可以同Maven紧密集成,并基于Maven依赖图确定需要重新编译的项目。

作为CI服务器,在Hudson项目的官方站点下载War格式的安装包后,可以方便的部署在GlassFish或Tomcat容器之上。在Hudson中,配置一个新的项目也十分快速直观,在新建 Hudson工程时填写名称和描述信息、设定检查代码储存库的时间间隔、指定本地代码编译路径、指定储存库的访问路径和授权用户、填写工程和分支名称以及构建完成后的动作等,就完成了整个CI工程的配置工作。

在结果展示方面,用户可以在编译日志中查看Hudson通过不同颜色标记列出的信息。Hudson还提供了易用的报表功能,并拥有强大的插件支持,如具有能显示测试结果趋势等信息的插件,以及随时间轴跟踪Bugs并监控代码覆盖的插件。在通知机制方面,Hudson可以方便的与用户建立联系,Hudson 提供了电子邮件通知选项,还支持以RSS方式输出报错通知。

目前,包括NetBeans项目本身以及Ruby IDE在内的众多项目都在使用Hudson实现持续集成,更多Hudson的相关内容,可以在Hudson的Wiki中查看并了解详细的使用方式。

同时Hudson支持插件扩展,你可以通过其的插件管理功能从网络上下载你所需要的插件,也可以自己为所在的工作团队创建符合需要的插件。下面我就来介绍下如何开发一个Hudson的插件

首先你要有Maven 2和JDK1.6以上,这是必须的。然后在你的Maven 2的setting.xml 文件中加入下列代码

Xml代码 
  1. 1. <settings>
  2. 2. ..
  3. 3. <profiles>
  4. 4.   <profile>
  5. 5.     <id>hudson</id>
  6. 6.
  7. 7.     <activation>
  8. 8.       <activeByDefault />
  9. 9.     </activation>
  10. 10.
  11. 11.    <pluginRepositories>
  12. 12.      <pluginRepository>
  13. 13.        <id>m.g.o-public</id>
  14. 14.        <url>http://maven.glassfish.org/content/groups/public/</url>
  15. 15.      </pluginRepository>
  16. 16.    </pluginRepositories>
  17. 17.    <repositories>
  18. 18.      <repository>
  19. 19.        <id>m.g.o-public</id>
  20. 20.        <url>http://maven.glassfish.org/content/groups/public/</url>
  21. 21.      </repository>
  22. 22.    </repositories>
  23. 23.  </profile>
  24. 24.</profiles>
  25. 25.
  26. 26.<activeProfiles>
  27. 27.  <activeProfile>hudson</activeProfile>
  28. 28.</activeProfiles>
  29. 29.
  30. 30.<pluginGroups>
  31. 31.  <pluginGroup>org.jvnet.hudson.tools</pluginGroup>
  32. 32.</pluginGroups>
  33. 33...
  34. 34.</settings>

这样会将你的Maven指向有着Hudson-related Maven plugins的仓库,而且允许你使用Hudson Maven plugins的短名字来调用相关的插件(例如:hpi:create 代替org.jvnet.hudson.tools:maven-hpi-plugin:1.23:create)。

接着在CMD中输入

Java代码 
  1. 1. mvn hpi:create

之后会问你一些如groupId和artifactId之类的问题,按照需要来填写就好了。

完成后计算机会自动的创建了一个项目,里面有一些模板代码,可供你学习如何开始写一个Hudson的插件,后面的代码全部来自模版代码。如果你需要在Eclipse里编辑插件可以执行

Java代码 
  1. 1. mvn -DdownloadSources=true eclipse:eclipse

然后你就可以在Eclipse中导入这个项目并开始开发了。

经过前一篇文章的步骤,在我们的指定目录下已经生成了一个Hudson 插件的项目文件夹。在Eclipse中导入这个项目,我们可以看见项目有如下的结构:

+ src 
    + main 
        + java 
             +  full.package.name 
                    +- HelloWorldBuilder.java 
                    +- PluginImpl.java 
+resources 
             +  full.package.name 
                    +- config.jelly 
                    +- global.jelly 
                +- index.jelly 
        + webapp 
            +- help-globalConfig.html 
            +- help-projectConfig.html

PluginImpl.java: 
这个类是用于插件注册扩展点(Extension points)的一个类,在这里,我们注册了一个Builder。其实在Hudson 中有很多不同种类的扩展点,比如Publisher、Recorder 等等。详细的说明可以参考Hudson 的网站。

Java代码 
  1. 1. public class PluginImpl extends Plugin {
  2. 2.     public void start() throws Exception {
  3. 3.
  4. 4.         BuildStep.BUILDERS.add(HelloWorldBuilder.DESCRIPTOR);
  5. 5.     }
  6. 6. }
HelloWorldBuilder.java 

这个类就是具体实现某一扩展点的一个类,在这里由于要扩展Builder这个扩展点,所以继承了 Builder 这个类

Java代码 
  1. 1. public class HelloWorldBuilder extends Builder {
  2. 2.
  3. 3.     private final String name;
  4. 4.
  5. 5.     @DataBoundConstructor
  6. 6.     public HelloWorldBuilder(String name) {
  7. 7.         this.name = name;
  8. 8.     }
  9. 9.
  10. 10.    /**
  11. 11.     * We'll use this from the <tt>config.jelly</tt>.
  12. 12.     */
  13. 13.    public String getName() {
  14. 14.        return name;
  15. 15.    }
  16. 16.
  17. 17.    public boolean perform(Build build, Launcher launcher, BuildListener listener) {
  18. 18.        // this is where you 'build' the project
  19. 19.        // since this is a dummy, we just say 'hello world' and call that a build
  20. 20.
  21. 21.        // this also shows how you can consult the global configuration of the builder
  22. 22.        if(DESCRIPTOR.useFrench())
  23. 23.            listener.getLogger().println("Bonjour, "+name+"!");
  24. 24.        else
  25. 25.            listener.getLogger().println("Hello, "+name+"!");
  26. 26.        return true;
  27. 27.    }
  28. 28.
  29. 29.    public Descriptor<Builder> getDescriptor() {
  30. 30.        // see Descriptor javadoc for more about what a descriptor is.
  31. 31.        return DESCRIPTOR;
  32. 32.    }
  33. 33.
  34. 34.    /**
  35. 35.     * Descriptor should be singleton.
  36. 36.     */
  37. 37.    public static final DescriptorImpl DESCRIPTOR = new DescriptorImpl();
  38. 38.
  39. 39.    /**
  40. 40.     * Descriptor for {@link HelloWorldBuilder}. Used as a singleton.
  41. 41.     * The class is marked as public so that it can be accessed from views.
  42. 42.     *
  43. 43.     * <p>
  44. 44.     * See <tt>views/hudson/plugins/hello_world/HelloWorldBuilder/*.jelly</tt>
  45. 45.     * for the actual HTML fragment for the configuration screen.
  46. 46.     */
  47. 47.    public static final class DescriptorImpl extends Descriptor<Builder> {
  48. 48.        /**
  49. 49.         * To persist global configuration information,
  50. 50.         * simply store it in a field and call save().
  51. 51.         *
  52. 52.         * <p>
  53. 53.         * If you don't want fields to be persisted, use <tt>transient</tt>.
  54. 54.         */
  55. 55.        private boolean useFrench;
  56. 56.
  57. 57.        DescriptorImpl() {
  58. 58.            super(HelloWorldBuilder.class);
  59. 59.        }
  60. 60.
  61. 61.        /**
  62. 62.         * Performs on-the-fly validation of the form field 'name'.
  63. 63.         *
  64. 64.         * @param value
  65. 65.         *      This receives the current value of the field.
  66. 66.         */
  67. 67.        public void doCheckName(StaplerRequest req, StaplerResponse rsp, @QueryParameter final String value) throws IOException, ServletException {
  68. 68.            new FormFieldValidator(req,rsp,null) {
  69. 69.                /**
  70. 70.                 * The real check goes here. In the end, depending on which
  71. 71.                 * method you call, the browser shows text differently.
  72. 72.                 */
  73. 73.                protected void check() throws IOException, ServletException {
  74. 74.                    if(value.length()==0)
  75. 75.                        error("Please set a name");
  76. 76.                    else
  77. 77.                    if(value.length()<4)
  78. 78.                        warning("Isn't the name too short?");
  79. 79.                    else
  80. 80.                        ok();
  81. 81.
  82. 82.                }
  83. 83.            }.process();
  84. 84.        }
  85. 85.
  86. 86.        /**
  87. 87.         * This human readable name is used in the configuration screen.
  88. 88.         */
  89. 89.        public String getDisplayName() {
  90. 90.            return "Say hello world";
  91. 91.        }
  92. 92.
  93. 93.        public boolean configure(StaplerRequest req, JSONObject o) throws FormException {
  94. 94.            // to persist global configuration information,
  95. 95.            // set that to properties and call save().
  96. 96.            useFrench = o.getBoolean("useFrench");
  97. 97.            save();
  98. 98.            return super.configure(req);
  99. 99.        }
  100. 100.
  101. 101.              /**
  102. 102.               * This method returns true if the global configuration says we should speak French.
  103. 103.               */
  104. 104.              public boolean useFrench() {
  105. 105.                  return useFrench;
  106. 106.              }
  107. 107.          }
  108. 108.      }

是不是看得有点头晕,呵呵,下面我来逐步分析这些代码

Java代码 
  1. 1. @DataBoundConstructor
  2. 2.     public HelloWorldBuilder(String name) {
  3. 3.         this.name = name;
  4. 4.     }
  5. 5.
  6. 6.     /**
  7. 7.      * We'll use this from the <tt>config.jelly</tt>.
  8. 8.      */
  9. 9.     public String getName() {
  10. 10.        return name;
  11. 11.    }

这段代码用于构造这个Bulider并且从相应的config.jelly中获取相应的参数。Hudson使用了一种叫structured form submission的技术,使得可以使用这种方式活动相应的参数。

Java代码 
  1. 1. public boolean perform(Build build, Launcher launcher, BuildListener listener) {
  2. 2.         // this is where you 'build' the project
  3. 3.         // since this is a dummy, we just say 'hello world' and call that a build
  4. 4.
  5. 5.         // this also shows how you can consult the global configuration of the builder
  6. 6.         if(DESCRIPTOR.useFrench())
  7. 7.             listener.getLogger().println("Bonjour, "+name+"!");
  8. 8.         else
  9. 9.             listener.getLogger().println("Hello, "+name+"!");
  10. 10.        return true;
  11. 11.    }

方法perform()是个很重要的方法,当插件运行的的时候这个方法会被调用。相应的业务逻辑也可以在这里实现。比如这个perform()方法就实现了怎么说 “Hello”

接下来,在HelloBuilder 这个类里面有一个叫 DescriptorImpl 的内部类,它继承了Descriptor。在Hudson 的官方说明文档里说Descriptor包含了一个配置实例的元数据。打个比方,我们在工程配置那里对插件进行了配置,这样就相当于创建了一个插脚的实例,这时候就需要一个类来存储插件的配置数据,这个类就是Descriptor。

Java代码 
  1. 1. public String getDisplayName() {
  2. 2.             return "Say hello world";
  3. 3.         }

如上面的代码,可以在Descriptor的这个方法下设置插件在工程配置页面下现实的名字

Java代码 
  1. 1. public boolean configure(StaplerRequest req, JSONObject o) throws FormException {
  2. 2.             // to persist global configuration information,
  3. 3.             // set that to properties and call save().
  4. 4.             useFrench = o.getBoolean("useFrench");
  5. 5.             save();
  6. 6.             return super.configure(req);
  7. 7.         }

如同注释属所说,这个方法用于将全局配置存储到项目中

注意点:

HUDSON_HOME:

Hudson需要一个位置来进行每次构建,保留相关的配置信息,以及保存测试的结果,这就是在部署好了Hudson环境以后,系统就会自动在当前用户下新建一个.hudson,在linux下如:~/.hudson,我们有三种方式来改变这个路径:

1.  在启动servlet容器之前,设置:“HUDSON_HOME”环境变量,指向你需要设定的目录

2.  在servlet容器之中,设定系统属性

3.  设置一个JNDI的环境实体<env-entry>“HUDSON_HOME”指向您所想要设定的目录

目前我们在glassfish中设置jvm-option的方式属于第二种。

当我们设置好这个变量以后想要换个目录,但又不想丢掉以前的配置怎么办,很简单,关闭Hudson,将目录A的内容拷贝的目录B中去,然后重新设定“HUDSON_HOME”的值,然后重启,你会发现之前你所作的所有配置都完好的保留了下来

1、Hudson-home的目录结构:

HUDSON_HOME

+- config.xml     (hudson的基本配置文件,如:jdk的安装路径)

+- *.xml          (其他系统相关的配置文件,比如:旺旺插件的全局配置信息)

+- fingerprints   (储存文件版本跟踪记录信息)

+- plugins        (存放插件)

+- jobs

+- [JOBNAME]      (任务名称,对应页面上的project name)

+- config.xml     (任务配置文件,类似于CC的config.xml中各个项目的配置)

+- workspace      (SCM所用到的目录,hudson所下载代码默认存放在这个目录)

+- builds

+- [BUILD_ID]     (每一次构建的序号)

+- build.xml      (构建结果汇总)

+- log            (运行日志)

+- changelog.xml  (SCM修改日志)

小提示:如果你使用了e-mail或者旺旺通知来接受测试消息,并且hudson的迁移设计到不同ip地址机器的迁移的话,可能需要去Hudson的主配置中修改一下Hudson的访问地址

workspace:

刚才在hudson-home的目录结构中已经看到了workspce,假设当前hudson-home为/home/hudson-home,那么当我们在hudson上配置一个项目demo的时候,就会在新建一个目录/home/hudson-home/demo,在第一次运行之前,在jobs下并没有demo这个文件夹,只有当第一次运行以后才会在jobs目录下创建demo目录,当代码顺利从svn上下载下来时才会创建workspace文件夹,所有从svn下载下来的代码都会存放在这个目录下。

1、相对路径:

项目配置过程中,Hudson使用的是相对路径,对于Hudson,在我们新建一个项目比如demo后,假设workspace的目录结构为:

workspace

+- demo

+- pom.xml

+- src

那么测试报告的路径就为demo/target/surefire-reports/*.xml,系统会自动去当前项目的workspace中去寻找这个路径

mvn package  -- 完成代码开发之后执行,按照pom.xml 中的配置信息将会打包为hpi 格式的插件文件,这个就是你最终可以拿来上传给你的hudson 平台的玩意

mvn hpi:run   -- 在本地的Jetty 中运行你的hudson 插件,调试专用,当然可以使用Debug 模式,执行之后,在本地访问http://localhost:8080/ 即可见(注意不要占用8080 端口)

mvnDebug hup:run ,debug调试模式

持续集成(三)- hudson插件入门相关推荐

  1. Jenkins持续集成环境之插件管理和角色管理

    1.持续集成环境-Jenkins插件管理 Jenkins本身不提供很多功能,我们可以通过使用插件来满足我们的使用.例如从Gitlab拉取代码,使用Maven构建项目等 功能需要依靠插件完成.接下来演示 ...

  2. 持续集成工具hudson

    一.什么是持续集成 持续集成的核心概念 CI 过程会经常构建软件组件:在许多情况下,每当源代码存储库(比如 Subversion 或 ClearCase)中的代码发生变化时,都要构建软件组件.CI 的 ...

  3. 持续集成学习笔记-入门篇(1)持续集成基本概念

    今年7月份中下旬,笔者见过一个号称"资深开发者"的哥们(据说编程有十来年了),笔者问他:"你们平时用的持续集成工具都有哪些?"这哥们回答:"那些都是骗 ...

  4. 持续集成工具Hudson安装实例

    安装maven 下载maven,解压 [root@localhost local]# pwd /usr/local [root@localhost local]# tar -zxvf apache-m ...

  5. 持续集成 之 Jenkins插件 Multiple SCMs Plugin

    背景 由于项目需要,我将源码分为多个版本库进行管理,像这样情况,如何使用Jenkins进行持续集成呢?经过一番摸索,Jenkins的一个插件解决了我的问题:Multiple SCMs Plugin.该 ...

  6. 持续集成工具TeamCity快速入门

    大名鼎鼎的Intellij IDEA大家都听说过吧,它的出品公司Jetbrains不仅推出了一系列好用的IDE,同时还推出了现在正热的Kotlin语言.Jetbrains还有一个非常好用的产品就是今天 ...

  7. 可持续集成(devops)工具盘古入门指南

    一.盘古介绍 盘古是javashop团队内部总结多年的部署经验推出的一款开源的devops工具, 致力于在提供简单.使用.高效的可持续集成服务.在目前流行的devops工具中缺少对机器.仓库.步骤.环 ...

  8. 持续集成之jenkins插件管理及镜像源替换

    jenkins本身未提供很多功能.可以通过使用插件来满足我们的需求.jenkins国外官方插件地址下载速度非常慢.所以可以修改为国内插件地址 1.替换国内插件下载地址 Jenkins > Man ...

  9. 【devops】持续集成环境-jenkins插件管理

    文章目录 前言 一. jenkins插件地址修改 web端后台修改 2. 在配置文件里修改: 3. 修改完后重启jenkins 二 . 安装插件举例 总结 前言 jenkins本身的功能并不多,很多的 ...

最新文章

  1. SAP MM MB21创建预留单据报错- Error during conversion to alternative units of measure -
  2. 【专访】PP租车孙览江:与有梦想的人一拍即合,PM都有改变世界的小情怀
  3. 如何制作流畅有力的游戏动画+Skullgirls案例分析
  4. 理解spark闭包以及broadcast(转载)
  5. Taro+react开发(29)引入固定地址的方式
  6. 如何设置mysql让其他人能访问_怎么设置MySQL就能让别人访问本机的数据库了?...
  7. MySQL事务隔离级别及场景测试
  8. 搭建keepalived遇到的问题
  9. kubernetes Ingress是什么
  10. 半连续性:上半连续与下半连续
  11. 关于技术人员的非技术能力
  12. GB码和BIG5码的互换技术-foxpro版-摘自csdn-faq
  13. python 用selenium获取好友空间说说及时间写入txt
  14. vue 实现一个滑块拖动验证功能
  15. 电脑无法打开计算机的策略对象,win10系统提示“无法打开此计算机上的组策略对象”的解决方法...
  16. 图片在盒子内等比展示不变形
  17. RabbitMQ(详解)
  18. 6 仓储管理系统 门店端功能
  19. revit模型旋转整个项目实际角度(非查看角度)
  20. 怎样把pdf文件拆分开

热门文章

  1. Java成神之路(二十六)Hibernate
  2. mac版太空地球3k动态壁纸安装方法
  3. 7-12 求6+66+666+6666+66666。分数 10
  4. mysql索引查询 with_mysql select with in子句不使用索引
  5. 奖客富翁系统代码C语言,木马代码-c语言木马代码,最简单的,我保证不做违法的 – 手机爱问...
  6. 安装RVM时报错 curl: (7) Failed to connect to raw.githubusercontent.com port 443: Connection refused
  7. 打开机顶盒显示e16服务器加扰,机顶盒常见错误代码说明和解决方法
  8. postgresql 动态添加过滤条件_PostgreSQL WHERE 子句
  9. C/C++黑洞陷阱(Kaprekar问题)
  10. c语言除法留小数点两位小数,高精度除法小数点位数