文章目录
第一部分:照葫芦画瓢
1. 首先使用命令在本地创建项目骨架
交互方式
mvn archetype:generate -Dfilter=io.jenkins.archetypes:
或非交互方式(可以修改-DartifactId后的项目名)
mvn archetype:generate -B -DarchetypeGroupId=io.jenkins.archetypes -DarchetypeArtifactId=hello-world-plugin -DhostOnJenkinsGitHub=true -DarchetypeVersion=1.7 -DartifactId=hello
结果如下:
2. 用idea打开并启动项目
用idea打开项目,等待右下角下载依赖组件。然后点击右上角,配置运行方式“Edit Configurations
”。
点Debug启动,看到下图三个地方说明成功了
然后访问jenkins,创建个任务,在配置的地方可以看到我们的插件效果。
说明:
这时候,会直接在你本地运行一个jenkins,然后我们的测试插件会被装上。这个jenkins是独立于项目外的,你可以安装其他插件,或者修改全局配置。idea项目重启,之前的配置、插件都还在。另外如果你通过idea的“Edit Configurations”配置为Debug模式,那么你修改页面和方法内的代码是可以立即生效的,不需要重启。
第二部分:遇到的问题
1. mvn命令创建archetype失败
提示[WARNING] No archetype found in remote catalog. Defaulting to internal catalog
我就是卡在了这里,怎么也下不下来。几天后我找到了解决办法,参考jenkinsci/archetypes的git地址上的说明,使用非交互方式构建
mvn archetype:generate -B -DarchetypeGroupId=io.jenkins.archetypes -DarchetypeArtifactId=hello-world-plugin -DhostOnJenkinsGitHub=true -DarchetypeVersion=1.7 -DartifactId=hello
2. maven依赖下不下来
国内镜像也配了,但是不行,我后来是手动去网站上下载的。https://mvnrepository.com/搜索到后,点“View All”,页面打开,下载后放到本地。
3. 启动报错
可以尝试在Termial(终端)中执行mvn hpi:run
命令。
4. 怎么修改Jenkins版本
pom.xml中有两个地方:
<jenkins.version>2.289.3</jenkins.version>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.jenkins.tools.bom</groupId>
<artifactId>bom-2.289.x</artifactId>
<version>937.v51fde92016ed</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
注意第二个里面,artifactId和version都要改。