Jenkins流水线使用@Grab 导入Maven库

有个需求需要在pipeline中调用Java的SDK去执行业务

使用 @Grab 注解可以在Maven中导入Java 库,

@Grab('org.apache.commons:commons-math3:3.4.1')
import org.apache.commons.math3.primes.Primes

引入依赖后,在通过import导入

另外的写法

@Grab(group='org.springframework', module='spring-orm', version='5.2.8.RELEASE')
import org.springframework.jdbc.core.JdbcTemplate

指定maven仓库

@GrabResolver(name='restlet', root='http://maven.restlet.org/')
@Grab(group='org.restlet', module='org.restlet', version='1.1.6')

引入多个包

@Grapes([
   @Grab(group='commons-primitives', module='commons-primitives', version='1.0'),
   @Grab(group='org.ccil.cowan.tagsoup', module='tagsoup', version='0.9.7')
])

这样就可以在流水线中使用java的包

@Grab('com.test.sdk:1.0.0')
@GrabResolver(name = 'share-lib', root = 'http://localhost:8083/repository/share-lib/')
import com.test.sdk

pipeline {
     stages {
        stage('call sdk') {
                echo "调用SDk"
                def sdk = new sdk()
                def result = sdk.get($param)
                echo "${result}
            }
        }
    }
}

默认情况下,下载的包缓存在 ~/.groovy/grapes/中,我在测试过程中,发现拉取一次后,后续就不会去拉取依赖了,即使有更新也不会去拉取

https://docs.groovy-lang.org/latest/html/documentation/grape.html#_quick_start

posted @ 2024-02-26 15:02  阿弱  阅读(27)  评论(0编辑  收藏  举报