Jenkins Shared Library 添加第三方包支持
背景
我们在写 Jenkins 的 Shared Library 时,有时候需要引用外部的一些 jar 包,比如 maven central 的一些 lib 等。
具体到我们的例子,需要引用 Gson 做 json 序列化。
问题
我们的 Shared Library 中有如下代码,用到了 Gson:
import com.google.gson.Gson
/**
* @author wxweven
*/
class JsonUtils {
static final Gson GSON = new Gson()
static String toJson(User user) {
return GSON.toJson(user)
}
}
但是在执行的时候,Jenkins 报错:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: JsonUtils.groovy: 3: unable to resolve class com.google.gson.Gson @ line 3, column 1. import com.google.gson.Gson
原因就是 Jenkins 无法找到 Gson 依赖。因为 Jenkins 系统默认是没有 Gson jar 包的。
添加 Grab 支持
那么怎么给 Jenkins 的 Shared Library 添加第三方包支持呢?
Jenkins 官方文档中有说明:Using third-party libraries。
添加 @Grab 注解即可,示例如下:
@Grab('org.apache.commons:commons-math3:3.4.1')
import org.apache.commons.math3.primes.Primes
void parallelize(int count) {
if (!Primes.isPrime(count)) {
error "${count} was not prime"
}
// …
}
结合我们的代码,添加 @Grab 注解后的版本如下:
@Grab('com.google.code.gson:gson:2.8.6')
import com.google.gson.Gson
/**
* @author wxweven
*/
class JsonUtils {
public static final Gson GSON = new Gson()
static String toJson(User user) {
return GSON.toJson(user)
}
}
这样就可以给 Jenkins 中添加 Gson 的 jar 包支持了。
Grab 的语法,跟 maven 的 pom 比较类似,默认的格式是:groupId:artifactId:version。
关于 Grab 的更多用法,请参考:官方文档
默认下载的 jar 包,会缓存在 Master 节点的 ~/.groovy/grapes/ 目录下。
结束语
我是梅小西,最近在某东南亚电商公司做 DevOps 的相关事情。从本期开始,将陆续分享基于 Jenkins 的 CI/CD 工作流,包括 Jenkins On k8s 等。
如果你对 Java 或者 Jenkins 等感兴趣,欢迎与我联系,微信:wxweven(备注 DevOps)
我是梅小西,最近在某东南亚电商公司做 DevOps 的相关事情。从本期开始,将陆续分享基于 Jenkins 的 CI/CD 工作流,包括 Jenkins On k8s 等。
如果你对 Java 或者 Jenkins 等感兴趣,欢迎与我联系,微信:wxweven(备注 DevOps)
本文由博客群发一文多发等运营工具平台 OpenWrite 发布
Jenkins Shared Library 添加第三方包支持的更多相关文章
- yum安装的Nginx添加第三方模块支持tcp
需求:生产有个接口是通过socket通信.nginx1.9开始支持tcp层的转发,通过stream实现的,而socket也是基于tcp通信. 实现方法:Centos7.2下yum直接安装的nginx, ...
- 解决pycharm添加第三方包失败
今天想用pycharm打开图像,但是import scipy的时候报错了,报错内容如下: from scipy.misc import imread Traceback (most recent ca ...
- 解决eclipse中自带的maven搜索不到非本地第三方包问题
解决eclipse中自带的maven搜索不到非本地第三方包问题 版权声明:本文为博主原创文章,未经博主允许不得转载. 最近使用eclipse中的maven插件时发现,在pom.xml文件中添加第 ...
- Eclipse / android studio 添加第三方jar包 步骤
eclipse 将第三方包放到libs文件夹后并没有引用. 基本步骤分为3步,具体介绍如下: 打开自己的Eclipse,在自己的Android工程上名上右键->Build Path ->C ...
- 关于如何正确地在android项目中添加第三方jar包
在android项目中添加第三方jar包虽然不是一个很复杂的问题,但是确实给很多开发者带来了不小的困扰.我自己就曾经碰到过calss not found exception.error inflati ...
- Jenkins pipeline shared library
Jenkinsfile https://jenkins.io/doc/book/pipeline/jenkinsfile/ Jenkins Pipeline is a suite of plugins ...
- 在 C Level 用 dlopen 使用 第三方的 Shared Library (.so)
http://falldog7.blogspot.com/2013/10/android-c-level-dlopen-shared-library-so.html 在 Android 裡,撰寫 JN ...
- 代码补全——Vim/Neovim中YouCompleteMe添加第三方库的支持
参考链接: https://github.com/ycm-core/YouCompleteMe#c-family-semantic-completion https://cloud.tencent.c ...
- 使用Sonatype Nexus搭建Maven私服后如何添加第三方JAR包?
Sonatype Nexus简介 登录Nexus后,点击右侧的“Repositories”,显示当前Nexus所管理的Repository: 默认情况下Nexus为我们创建了以下主要的Reposito ...
- Android 添加第三方jar包
1,拷贝jar包到项目的\app\libs文件夹下. 2,打开项目下的build.gradle(Module:app)文件,在“dependencies {}”中添加“compile files('l ...
随机推荐
- SciTech-Mathmatics-Probability and Statistics: Differencing "mind"/"language"/"Concept"/"ideal"/"Context"/"notation"/"Symbol"/"Term"/"Axiom"/"Definition"/&
SciTech-Mathmatics-Probability and Statistics: Differencing: "mind"/"language"/& ...
- Intuitive Vs Conceptive : Set/Number/Measure Theory + Real Analysis: Lebesgue Measure and Integration
Lebesgue's post-doctory thesis: Measure, length and area; Joseph Louis Bertrand's three different va ...
- SciTech-BigDataAIML-Measurement: Euclidian Distance + Manhattan Distance + Area面积 + Density密度 + KLD(KL散度):测度比较"两Distribution(概率分布)"的Similarity(接近度)
Measurement 测度 Euclidian Distance(欧几理得距离) \(\large \begin{array}{rl} \\ EuDistance(Point_1, Point_2) ...
- Edu-English-Phonetic-IPA:国际音标发音学:英语音标的学习神器,终于找到
https://mp.weixin.qq.com/s?__biz=MzU3NTIzOTA5OA==&mid=2247493736&idx=1&sn=8ed10241adeaa1 ...
- ES 7.8 速成笔记(中)
接上篇继续,本篇主要研究如何查询 一.sql方式查询 习惯于数据库开发的同学,自然最喜欢这种方式.为了方便讲解,先写一段代码,生成一堆记录 package com.cnblogs.yjmyzz; im ...
- 基础篇:6.8)形位公差-公差带 Tolerance Zone
本章目的:了解14个形位公差的公差带形状,其从属关系. 1.定义 公差带-实际被测要素允许变动的区域. 它体现了对被测要素的设计要求,也是加工和检验的根据. 2.公差带四大特征-形状.大小.方向.位置 ...
- 如何关闭win10自动更新?怎么永久关闭的问题
有不少使用win10正式版的雨林木风官网用户,说这系统的自动更新真的好烦,不是更新系统蓝屏,就是系统崩溃,而且每次更新都关机开机都很慢.小伙伴就问了,就没有什么永久关闭win10 自动更新的办法吗?当 ...
- CF ROUND946 (DIV. 3)E
Money Buys Happiness 题面翻译 你是一个物理学家.一开始你没有钱,每个月的末尾你会得到 \(x\) 英镑.在第 \(i\) 个月里,你可以付出 \(c_i\) 英镑,获取 \(h_ ...
- 使用 Claude Code 的自定义 Sub Agent 完善博文写作体验
Claude Code ! Claude Code ! 停不下来了~ 两天前, Claude Code 可以自定义自己的 AI Agent 了 -> https://docs.anthropic ...
- PPT(五)-编辑顶点
编辑顶点和合并形状这两个其实是不可分割的两兄弟,合并形状时将多个形状进行组合剪切掏空等变成一个形状.编辑顶点是针对某个形状进行特别的调整 一.编辑顶点怎么玩 (1)先要有一个形状 选中形状->右 ...