简体   繁体   English

在其他Jenkins groovy脚本中使用groovy类

[英]Using a groovy class in other jenkins groovy scripts

I have 4 groovy scripts (2 are dsl.groovy scripts): 我有4个groovy脚本(其中2个是dsl.groovy脚本):

JobConfig.groovy : JobConfig.groovy

class JobConfig {
    final name

    JobConfig(map) {
        name = map['name']
    }
}

topLevel.groovy : topLevel.groovy

import JobConfig.*

def doSmthWithJobConfig(final JobConfig config) {
    println(config.name);
}

sublevel1.dsl.groovy : sublevel1.dsl.groovy

GroovyShell shell = new GroovyShell()
def topLevelScript = shell.parse(new File("topLevel.groovy"))

def jobConfigs = [
    new JobConfig(name: 'JenkinsTestDSLs'),
    new JobConfig(name: 'JenkinsTestDSLs2')
]

jobConfigs.each {
    topLevelScript.doSmthWithJobConfig(it);
}

sublevel2.dsl.groovy : sublevel2.dsl.groovy

GroovyShell shell = new GroovyShell()
def topLevelScript = shell.parse(new File("topLevel.groovy"))

def jobConfigs = [
    new JobConfig(name: 'JenkinsTestDSLs3'),
    new JobConfig(name: 'JenkinsTestDSLs4')
]

jobConfigs.each {
    topLevelScript.doSmthWithJobConfig(it);
}

Now if locally I do: 现在,如果在本地我这样做:

groovyc JobConfig.groovy

,I get no issues with running the scripts locally. ,我在本地运行脚本没有任何问题。

But on jenkins even if I provide the JobConfig.class at the same place where these scripts are, I can't get it running. 但是在jenkins上,即使我在这些脚本所在的位置提供JobConfig.class,也无法使其运行。 I read here that I don't need to do any compiling as long as JobConfig.groovy is on the CLASSPATH. 在这里读到,只要JobConfig.groovy在CLASSPATH上,我就不需要进行任何编译。 How do I do that with jenkins? 我如何用詹金斯做到这一点? Or is there another solution? 还是有其他解决方案?

If you don't want to compile the groovy class(es) and then add the compiled classes to the classpath, you can make use of classes within groovy scripts like this: 如果您不想编译groovy类,然后将已编译的类添加到类路径中,则可以在groovy脚本中使用如下类:

Given a groovy class 给定一个普通的班级

class GroovyClass {
    def GroovyClass(someParameter) {}

    def aMethod() {}
}

you can use the class in groovy script like this 您可以像这样在groovy脚本中使用该类

import hudson.model.*
import java.io.File;
import jenkins.model.Jenkins;

def jenkinsRootDir = build.getEnvVars()["JENKINS_HOME"];
def parent = getClass().getClassLoader()
def loader = new GroovyClassLoader(parent)

def someParameterValue = "abc"

def groovyClass = loader.parseClass(new File(jenkinsRootDir + "/userContent/GroovyScripts/GroovyClass.groovy")).newInstance(someParameterValue)

groovyClass.aMethod()

What I would do is following, instead of trying to make it all work manually, I would create maven project or gradle one and organize your groovy files within resulted project. 我将要做的是,而不是尝试使其全部手动工作,我将创建maven项目或gradle一个项目并在结果项目中组织您的常规文件。 When configure maven or gradle to run it. 配置maven或gradle使其运行时。 Once maven or gradle runs it without any issues, you can give it to jenkins. 一旦maven或gradle毫无问题地运行它,您就可以将其交给jenkins。

Jenkins takes maven projects for sure and for gradle you will have to check, however one thing for sure, is maven runs you groovy project locally it will run without any issues under Jenkins as well. Jenkins肯定会接受Maven项目,而gradle则需要检查,但是可以肯定的是,Maven在本地运行您的groovy项目,它在Jenkins下也不会出现任何问题。

Please try, let me know. 请尝试,让我知道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM