
[英]How to use @Library in an imported groovy script in Jenkins declarative pipeline?
[英]Jenkins | Use method from an imported java class in a groovy script
我希望能够通过其 java 类使用 Jenkins 插件中的方法
只是要指出我不是开发人员或 groovy/java 专家 - 很高兴学习!
我的方法所属的 java 类是com.cloudbees.jenkins.plugins.bitbucket.BitbucketSCMNavigator
从这里我想使用方法getRepoOwner()
我所做的是设置我的导入并定义一个对类的新调用:
import com.cloudbees.jenkins.plugins.bitbucket.BitbucketSCMNavigator
def bbSCMNav = new BitbucketSCMNavigator()
当我运行它时,我收到以下错误:
org.codehaus.groovy.runtime.metaclass.MethodSelectionException: Could not find which method <init>() to invoke from this list:
public com.cloudbees.jenkins.plugins.bitbucket.BitbucketSCMNavigator#<init>(java.lang.String)
public com.cloudbees.jenkins.plugins.bitbucket.BitbucketSCMNavigator#<init>(java.lang.String, java.lang.String, java.lang.String)
我已经搜索了上面的错误Could not find which method <init>() to invoke from this list
我遇到了这张票无法找到要从这个列表中在 groovy 闭包中的 newInstance 上调用哪个方法 <init>()
不能说我完全理解回复是否对我有帮助,因为我说我不是开发人员,groovy 和 java 对我来说相对较新,但很高兴理解是否有人能指出我正确的方向
本练习的目标是在构建的运行时使用该方法来获取getRepoOwner()
的输出并在变量中使用它来构造 URI
这个问题似乎也与我的类似 - Calling internal methods of Jenkins plugin (thinBackup)
但我没有在这里使用 maven 或 pom.xml
干杯
此错误无法找到哪个方法 < init >()与缺少的构造函数相关。
几乎所有内部 jenkins 类都可以在 groovy 中使用。
在您的情况下,BitbucketSCMNavigator 没有默认构造函数。 它有一个带有一个 String 参数的构造函数。 检查这一行
我可以用另一个内部类org.jenkinsci.plugins.workflow.cps.CpsGroovyShellFactory复制你的错误:
node {
stage('internal') {
org.jenkinsci.plugins.workflow.cps.CpsGroovyShellFactory obj =
new org.jenkinsci.plugins.workflow.cps.CpsGroovyShellFactory();
}
}
hudson.remoting.ProxyException: org.codehaus.groovy.runtime.metaclass.MethodSelectionException: Could not find which method <init>() to invoke from this list: private org.jenkinsci.plugins.workflow.cps.CpsGroovyShellFactory#<init>(org.jenkinsci.plugins.workflow.cps.CpsFlowExecution, boolean, java.lang.ClassLoader, java.util.List)
但是,查看此类CpsFlowExecution我可以看到 CpsGroovyShellFactory 没有默认构造函数。 它有一个带一个参数的构造函数:CpsGroovyShellFactory(this)
因此,如果我使用一个参数实例化构造函数,则不会出现错误。
node {
stage('internal') {
org.jenkinsci.plugins.workflow.cps.CpsGroovyShellFactory obj =
new org.jenkinsci.plugins.workflow.cps.CpsGroovyShellFactory(null);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.