3年多沒有在csdn上寫blog了, 今天偶然和 念茜(http://blog.csdn.net/yiyaaixuexi) 聊了幾句,看到她最近寫了這麼多的技術blog, 我也上一點乾貨吧。
首先介紹一下什麼是Jenkins :
持續整合是一種軟體開發實踐,對於提高軟體開發效率並保障軟體開發品質提供了理論基礎。Jenkins 是一個開源軟體項目,旨在提供一個開放易用的軟體平台,使持續整合變成可能。 http://www.ibm.com/developerworks/cn/java/j-lo-jenkins/index.html
介紹Jenkins 的使用不是本文的重點, 本文的重點是如何把 Jenkins 作為一個基礎平台, 在上面做二次開發。
===================
Jenkins 平台有大量的 UI、遠端控制、編譯、部署、監控等等外掛程式。
我們這次的項目需求是開發一個業務營運平台, 對整個系統設定大量的監控點, 以各種可視化方式輸出即時的系統狀態圖表, 在Jenkins 平台進行二次開發是一個合適的方案。
我最終還是用 mvn+jetty的方法來開發jenkins外掛程式, 實際用下來感覺也還不錯~~
也算是一個很有用的技能。
(eclipse遠端偵錯)
http://www.ibm.com/developerworks/cn/opensource/os-eclipse-javadebug/index.html
sample 項目:
https://sourcer.yunrang.com/svn/yr/projects/branches/develop/ads/szpilot/plugins/ui-samples-plugin
先確保 %USERPROFILE%\.m2\settings.xml有以下內容, 同時, 需要 maven 3.0.4版本 , 升級方法不麻煩, 見之前的郵件。
<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>
</settings>
然後在外掛程式源碼目錄裡執行
mvn -DdownloadSources=true -DdownloadJavadocs=true -DoutputDirectory=target/eclipse-classes eclipse:eclipse
然後就可以用 mvn package 打包,
開發的時候, 用 mvn hpi:run 看結果, 調試。
用瀏覽器訪問 http://localhost:8080/
============================
同時, 還需要做的事情是:
檢出
https://github.com/jenkinsci/jenkins
執行
mvn eclipse:eclipse
然後匯入eclipse, 預設會是 jdk1.5的工作空間, 需要去 java-compile 和 build-path -> library 改成 1.6的環境, 比較好, 可以沒有紅色報錯。
==========================
全部做完, 就能在eclipse裡開發代碼, 並在瀏覽器裡查看效果了。
========================================
調試方法:
$ export MAVEN_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,address=8000,suspend=n"
$ mvn hpi:run
在mvn hpi:run啟動過程中, 在eclipse debug attach, 慢了會失敗,連不上。
jenkins外掛程式開發方法:
https://wiki.jenkins-ci.org/display/JENKINS/Defining+a+new+extension+point
Creating a New Plugin
To start a new plugin, run the following Maven command:
$ mvn -cpu hpi:create
This will ask you a few questions, like the groupId (the Maven jargon for the package name) and the artifactId (the Maven jargon for your project name), then create a skeleton plugin from which you can start
with. Make sure you can build this:
$ cd newly-created-directory
$ mvn package
Explanations:
-cpu means that Maven should update all relevant Maven plugins (check for plugin updates)
hpi: this prefix specifies that the Jenkins HPI Plugin is being invoked, a plugin that supports development of Jenkins plugins
create is the goal which creates the directory layout and the POM for your new Jenkins plugin and it adds it to the module list
package is a standard phase which compiles all sources, runs the tests and creates a package - when used by the HPI plugin it will create an *.hpi file
Building a Plugin
To build a plugin, run mvn install. This will create the file ./target/pluginname.hpi that you can deploy to Jenkins.
$ mvn install
=================