mvn -DdownloadSources=true -DdownloadJavadocs=true –DoutputDirectory= target/eclipse-classes eclipse:eclipse或者mvn eclipse:eclipse 在此目录中生成eclipse工程,可以使用eclipse将工程进行导入。如下图:
图10
5、工作空间布局
导入之后目录结构如下图:
图11
src/main/java :存放java源文件。
src/main/resources:jelly/Groovy视图文件。
src/main/webapp:静态资源文件,例如图片,HTML文件。 pom.xml:配置文件,Maven使用此文件编译插件。
6、源代码
在src/main/java/com/jysong/jenkins目录下可能有个null文件夹,在文件夹下面有HelloWorldBuilder.java文件,将HelloWorldBuilder.java文件拷贝到jenkins文件夹下面,将null文件夹删除。并且将HelloWorldBuilder.java文件中的第一行的package最后面的.null删除。HelloWorldBuilder.java文件是一个开发插件的模板,包含了开发一个简单插件的所有内容。后面将对这个文件代码进行详细分析。
在src/main/resources/com/jysong/jenkins目录下可能有个null文件夹,在文件夹下面有个HelloWorldBuilder文件夹,将HelloWorldBuilder文件夹拷贝到jenkins文件夹下面,将null文件夹删除。在HelloWorldBuilder文件夹下面有global.jelly和config.jelly配置文件。这两个文件是进行页面配置的文件。
7、调试插件
在windows系统上,执行以下命令:
set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket, server=y, address
6
=8000, suspend=n
mvn hpi:run
使用http://localhost:8080/ 在浏览器中登录,将会看到jenkins页在Jetty中运行。MAVEN_OPTS启动了端口为8000的调试器,可以在这个端口开启一个调试会话。
如果8080的端口被占用,将会出现错误,不会运行jetty服务器。可以更改端口使用以下命令:
mvn hpi:run –Djetty.port=8090 可以使用http://localhost:8090/进行登录了。 设置上下文路径 mvn hpi:run –Dhpi.prefix=/Jenkins
执行这个命令之后登录地址将变为http://localhost:8090/jenkins
8、发布插件
运行以下命令,生成你的插件的图片。 mvn package
生成 ./target/*.hpi文件,其他使用者可以使用jenkins的web界面上传到jenkins。
9、安装插件
在jenkins的web界面中由
Manage Jenkins>Manage Plugins>Advanced
图12
点击Choose File,选择你的插件的target目录下的hpi文件。选择之后点击Upload,插件就会配置到jenkins中。
到此一个简单的插件开发完成了,可以在此基础上进行更复杂的开发。详细开发插件流程的地址https://wiki.jenkins-ci.org/display/JENKINS/Plugin+tutorial
源码分析
1、java源代码
在目录src/main/java/com/jysong/jenkins下有文件HelloWorldBuilder.java。代码如下:
public class HelloWorldBuilder extends Builder { private final String name;
7
// Fields in config.jelly must match the parameter names in the \ @DataBoundConstructor
public HelloWorldBuilder(String name) { this.name = name; } /**
* We'll use this from the config.jelly. */
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(\+name+\); else
listener.getLogger().println(\+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. * *
* See
src/main/resources/hudson/plugins/hello_world/HelloWorldBuilder/*.jelly
* for the actual HTML fragment for the configuration screen. */
@Extension // This indicates to Jenkins that this is an implementation
8
of an extension point.
public static final class DescriptorImpl extends BuildStepDescriptor
* To persist global configuration information, * simply store it in a field and call save(). * *
* If you don't want fields to be persisted, use transient. */
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(\); if (value.length() < 4)
return FormValidation.warning(\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 \; }
@Override
public boolean configure(StaplerRequest req, JSONObject formData)
9
throws FormException {
// To persist global configuration information, // set that to properties and call save(). useFrench = formData.getBoolean(\); // ^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; } } }
这里主要使用了jenkins的Builder作为扩展点,Builder扩展点是编译时的功能。更多扩展点https://wiki.jenkins-ci.org/display/JENKINS/Extension+points。 HelloWorldBuilder类中的构造函数使用@DataBoundConstructor来声明。构造函
数要对变量进行赋值。
HelloWorldBuilder类中perform重载函数。构建的执行通过实现perform方法来进行自定义。每次执行编译时都会运行perform函数。它有三个参数:
Build参数是描述了当前任务的一次构建,通过它可以访问到一些比较重要的模型对象如:project当前项目的对象、workspace构建的工作空间、Result当前构建步骤的结果。
Launcher参数用于启动构建。
BuildListener该接口用于检查构建过程的状态(开始、失败、成功..),通过它可以在构建过程中发送一些控制台信息给jenkins。
perform方法的返回值告诉jenkins当前步骤是否成功,如果失败了jenkins将放弃后续的步骤。
在这个例子中if..else..语句是向控制台端输出日志信息,其中name的信息由构造函数有关。
将if..else..语句进行删除,添加以下代码。
int number = build.getNumber();
String version = build.getHudsonVersion();
10