Jenkins插件开发:1: 环境准备和helloworld插件

在这里插入图片描述
Jenkins可以通过插件进行功能扩展,这篇文章介绍如何使用Jenkins进行插件开发,并结合一个具体的入门示例进行说明。

环境准备

开发环境

本文示例中使用Java(1.8)和Maven(3.5.4),版本详细信息如下所示:

liumiaocn:~ liumiao$ mvn --version
Apache Maven 3.5.4 (1edded0938998edf8bf061f1ceb3cfdeccf443fe; 2018-06-18T02:33:14+08:00)
Maven home: /Library/Maven/apache-maven-3.5.4
Java version: 1.8.0_191, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/jre
Default locale: en_CN, platform encoding: UTF-8
OS name: "mac os x", version: "10.14", arch: "x86_64", family: "mac"
liumiaocn:~ liumiao$ 

开发插件

步骤1: 使用mvn命令创建项目框架

执行命令:mvn -U archetype:generate -Dfilter=“io.jenkins.archetypes:”

使用上述命令进行Jenkins插件项目框架的创建。执行日志如下所示:

liumiaocn:plugin liumiao$ mvn -U archetype:generate -Dfilter="io.jenkins.archetypes:"
[INFO] Scanning for projects...
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-metadata.xml
...省略
[INFO] Generating project in Interactive mode
[INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0)
Choose archetype:
1: remote -> io.jenkins.archetypes:empty-plugin (Skeleton of a Jenkins plugin with a POM and an empty source tree.)
2: remote -> io.jenkins.archetypes:global-configuration-plugin (Skeleton of a Jenkins plugin with a POM and an example piece of global configuration.)
3: remote -> io.jenkins.archetypes:global-shared-library (Uses the Jenkins Pipeline Unit mock library to test the usage of a Global Shared Library)
4: remote -> io.jenkins.archetypes:hello-world-plugin (Skeleton of a Jenkins plugin with a POM and an example build step.)
5: remote -> io.jenkins.archetypes:scripted-pipeline (Uses the Jenkins Pipeline Unit mock library to test the logic inside a Pipeline script.)
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): : 4
Choose io.jenkins.archetypes:hello-world-plugin version: 
1: 1.1
2: 1.2
3: 1.3
4: 1.4
5: 1.5
6: 1.6
Choose a number: 6: 6
Downloading from central: https://repo.maven.apache.org/maven2/io/jenkins/archetypes/hello-world-plugin/1.6/hello-world-plugin-1.6.pom
...省略
[INFO] Using property: groupId = unused
Define value for property 'artifactId': helloworld-plugin
Define value for property 'version' 1.0-SNAPSHOT: : 
[INFO] Using property: package = io.jenkins.plugins.sample
Confirm properties configuration:
groupId: unused
artifactId: helloworld-plugin
version: 1.0-SNAPSHOT
package: io.jenkins.plugins.sample
 Y: : y
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Archetype: hello-world-plugin:1.6
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: unused
[INFO] Parameter: artifactId, Value: helloworld-plugin
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: package, Value: io.jenkins.plugins.sample
[INFO] Parameter: packageInPathFormat, Value: io/jenkins/plugins/sample
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: package, Value: io.jenkins.plugins.sample
[INFO] Parameter: groupId, Value: unused
[INFO] Parameter: artifactId, Value: helloworld-plugin
[INFO] Project created from Archetype in dir: /Users/liumiao/jenkins/plugin/helloworld-plugin
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 54.652 s
[INFO] Finished at: 2019-11-21T05:13:18+08:00
[INFO] ------------------------------------------------------------------------
liumiaocn:plugin liumiao$

步骤2: 构建插件

因为使用步骤1生成的插件已经包含示例,所以可以直接执行构建。生成的初始示例项目结构如下所示:

