設置環境變量 ‘JAVA_HOME’

1. 獲取最新版本從http://maven.apache.org/download.cgi 然后解壓縮到你想要安裝到的目錄下
2. 設置環境變量M2_HOME

3. 設置環境變量M2 and set it to“%M2_HOME%/bin”
4. 添加 %M2%;到PATH環境變量
5. 打開 command PRompt ,運行“mvn -version” 來確認是否安裝成功.
6.對%USERPROFILE%/.m2/settings.xml(USERPROFILE為用戶名路徑如C:/Documents and Settings下的用戶)文件添加以下內容:
如果.m2文件夾不存在,你需要自己創建
Copy/Paste 以下內容到settings.xml:
<settings> <localRepository>d:/SW/localRepository/</localRepository> <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>1.3 安裝Integrated Development Environment (IDE)
http://www.eclipse.org/downloads/
2. 基于Eclipse開發JenkinsPlugin
2.1 Maven Projects
每個Maven工程的核心是pom.xml,他包含所有構建這個項目的信息,這個文件通常會很大而且難懂,通常我們不需要完整的理解它
當你下載一個Maven項目是,它至少包含pom.xml和src文件夾
關于Maven詳細內容:
Build Lifecycle: http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.htmlPOM Reference: http://maven.apache.org/pom.html
2.2 導入Maven到Eclipise
為了能在Eclipse中使用Maven 工程,我們首先需要能夠將其導,但是Eclipse并不支持Maven,這里我們有2種辦法
1. 安裝Eclipse plugin à m2e (Maven 2 Eclipse)
2. 使用Jenkins wiki推薦的方法
這里我們使用第2種方法,步驟如下
1. 打開commandprompt 然后切換到pom.xml所在的路徑
2. 使用以下命令生成Eclipse工程
mvn -DdownloadSources=true -DdownloadJavadocs=true -DoutputDirectory=target/eclipse-classes eclipse:eclipse3. Import工程到eclipse中
4. 設置系統變量M2_REPO
Java > Build Path > Classpath Variables
5. 點擊New 按鈕,然后定義M2_REPO 變量,內容為軟件庫的下載路徑(見1.2,步驟6)
2.3 編譯和運行plugin
在pom.xml所在的目錄下,執行mvn package, 編譯結果<plugin-name>.hpi將被輸出在target 文件夾下.
Clean 工程: mvn clean package
2.4 使用Eclipse 調試plugin
運行以下2個命令,將開啟Jenkins 調試模式,其允許Eclipse連接到remote debugging session
set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,address=8000,suspend=nmvn hpi:run我們需要在Eclipse 創建Remote Java application
舉例:
set MAVEN_OPTS=-Xdebug-Xrunjdwp:transport=dt_socket,server=y,address=8000,suspend=nset JENKINS_HOME=d:/sw/ci/Jenkinsmvn hpi:run
3. 創建Plugin
3.1 使用skeleton generator
最簡單的辦法是使用skeleton generator來創建,然后在進行擴展
http://plugin-generator.jenkins-ci.org/
3.2 擴展點
必須確保plugin實現了一個擴展點
HelloWorldBuilder 類實現了Builder這個擴展點。因此在Build Step中我們可以找到它,如果實現SCM擴展點,在Build Triggers中可以找到
更多擴展點:https://wiki.jenkins-ci.org/display/JENKINS/Extension+points
3.3 源碼分析
public class HelloWorldBuilder extends Builder { private final String name; // Fields in config.jelly must match the parameter names in the "DataBoundConstructor" @DataBoundConstructor public HelloWorldBuilder(String name) { this.name = name; } /** * We'll use this from the <tt>config.jelly</tt>. */ public String getName() { return name; } @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. // 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; } // Overridden for better type safety. // If your plugin doesn't really define any property on Descriptor, // you don't have to do this. @Override public DescriptorImpl getDescriptor() { return (DescriptorImpl)super.getDescriptor(); } /** * Descriptor for {@link HelloWorldBuilder}. Used as a singleton. * The class is marked as public so that it can be accessed from views. * * <p> * See <tt>src/main/resources/hudson/plugins/hello_world/HelloWorldBuilder/*.jelly</tt> * for the actual HTML fragment for the configuration screen. */ @Extension // This indicates to Jenkins that this is an implementation of an extension point. public static final class DescriptorImpl extends BuildStepDescriptor<Builder> { /** * To persist global configuration information, * simply store it in a field and call save(). * * <p> * If you don't want fields to be persisted, use <tt>transient</tt>. */ private boolean useFrench; /** * Performs on-the-fly validation of the form field 'name'. * * @param value * This parameter receives the value that the user has typed. * @return * Indicates the outcome of the validation. This is sent to the browser. */ public FormValidation doCheckName(@QueryParameter String value) throws IOException, ServletException { if (value.length() == 0) return FormValidation.error("Please set a name"); if (value.length() < 4) return FormValidation.warning("Isn't the name too short?"); return FormValidation.ok(); } public boolean isApplicable(Class<? extends AbstractProject> aClass) { // Indicates that this builder can be used with all kinds of project types return true; } /** * This human readable name is used in the configuration screen. */ public String getDisplayName() { return "Say hello world"; } @Override public boolean configure(StaplerRequest req, JSONObject formData) throws FormException { // To persist global configuration information, // set that to properties and call save(). useFrench = formData.getBoolean("useFrench"); // ^Can also use req.bindJSON(this, formData); // (easier when there are many fields; need set* methods for this, like setUseFrench) save(); return super.configure(req,formData); } /** * This method returns true if the global configuration says we should speak French. * * The method name is bit awkward because global.jelly calls this method to determine * the initial state of the checkbox by the naming convention. */ public boolean getUseFrench() { return useFrench; } } }HelloWorldBuilder類中的構造函數使用@DataBoundConstructor來聲明。構造函數要對變量進行賦值。
HelloWorldBuilder類中perform重載函數。構建的執行通過實現perform方法來進行自定義。每次執行編譯時都會運行perform函數。它有三個參數:
Build參數是描述了當前任務的一次構建,通過它可以訪問到一些比較重要的模型對象如:project當前項目的對象、workspace構建的工作空間、Result當前構建步驟的結果。
Launcher參數用于啟動構建。
BuildListener該接口用于檢查構建過程的狀態(開始、失敗、成功..),通過它可以在構建過程中發送一些控制臺信息給jenkins。
perform方法的返回值告訴jenkins當前步驟是否成功,如果失敗了jenkins將放棄后續的步驟。
3.4 視圖配置文件
Jenkins使用了Jelly頁面渲染技術,這是一個基于XML的服務端頁面渲染引擎,其將基于Jelly的xml標簽轉換為對應的Html標簽并輸出到客戶端。模型對象的信息通過Jexl表達式被傳遞到頁面上(相當于jsp的JSTL)。jelly文件以.jelly為后綴,在hudson中使用類全名的形式來查找模型類對應的jelly頁面文件,如名為src/main/java/com/jysong/jenkins/HelloWorldBuilder.java的類,其對應的頁面文件應該存在于src/main/resources/com/jysong/jenkins/HelloWorldBuilder目錄的下。
此外hudson通過固定的命名方式來確定頁面文件屬于局部配置還是全局配置:config.jelly提供局部配置;global.jelly提供全局配置。config.jelly是具體的某個job的配置,global.jelly是指jenkins的系統配置。
視圖有三種:1,全局配置(global.jelly)2,Job配置(config.jelly),還有就是使用幫助(help-字段名).html
1.全局配置詳解
global.jelly為全局配置頁面。
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form"><f:section title="Hello World Builder"> <f:entry title="French" field="useFrench" description="Check if we should say hello in French"> <f:checkbox /> </f:entry></f:section></j:jelly>其中title為標題,表示要顯示的內容。field為將調用DescriptorImpl內部類的方法getUseFrench(),field域會將方法去掉get并且將第一個字母小寫,找到相對應的方法。description將顯示描述信息。f:checkbox為復選框控件。
在每次保存全局配置時,jenkins都會調用該descriptor對象,并調用其configure方法,可以實現該方法并提供自己的定制
在DescriptorImpl中的configure方法中,可以對全局配置進行操作。save方法用于將當前Descriptor所提供的配置持久化(通過get**方法),為了使save能正常工作,需要提供配置項的get方法。
2.局部配置詳解
config.jelly 的內容將被包含在擴展功能的配置中,在HelloWorldBuilder文件夾下面的配置內容是:
其中entry 表示用于交互的html表單域,title將作為表單域label的值,在界面中要顯示的內容。 field表示的是HelloWorldBuilder的構造函數中的參數。如果有多個field,就要有多個相對應的參數。textbox 表示簡單的渲染一個輸入文本,輸入的值將賦給name。
更多UI內容見https://wiki.jenkins-ci.org/display/JENKINS/UI+Samples+Plugin
新聞熱點
疑難解答