当前位置:编程学习 > 网站相关 >>

Jenkins插件开发入门

Jenkins插件开发指南

环境变量

为了能开发插件,开发环境需安装Maven和JDK 6.0以上版本

配置maven的settings.xml配置文件

01
<settings>
02
  <pluginGroups>
03
    <pluginGroup>org.jenkins-ci.tools</pluginGroup>
04
  </pluginGroups>
05
<profiles>
06
<!-- Give access to Jenkins plugins -->
07
    <profile>
08
      <id>jenkins</id>
09
      <activation>
10
        <activeByDefault>true</activeByDefault> <!-- change this to false, if you don't like to have it on per default -->
11
      </activation>
12
      <repositories>
13
        <repository>
14
          <id>repo.jenkins-ci.org</id>
15
          <url>http://repo.jenkins-ci.org/public/</url>
16
        </repository>
17
      </repositories>
18
      <pluginRepositories>
19
        <pluginRepository>
20
          <id>repo.jenkins-ci.org</id>
21
          <url>http://repo.jenkins-ci.org/public/</url>
22
        </pluginRepository>
23
      </pluginRepositories>
24
    </profile>
25
  </profiles>
26
</settings>

创建新的插件

创建插件之前需运行以下Maven命令:


1
mvn -cpu hpi:create
该操作需要你输入一些参数,比如说groupid,artifactid。之后会创建一个新的插件模板便于开发者之后的开发工作。确保你可以使用一下命令:


1
cd newly-create-directory
2
mvn package
设置Eclipse开发环境

1
mvn -DdownloadSources=true -DdownloadJavadocs=true -DoutputDirectory=target/eclipse-classes eclipse:eclipse
或者 使用m2eclipse插件在Eclipse打开即可

插件目录结构

pom.xml:Maven的构建配置文件

src/main/java:Java源文件目录

src/main/resources:插件Jelly/Grovy视图

src/main/webapps:插件的静态资源如images和html文件

插件调试

插件开发中在使用一下命令对插件进行调试

Windows

1
set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,address=8000,suspend=n
2
mvn hpi:run
Linux

1
$ export MAVEN_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,address=8000,suspend=n"
2
$ mvn hpi:run
改变端口

1
mvn hpi:run -Djetty.port=8090
设置comtext path

1
mvn hpi:run -Dhpi.prefix=/jenkins
插件发布

1
mvn package
源码分析

001
<b>import hudson.Launcher;
002
import hudson.Extension;
003
import hudson.util.FormValidation;
004
import hudson.model.AbstractBuild;
005
import hudson.model.BuildListener;
006
import hudson.model.AbstractProject;
007
import hudson.tasks.Builder;
008
import hudson.tasks.BuildStepDescriptor;
009
import net.sf.json.JSONObject;
010
import org.kohsuke.stapler.DataBoundConstructor;
011
import org.kohsuke.stapler.StaplerRequest;
012
import org.kohsuke.stapler.QueryParameter;
013
 
014
import javax.servlet.ServletException;
015
import java.io.IOException;
016
 
017
/**
018
 * Sample {@link Builder}.
019
 *
020
 * <p>
021
 * When the user configures the project and enables this builder,
022
 * {@link DescriptorImpl#newInstance(StaplerRequest)} is invoked
023
 * and a new {@link HelloWorldBuilder} is created. The created
024
 * instance is persisted to the project configuration XML by using
025
 * XStream, so this allows you to use instance fields (like {@link #name})
026
 * to remember the configuration.
027
 *
028
 * <p>
029
 * When a build is performed, the {@link #perform(AbstractBuild, Launcher, BuildListener)}
030
 * method will be invoked.
031
 */
032
public class HelloWorldBuilder extends Builder {
033
 
034
    private final String name;
035
 
036
    // Fields in config.jelly must match the parameter names in the "DataBoundConstructor"
037
    @DataBoundConstructor
038
    public HelloWorldBuilder(String name) {
039
        this.name = name;
040
    }
041
 
042
    /**
043
     * We'll use this from the <tt>config.jelly</tt>.
044
     */
045
    public String getName() {
046
        return name;
047
    }
048
 
049
    @Override
050
    public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) {
051
        // This is where you 'build' the project.
052
        // Since this is a dummy, we just say 'hello world' and call that a build.
053
 
054
        // This also shows how you can consult the global configuration of the builder
055
        if (getDescriptor().getUseFrench())
056
            listener.getLogger().println("Bonjour, "+name+"!");
057
        else
058
            listener.getLogger().println("Hello, "+name+"!");
059
        return true;
060
    }
061
 
062
    // Overridden for better type safety.
063
    // If your plugin doesn't really define any property on Descriptor,
064
    // you don't have to do this.
065
    @Override
066
    public DescriptorImpl getDescriptor() {
067
        return (DescriptorImpl)super.getDescriptor();
068
    }
069
 
070
    /**
071
     * Descriptor for {@link HelloWorldBuilder}. Used as a singleton.
072
     * The class is marked as public so that it can be accessed from views.
073
     *
074
   

补充:Web开发 , 其他 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,