如何在Groovy Jenkins管道中使用全局外部Java库中的方法? [英] How to use methods from a global external java library in a Groovy Jenkins Pipeline?

查看:835
本文介绍了如何在Groovy Jenkins管道中使用全局外部Java库中的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我刚接触Java,Groovy和Jenkins,请耐心等待;)

我准备一个Jenkins服务器,提供管道支持在我们的构建环境中使用。
我们使用一种特殊的内部脚本语言,为此我必须在java中编写一个包装器。没有选择只在Groovy中完成这项工作,我们必须使用这种特殊语言。



我已经尝试了许多方法将java lib引用到这个jenkins项目中,但是既没有工作。
主要是我使用了 https:// github上的文档。 com / jenkinsci / workflow-cps-global-lib-plugin 来实现这一点,但也尝试了几种搜索google或者stackoverflow的方法。按照文档,这应该是可能的。



我已经将测试过程减少到测试设置以进行测试。

假设以下...

我在Jenkins中有一个名为'MultibranchTestProject01'的多分项目。

Jenkinsfile:

  @Library('DeltaJenkinsScripts @ develop')

def runStageCollect = true

if(runStageCollect)
{
stage(Collect)
{
helloWorld(Joe)
}
}

引用的库是通过Jenkins设置中的全局管道库全局引用的,但也明确指出了这些内容。
它在git环境中托管,引用似乎工作。
这个库的文件结构:

/vars/helloWorld.groovy

  package de.dcomp.prod 

def call(name){
def tt = new Test()
tt.testText()
}

/src/de/dcomp/prod/Test.groovy

  package de.dcomp.prod 

import de.dcomp.ftel。*

def testText()
{
def sRetVal =
echotestText - START
// sRetVal = ScriptRunner.GetStaticSampleText()
def oSR = new ScriptRunner()
sRetVal = oSR.GetInstanceSampleText()
echoReturnValue:$ {sRetVal}
}

我有一个名为ScriptRunner-0.0.1-SNAPSHOT.jar的java库。这个库有一个类:

  package de.dcomp.ftel; 

public class ScriptRunner
{
public String GetInstanceSampleText()
{
returnScriptRunner.GetInstanceSampleText()called ...;

public static String GetStaticSampleText()
{
returnScriptRunner.GetStaticSampleText()called ...;




$ b

我在引用和使用这个库时没有问题一个独立的Java项目。



我试过几种方法来包含它:


  • 将jar文件放在'C:\Users\cr.groovy\lib'中

  • 在测试的linux环境中设置Classpath。

  • 使用插件Pipeline:Classpath Steps以不同的表示法将库添加到类路径中,例如'C:\Users\cr.groovy\lib',C:/Users/cr/.groovy/lib','C:\Users\cr.groovy\lib\ScriptRunner-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存储库并引用per @GrabResolver和@Grab,尽管这不是我想要的解决方案。 >


或动态加载:

  this .class.classLoader.rootLoader.addURL(新的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:无法解析ScriptRunner类
@第10行,第12列。
def oSR = new ScriptRunner()

错误消息始终指向进程找不到Java库。如果我尝试使用其他一些库,例如来自Apache Commons。



如果可以的话,我想避免将它作为插件来书写。



谢谢

解决方案

到目前为止,我发现的唯一方法是在管道中运行这个方法来找出什么目录正在被检查:

  println System.getProperty(java.ext.dirs)

在我的案例中,它正在寻找

  / usr / java / packages / lib / ext 

(在创建目录之后),然后重新启动Jenkins。



之后,我成功地能够导入库并使用它。



看起来很冒险,可能会被认为是一个错误,并会在没有通知的情况下被移除。


First, I´m new to Java, Groovy and Jenkins so please be patient with me ;)

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.

I have tried many methods of referencing the java lib to this jenkins project but neither worked. Mainly i´ve used the documentation on https://github.com/jenkinsci/workflow-cps-global-lib-plugin 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.

Assume the following...

I have a multibranch project in Jenkins named 'MultibranchTestProject01'.
The Jenkinsfile:

@Library('DeltaJenkinsScripts@develop')

def runStageCollect = true

if (runStageCollect)
{
    stage("Collect")
    {
        helloWorld("Joe")
    }
}

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

package de.dcomp.prod

def call(name) {
    def tt = new Test()
    tt.testText()
}

/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}"
}

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...";
    }
}

I have no problem in referencing and using this library in a standalone java project.

I´ve tried several ways to include it:

  • Put the jar file to 'C:\Users\cr.groovy\lib'
  • Setting the Classpath in a testing linux environment.
  • Using the plugin "Pipeline: Classpath Steps" to add the library to the classpath in different notations, e.g. 'C:\Users\cr.groovy\lib', C:/Users/cr/.groovy/lib', 'C:\Users\cr.groovy\lib\ScriptRunner-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'
  • adding the lib to a local maven repository and referencing per @GrabResolver and @Grab, though this is not the solution i would like to have

or dynamic loading with:

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()

The result is always something like this.

groovy.lang.MissingPropertyException: No such property: ScriptRunner for class: de.dcomp.prod.Test

or this:

de/dcomp/prod/Test.groovy: 10: unable to resolve class ScriptRunner 
 @ line 10, column 12.
    def oSR = new ScriptRunner()

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.

Thanks in advance!

解决方案

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")

And in my case, it was looking in

/usr/java/packages/lib/ext

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.

Seems very hacky and the sort of thing that might be considered a bug and removed without notice.

这篇关于如何在Groovy Jenkins管道中使用全局外部Java库中的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