问题描述
首先,我是 Java、Groovy 和 Jenkins 的新手,所以请耐心等待;)
First, I′m new to Java, Groovy and Jenkins so please be patient with me ;)
我正在准备一个支持流水线的 Jenkins 服务器,以备将来在我们的构建环境中使用.我们使用一种特殊的内部脚本语言,为此我必须用 java 编写一个包装器.没有选项只能在 Groovy 中完成这项工作,我们必须使用这种特殊的语言.
I′m preparing a Jenkins server with Pipeline support for future use in our build environment. We use a special inhouse scripting language for which i have to write a wrapper in java. There is no option to do the work only in Groovy, we have to use this special language.
我尝试了很多方法来将 java 库引用到这个 jenkins 项目,但都没有奏效.主要是我使用了 https://github.com/jenkinsci/上的文档workflow-cps-global-lib-plugin 来实现这一点,但也尝试了几种搜索 google 或 stackoverflow 的方法.按照文档,这应该是可能的.
I have tried many methods of referencing the java lib to this jenkins project but neither worked. Mainly i′ve used the documentation on http://www.51sjk.com/Upload/Articles/1/0/336/336019_20221103095527350.jpg to implement this but also tried several approaches searching google or stackoverflow. Following the documentation, this include should be possible.
出于测试目的,我已将流程简化为测试设置.
I′ve reduced the process to a test setup for testing purposes.
假设以下...
我在 Jenkins 中有一个名为MultibranchTestProject01"的多分支项目.
Jenkins 文件:
I have a multibranch project in Jenkins named 'MultibranchTestProject01'.
The Jenkinsfile:
@Library('DeltaJenkinsScripts@develop') def runStageCollect = true if (runStageCollect) { stage("Collect") { helloWorld("Joe") } }
引用的库通过 Jenkins 设置中的全局管道库"全局引用,但在此处也明确地进行了说明.它托管在 git 环境中,并且引用似乎有效.该库的文件结构:
The referenced library is referenced globally via 'Global Pipeline Libraries' in the Jenkins settings but also explicitly here to clarify things. It′s hosted in a git environment and the referencing seems to work. The file structure of this library:
/vars/helloWorld.groovy
/vars/helloWorld.groovy
package de.dcomp.prod def call(name) { def tt = new Test() tt.testText() }
/src/de/dcomp/prod/Test.groovy
/src/de/dcomp/prod/Test.groovy
package de.dcomp.prod import de.dcomp.ftel.* def testText() { def sRetVal = "" echo "testText - START" //sRetVal = ScriptRunner.GetStaticSampleText() def oSR = new ScriptRunner() sRetVal = oSR.GetInstanceSampleText() echo "ReturnValue: ${sRetVal}" }
我有一个名为 ScriptRunner-0.0.1-SNAPSHOT.jar 的 java 库.这个库只有一个类:
I have a java lib called ScriptRunner-0.0.1-SNAPSHOT.jar. This library has a single class:
package de.dcomp.ftel; public class ScriptRunner { public String GetInstanceSampleText() { return "ScriptRunner.GetInstanceSampleText() called..."; } public static String GetStaticSampleText() { return "ScriptRunner.GetStaticSampleText() called..."; } }
我在**的 java 项目中引用和使用这个库没有问题.
I have no problem in referencing and using this library in a standalone java project.
我尝试了几种方法来包含它:
I′ve tried several ways to include it:
- 将jar文件放到'C:Userscr.groovylib'
- 在测试 linux 环境中设置类路径.
- 使用插件管道:类路径步骤"以不同的符号将库添加到类路径中,例如'C:Userscr.groovylib', C:/Users/cr/.groovy/lib', 'C:Userscr.groovylibScriptRunner-0.0.1-SNAPSHOT.jar', 'C:/Users/cr/.groovy/lib/ScriptRunner-0.0.1-SNAPSHOT.jar', 'file:///C:/Users/cr/.groovy/lib/ScriptRunner-0.0.1-SNAPSHOT.jar'
- 将 lib 添加到本地 maven 存储库并根据 @GrabResolver 和 @Grab 进行引用,尽管这不是我想要的解决方案
或动态加载:
this.class.classLoader.rootLoader.addURL(new URL("file:///C:/Users/cr/.groovy/lib/ScriptRunner-0.0.1-SNAPSHOT.jar")); def srClass = Class.forName("de.dcomp.ftel.ScriptRunner") def sr = srClass.newInstance()
结果总是这样.
groovy.lang.MissingPropertyException: No such property: ScriptRunner for class: de.dcomp.prod.Test
或者这个:
de/dcomp/prod/Test.groovy: 10: unable to resolve class ScriptRunner @ line 10, column 12. def oSR = new ScriptRunner()
错误消息总是指向进程找不到 Java 库的方向.如果我尝试使用其他库,也会发生同样的事情,例如来自 Apache Commons.
The error messages point always in the direction that the process cannot find the Java library. The same thing happened if i try to use some other library, e.g. from Apache Commons.
如果可能的话,我想避免把它写成插件.
I would like to avoid writig it as a plugin if this is possible.
提前致谢!
推荐答案
到目前为止我发现的唯一可行的方法是在管道中运行它以找出正在检查的目录:
The only method I've found so far that works was to run this in the pipeline to find out what directories are being checked:
println System.getProperty("java.ext.dirs")
就我而言,它正在寻找
/usr/java/packages/lib/ext
所以我把要加载的 jar 放在那个位置(必须创建目录之后),然后重新启动 Jenkins.
So I put the jar I wanted to load in that location (after having to create the directory), and then restarted Jenkins.
之后我成功地导入并使用了该库.
Afterwards I was successfully able to do an import of the library and use it.
看起来很hacky,可能会被视为错误并在不通知的情况下删除.
Seems very hacky and the sort of thing that might be considered a bug and removed without notice.