liumiaocn:plugin liumiao$ tree helloworld-plugin/
helloworld-plugin/
├── pom.xml
└── src
    ├── main
    │   ├── java
    │   │   └── io
    │   │       └── jenkins
    │   │           └── plugins
    │   │               └── sample
    │   │                   └── HelloWorldBuilder.java
    │   └── resources
    │       ├── index.jelly
    │       └── io
    │           └── jenkins
    │               └── plugins
    │                   └── sample
    │                       ├── HelloWorldBuilder
    │                       │   ├── config.jelly
    │                       │   ├── config.properties
    │                       │   ├── config_de.properties
    │                       │   ├── config_es.properties
    │                       │   ├── config_fr.properties
    │                       │   ├── config_it.properties
    │                       │   ├── config_pt_BR.properties
    │                       │   ├── config_sv.properties
    │                       │   ├── config_tr.properties
    │                       │   ├── config_zh_CN.properties
    │                       │   ├── help-name.html
    │                       │   ├── help-name_de.html
    │                       │   ├── help-name_es.html
    │                       │   ├── help-name_fr.html
    │                       │   ├── help-name_it.html
    │                       │   ├── help-name_pt_BR.html
    │                       │   ├── help-name_sv.html
    │                       │   ├── help-name_tr.html
    │                       │   ├── help-name_zh_CN.html
    │                       │   ├── help-useFrench.html
    │                       │   ├── help-useFrench_de.html
    │                       │   ├── help-useFrench_es.html
    │                       │   ├── help-useFrench_fr.html
    │                       │   ├── help-useFrench_it.html
    │                       │   ├── help-useFrench_pt_BR.html
    │                       │   ├── help-useFrench_sv.html
    │                       │   ├── help-useFrench_tr.html
    │                       │   └── help-useFrench_zh_CN.html
    │                       ├── Messages.properties
    │                       ├── Messages_de.properties
    │                       ├── Messages_es.properties
    │                       ├── Messages_fr.properties
    │                       ├── Messages_it.properties
    │                       ├── Messages_pt_BR.properties
    │                       ├── Messages_sv.properties
    │                       ├── Messages_tr.properties
    │                       └── Messages_zh_CN.properties
    └── test
        └── java
            └── io
                └── jenkins
                    └── plugins
                        └── sample
                            └── HelloWorldBuilderTest.java

19 directories, 41 files
liumiaocn:plugin liumiao$

生成的pom.xml文件的内容如下所示,也可以根据需要进行手工生成

liumiaocn:plugin liumiao$ cat helloworld-plugin/pom.xml 
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.jenkins-ci.plugins</groupId>
        <artifactId>plugin</artifactId>
        <version>3.50</version>
        <relativePath />
    </parent>
    <groupId>io.jenkins.plugins</groupId>
    <artifactId>helloworld-plugin</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>hpi</packaging>
    <properties>
        <!-- Baseline Jenkins version you use to build the plugin. Users must have this version or newer to run. -->
        <jenkins.version>2.164.3</jenkins.version>
        <java.level>8</java.level>
        <!-- Other properties you may want to use:
          ~ jenkins-test-harness.version: Jenkins Test Harness version you use to test the plugin. For Jenkins version >= 1.580.1 use JTH 2.0 or higher.
          ~ hpi-plugin.version: The HPI Maven Plugin version used by the plugin..
          ~ stapler-plugin.version: The Stapler Maven plugin version required by the plugin.
     -->
    </properties>
    <name>TODO Plugin</name>
    <!-- The default licence for Jenkins OSS Plugins is MIT. Substitute for the applicable one if needed. -->
    <licenses>
        <license>
            <name>MIT License</name>
            <url>https://opensource.org/licenses/MIT</url>
        </license>
    </licenses>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <!-- Pick up common dependencies for 2.164.x: https://github.com/jenkinsci/bom#usage -->
                <groupId>io.jenkins.tools.bom</groupId>
                <artifactId>bom-2.164.x</artifactId>
                <version>3</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.jenkins-ci.plugins</groupId>
            <artifactId>structs</artifactId>
        </dependency>
        <dependency>
            <groupId>org.jenkins-ci.plugins.workflow</groupId>
            <artifactId>workflow-cps</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.jenkins-ci.plugins.workflow</groupId>
            <artifactId>workflow-job</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.jenkins-ci.plugins.workflow</groupId>
            <artifactId>workflow-basic-steps</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.jenkins-ci.plugins.workflow</groupId>
            <artifactId>workflow-durable-task-step</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <!-- If you want this to appear on the wiki page:
    <developers>
      <developer>
        <id>bhacker</id>
        <name>Bob Q. Hacker</name>
        <email>bhacker@nowhere.net</email>
      </developer>
    </developers> -->

    <!-- Assuming you want to host on @jenkinsci:
    <url>https://github.com/jenkinsci/${project.artifactId}-plugin</url>
    <scm>
        <connection>scm:git:git://github.com/jenkinsci/${project.artifactId}-plugin.git</connection>
        <developerConnection>scm:git:git@github.com:jenkinsci/${project.artifactId}-plugin.git</developerConnection>
        <url>https://github.com/jenkinsci/${project.artifactId}-plugin</url>
    </scm>
    -->
    <repositories>
        <repository>
            <id>repo.jenkins-ci.org</id>
            <url>https://repo.jenkins-ci.org/public/</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>repo.jenkins-ci.org</id>
            <url>https://repo.jenkins-ci.org/public/</url>
        </pluginRepository>
    </pluginRepositories>
