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 

<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.0modelVersion>
    <parent>
        <groupId>org.jenkins-ci.pluginsgroupId>
        <artifactId>pluginartifactId>
        <version>3.50version>
        <relativePath />
    parent>
    <groupId>io.jenkins.pluginsgroupId>
    <artifactId>helloworld-pluginartifactId>
    <version>1.0-SNAPSHOTversion>
    <packaging>hpipackaging>
    <properties>
        
        <jenkins.version>2.164.3jenkins.version>
        <java.level>8java.level>
        
    properties>
    <name>TODO Pluginname>
    
    <licenses>
        <license>
            <name>MIT Licensename>
            <url>https://opensource.org/licenses/MITurl>
        license>
    licenses>
    <dependencyManagement>
        <dependencies>
            <dependency>
                
                <groupId>io.jenkins.tools.bomgroupId>
                <artifactId>bom-2.164.xartifactId>
                <version>3version>
                <scope>importscope>
                <type>pomtype>
            dependency>
        dependencies>
    dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.jenkins-ci.pluginsgroupId>
            <artifactId>structsartifactId>
        dependency>
        <dependency>
            <groupId>org.jenkins-ci.plugins.workflowgroupId>
            <artifactId>workflow-cpsartifactId>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>org.jenkins-ci.plugins.workflowgroupId>
            <artifactId>workflow-jobartifactId>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>org.jenkins-ci.plugins.workflowgroupId>
            <artifactId>workflow-basic-stepsartifactId>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>org.jenkins-ci.plugins.workflowgroupId>
            <artifactId>workflow-durable-task-stepartifactId>
            <scope>testscope>
        dependency>
    dependencies>

    

    
    <repositories>
        <repository>
            <id>repo.jenkins-ci.orgid>
            <url>https://repo.jenkins-ci.org/public/url>
        repository>
    repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>repo.jenkins-ci.orgid>
            <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/

你可能感兴趣的:(#,Jenkins,#,Jenkins插件)