设为首页 收藏本站
查看: 1059|回复: 0

[经验分享] Jenkins插件开发(2)——搭建开发环境

[复制链接]

尚未签到

发表于 2017-2-28 10:32:17 | 显示全部楼层 |阅读模式
  官方文档参照:https://wiki.jenkins-ci.org/display/JENKINS/Plugin+tutorial
  This document, together with the hello-world plugin, shows you how to get started with the plugin development.

What Can Plugins Do?
  Jenkins defines extensibility points, which are interfaces or abstract classes that model an aspect of a build system. Those interfaces define contracts of what need to be implemented, and Jenkins allows plugins to contribute those implementations. See this document for more about extension points.
  In this document, we'll be implementing a Builder that says hello. (built-in builders include Ant, Maven, and shell script. Builders build a project.)

Setting Up Environment
  To develop a plugin, you need Maven 2 (why?) and JDK 6.0 or later. If this is the first time you use Maven, make sure Maven can download stuff over the internet.



DSC0000.png Nexus Users
If you are using the Nexus Maven Repository Manager, you can ignore these instructions, and instead, click here for instructions on how to add Jenkins build prerequisites and the proper settings.xml entries.
  With a fairly recent version of Maven (ie. 2.0.9 or newer) you should only need to add the following to your ~/.m2/settings.xml (Windows users will find them in%USERPROFILE%\.m2\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>
  This will let you use short names for Jenkins Maven plugins (i.e. hpi:create instead of org.jenkins-ci.tools:maven-hpi-plugin:1.61:create).
  备注:因为我用的是Nexus Maven Repository,所以直接配了一个Proxy到“http://repo.jenkins-ci.org/public”。

Creating a New Plugin
  To start a new plugin, use the online skeleton generator. Alternatively, if you are more comfortable with Maven, run the following command:







$ mvn -U org.jenkins-ci.tools:maven-hpi-plugin: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:
  -U 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
Setting up a productive environment with your IDE

NetBeans
  NetBeans users can use the IDE's Maven support to open the project directly. (Bundled in 6.7 and up; available from Plugin Manager for 6.5.) The archetype may also be available right from the New Project dialog.
  As you navigate through the code, you can tell NetBeans to attach source code jar files by clicking "Attach" button that appears in the top of the main content window. This allows you to read the Jenkins core source code as you develop plugins.

IntelliJ IDEA
  IntelliJ 7.0 (or later) users can load pom.xml directly from IDE, and you should see all the source code of libraries and Jenkins core all the way to the bottom.



DSC0001.png IntelliJ defaults to downloading sources and JavaDocs on demand. So, to see the source, you may need to click the Download artifacts button in the Maven Projects tab.

Eclipse
  Use Eclipse 3.3 or later to avoid a bug in Eclipse 3.2.
Eclipse users can run the following Maven command to generate Eclipse project files (the custom outputDirectory parameter is used to work around the lack of JSR-269 annotation processor support in Eclipse:)



$ mvn -DdownloadSources=true -DdownloadJavadocs=true -DoutputDirectory=target/eclipse-classes eclipse:eclipse
  Alternatively, Eclipse users can install m2eclipse to open a Maven project directly in IDE. (备注:用m2eclipse方便点。)
  If you get the error message Unable to find a plugin class. Did you put @plugin in javadoc? this maybe caused by Eclipse and Maven both using the target folder for build output.
Run mvn clean before building with Maven or change the output path.

Plugin Workspace Layout
  The plugin workspace consists of the following major pieces:

pom.xml
  Maven uses it for building your plugin.

src/main/java
  Java source files of the plugin.

src/main/resources
  Jelly/Groovy views of the plugin. See this document for more about it.

src/main/webapp
  Static resources of the plugin, such as images and HTML files.

Source Code
  Let's take a look at the source code. A plugin's main entry point is a PluginImpl class that extends from Plugin. Once Jenkins detects your plugin class (via its inheritance relationship from Plugin), it will create an instance, and invoke methods. A Plugin class is optional; a plugin may simply implement extensions:
  Most of the time, a plugin class just registers extension points, and your main work involves implementing those extension points. See the source code for more about how aBuilder is implemented and what it does.

Debugging a Plugin
  NetBeans 6.7+ users can just hit Debug. For all others, run the following command to launch Jenkins with your plugin:
Convenient:



mvnDebug hpi:run
  Unix:



$ export MAVEN_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,address=8000,suspend=n"
$ mvn hpi:run
  Windows:



> set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,address=8000,suspend=n
> mvn hpi:run
  If you open http://localhost:8080/ in your browser, you should see the Jenkins page running in Jetty. The MAVEN_OPTS portion launches this whole thing with the debugger port 8000, so you should be able to start a debug session to this port from your IDE.
  Once this starts running, keep it running. Jetty will pick up all the changes automatically.


  • When you make changes to view files in src/main/resources or resource files in src/main/webapp, just hit F5 in your browser to see the changes.
  • When you change Java source files, compile them in your IDE (NetBeans 6.7+: Debug > Apply Code Changes) and Jetty should automatically redeploy Jenkins to pick up those changes. There is no need to run mvn at all.
  启动后截图如下:


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-348315-1-1.html 上篇帖子: Jenkins: 基础篇(环境配置) 下篇帖子: 使用Gradle构建构建一个Java Web工程及持续集成环境Jenkins配置
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表