</project>
liumiaocn:plugin liumiao$

使用mvn package或者mvn clean package进行构建即可,执行日志如下所示:

liumiaocn:plugin liumiao$ ls
helloworld-plugin
liumiaocn:plugin liumiao$ cd helloworld-plugin/
liumiaocn:helloworld-plugin liumiao$ mvn package
[INFO] Scanning for projects...
...省略
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running InjectedTest
[INFO] Tests run: 23, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 11.212 s - in InjectedTest
[INFO] Running io.jenkins.plugins.sample.HelloWorldBuilderTest
[INFO] Tests run: 5, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 38.492 s - in io.jenkins.plugins.sample.HelloWorldBuilderTest
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 28, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] 
[INFO] --- maven-license-plugin:1.8:process (default) @ helloworld-plugin ---
[INFO] Generated /Users/liumiao/jenkins/plugin/helloworld-plugin/target/helloworld-plugin/WEB-INF/licenses.xml
[INFO] 
[INFO] --- maven-hpi-plugin:3.9:hpi (default-hpi) @ helloworld-plugin ---
[INFO] Generating /Users/liumiao/jenkins/plugin/helloworld-plugin/target/helloworld-plugin/META-INF/MANIFEST.MF
[INFO] Checking for attached .jar artifact ...
[INFO] Generating jar /Users/liumiao/jenkins/plugin/helloworld-plugin/target/helloworld-plugin.jar
[INFO] Building jar: /Users/liumiao/jenkins/plugin/helloworld-plugin/target/helloworld-plugin.jar
[INFO] Exploding webapp...
[INFO] Copy webapp webResources to /Users/liumiao/jenkins/plugin/helloworld-plugin/target/helloworld-plugin
[INFO] Assembling webapp helloworld-plugin in /Users/liumiao/jenkins/plugin/helloworld-plugin/target/helloworld-plugin
[INFO] Generating hpi /Users/liumiao/jenkins/plugin/helloworld-plugin/target/helloworld-plugin.hpi
[INFO] Building jar: /Users/liumiao/jenkins/plugin/helloworld-plugin/target/helloworld-plugin.hpi
[INFO] 
[INFO] --- maven-jar-plugin:3.1.2:test-jar (maybe-test-jar) @ helloworld-plugin ---
[INFO] Skipping packaging of the test-jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 04:38 min
[INFO] Finished at: 2019-11-21T05:22:40+08:00
[INFO] ------------------------------------------------------------------------
liumiaocn:helloworld-plugin liumiao$

可以看到,已经生成了helloworld-plugin.hpi,使用此文件即可进行安装了。

liumiaocn:helloworld-plugin liumiao$ ls
pom.xml src     target
liumiaocn:helloworld-plugin liumiao$ ls target
classes                helloworld-plugin      jenkins-for-test       test-classes
generated-sources      helloworld-plugin.hpi  maven-status           tmp
generated-test-sources helloworld-plugin.jar  surefire-reports
liumiaocn:helloworld-plugin liumiao$ 

参考内容

https://jenkins.io/doc/developer/tutorial/create/

淼叔 CSDN认证博客专家 神经网络 TensorFlow NLP
资深架构师,PMP、OCP、CSM、HPE University讲师,EXIN DevOps Professional与DevOps Master认证讲师,曾担任HPE GD China DevOps & Agile Leader,帮助企业级客户提供DevOps咨询培训以及实施指导。熟悉通信和金融领域,有超过十年金融外汇行业的架构设计、开发、维护经验,在十几年的IT从业生涯中拥有了软件开发设计领域接近全生命周期的经验和知识积累,著有企业级DevOps技术与工具实战。
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页