jenkins插件开发

为什么要开发jenkins插件:

   Jenkins是持续集成运行、管理平台(与hudson一样,具体说明可以查看jenkinswiki)。jenkins本身提供了一套插件的管理机制,这些插件允许可插拨形式存在。jenkins插件虽然能提供很多种插件,但还是不能满足我们持续集成的需要,所以需要定制一些插件来支撑整个持续集成平台的运行。

 

Jenkins运行周期:

     Jenkins有自己运行的生命周期,如下:

  • checkout - check out出源码
  • Pre-build - 预编译
    <developers>
        <developer>
          <id>xxx</id>
          <name>Developer gaofeng.qiangf</name>
          <email>[email protected]</email>
        </developer>
      </developers>
      <build>
        	<plugins>
          		<plugin>
            		<groupId>org.apache.maven.plugins</groupId>
            		<artifactId>maven-enforcer-plugin</artifactId>
            		<version>1.0.1</version>
            		<configuration>
               			<!-- your example configuration here -->
            		</configuration>
          		</plugin>
    			<plugin>
            		<groupId>org.apache.maven.plugins</groupId>
           			 <artifactId>maven-compiler-plugin</artifactId>
           				<configuration>
          		    		<source>1.6</source>
           			   		<target>1.6</target>
          	          		<debug>true</debug>
            		</configuration>
          			</plugin>
        	</plugins>
      	</build>
    
     
  • Build wrapper-准备构建的环境,设置环境变量等
  • Builder runs - 执行构建,比如调用calling Ant, Make 等等
  • Recording - 记录输出,如测试结果
  • Notification - 通知成员

Jenkins提供的扩展点:

为了支撑插件能在各个生命周期中运行,jenkins提供了各种扩展点,我们主类必须要extends一个扩展点;针对现状,基本上只需要使用Notifier,与builder这两个扩展点;详细如下:

  •   Builder:这个扩展点支撑的是构建这个阶段需要做的事情,包括prebuild postbuid、构建环境等步骤,例如我们更换slave机器的hosts插件
  •  Notifier:包括通知、消息通知等情况,我们的sendnapolimessage是基于这种扩展点来开发的具体扩展点说明请参考:https://wiki.jenkins-ci.org/display/JENKINS/Extension+points

插件开发步骤

 

设置settings.xml(包括代理)

  因为插件使用的是maven的管理形式,所以需要settings文件,了解maven的同学应该知道settings文件的作用,这里不多说。需要在我们的settings.xml中添加如下设置:

 

<settings>
  <pluginGroups>
    <pluginGroup>org.jenkins-ci.tools</pluginGroup>
  </pluginGroups>

  <profiles>
    <!-- Give access to Jenkins plugins -->
    <profile>
      <id>jenkins</id>
      <activation>
        <activeByDefault>true</activeByDefault> <!-- change this to false, if you don't like to have it on per default -->
      </activation>
      <repositories>
        <repository>
          <id>repo.jenkins-ci.org</id>
          <url>http://repo.jenkins-ci.org/public/</url>
        </repository>
      </repositories>
      <pluginRepositories>
        <pluginRepository>
          <id>repo.jenkins-ci.org</id>
          <url>http://repo.jenkins-ci.org/public/</url>
        </pluginRepository>
      </pluginRepositories>
    </profile>
  </profiles>
  <mirrors>
    <mirror>
      <id>repo.jenkins-ci.org</id>
      <url>http://repo.jenkins-ci.org/public/</url>
      <mirrorOf>m.g.o-public</mirrorOf>
    </mirror>
  </mirrors> 
</settings>

 由于下载比较慢,所以我添加了一个proxy,具体设置如下:

 

<!--使用代理-->
  <proxy>
	<!-- 唯一标识 -->
      <id>my-proxy</id>
		<!-- 指这个代理是否被激活,比如上面提到有多个代理,则会寻找第一个被激活的代理 -->
      <active>true</active>
		<!-- 指使用http协议访问 -->
      <protocol>http</protocol>
		<!-- 服务器的IP地址 -->
      <host>218.14.227.197</host>
		<!-- 访问的端口号 -->
      <port>3128</port>
		<!--  如果服务器需要验证,那么这里就需要配置用户名和密码  -->
      <username>***</username>
      <password>***</password>
		<!-- 这里制定那些域名不需要被代理  -->
      <nonProxyHosts>repository.mycom.com|*.google.com</nonProxyHosts>
    </proxy>

 设置maven3

   Jenkins为什么要使用maven3,在使用maven2编译的时候都不会出现问题,但使用mvn-hpi插件的时候,会报空指针异常错误,查看了wiki,这个bug未解决,但切换到maven3该问题消失,应该是maven2对这个插件支持不够完善。

创建maven项目(pom文件介绍)

1、使用mvn –cpu create:hpi命令。会自己生成一个hellobuilder示例maven工程。,中间让你输入groupidaid

 2、在maven中添加我们需要的依赖以及jdk、开发者维护说明。和maven的方式一样。

 

<developers>
    <developer>
      <id>xxxx</id>
      <name>Developer gaofeng.qiangf</name>
      <email>[email protected]</email>
    </developer>
  </developers>
  <build>
    	<plugins>
      		<plugin>
        		<groupId>org.apache.maven.plugins</groupId>
        		<artifactId>maven-enforcer-plugin</artifactId>
        		<version>1.0.1</version>
        		<configuration>
           			<!-- your example configuration here -->
        		</configuration>
      		</plugin>
			<plugin>
        		<groupId>org.apache.maven.plugins</groupId>
       			 <artifactId>maven-compiler-plugin</artifactId>
       				<configuration>
      		    		<source>1.6</source>
       			   		<target>1.6</target>
      	          		<debug>true</debug>
        		</configuration>
      			</plugin>
    	</plugins>
  	</build>

导入eclipse

使用命令 mvn eclipse:eclipse,然后再eclipse中使用import功能导入,这个不做详细介绍,相信大家都会使用

项目目录介绍(重要):

 

猜你喜欢

转载自qian716719.iteye.com/blog/1953748