2016-07-03 5 views
1

Ich verwende die Workflow Remote Loader Plugin Funktion, um Abhängigkeiten für mein Build-Skript zu laden. In meiner Haupt-Datei, ich habe folgende:Jenkins Build Pipeline fileLoad in Bibliotheksdateien funktioniert nicht

build-app.groovy

def java 
def util 

node { 
    stage "Setup" 
    fileLoader.withGit('[email protected]:myorg/build-scripts.git', 'master', '234720asdf8ed1a2', '') { 

     java = fileLoader.load('lib/java.groovy'); 
     util = fileLoader.load('lib/util.groovy'); 
    } 

    util.checkIfFileExists('myFile.txt') 
    def version = java.getJarVersion("api") 
} 

util.checkIfFileExists() funktioniert perfekt, aber ein Problem tritt auf, wenn java.getJarVersion genannt wird. In getJarVersion besteht auch eine Abhängigkeit von util. Meine Java-lib ist wie folgt:

java.groovy

def util = fileLoader.load('lib/util.groovy') 

def getJarVersion() { 
    ... 
    util.checkIfFileExists('myFile.txt') 
    ... 
} 

Dieses wird folgende Fehler gibt, wenn ich in Jenkins laufen:

groovy.lang.MissingPropertyException: No such property: util for class: groovy.lang.Binding 
    at groovy.lang.Binding.getVariable(Binding.java:62) 
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:224) 
    at org.kohsuke.groovy.sandbox.impl.Checker$4.call(Checker.java:241) 
    at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:238) 
    at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.getProperty(SandboxInvoker.java:23) 
    at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:17) 
    at Script4.checkIfFileExists(Script4.groovy:39) 
    at Script4.getJarVersion(Script4.groovy:23) 
    at WorkflowScript.run(WorkflowScript:18) 
    at ___cps.transform___(Native Method) 
    at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.get(PropertyishBlock.java:62) 
    at com.cloudbees.groovy.cps.LValueBlock$GetAdapter.receive(LValueBlock.java:30) 
    at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.fixName(PropertyishBlock.java:54) 
    at sun.reflect.GeneratedMethodAccessor254.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72) 
    at com.cloudbees.groovy.cps.impl.ConstantBlock.eval(ConstantBlock.java:21) 
    at com.cloudbees.groovy.cps.Next.step(Next.java:58) 
    at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:154) 
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access$001(SandboxContinuable.java:18) 
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable$1.call(SandboxContinuable.java:32) 
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable$1.call(SandboxContinuable.java:29) 
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.GroovySandbox.runInSandbox(GroovySandbox.java:108) 
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:29) 
    at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:164) 
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:276) 
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$000(CpsThreadGroup.java:78) 
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:185) 
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:183) 
    at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:47) 
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) 
    at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:112) 
    at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28) 
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) 
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) 
    at java.lang.Thread.run(Thread.java:745) 

Die doc Plugin sagt:

Use static initializers within the Groovy file of the loaded file to load more context from neighbor files.

Ich habe versucht static def util = fileLoader.load('lib/util.groovy'), aber das erzeugte den gleichen Fehler. Ich habe auch eine Vielzahl anderer Kombinationen vergeblich versucht. Vielleicht ist das ein Mangel an Verständnis für die groovige Sprache? Nicht wirklich sicher.

Vielen Dank im Voraus!

+0

Try '@groovy.transform.Field def util = fileLoader.load ('lib/util.groovy') ' –

+0

diese Gerade versucht und jetzt' java.getJarVersion' kann nicht von meiner Haupt-Build-Datei aufgerufen werden (Original-Post aktualisiert, um zu zeigen, wie es heißt): 'java.lang.NoSuchMethodError: Keine solche DSL-Methode 'getJarVersion' gefunden' – threejeez

+0

Wenn _static initializer_ in Groovy ist das gleiche wie in Java dann deklariert es nicht eine Variable _static_ wie bei 'static def util'. Es ist eine Abkürzung für [_Static Initialization Block_] (https://docs.oracle.com/javase/tutorial/java/javaOO/initial.html). –

Antwort

2

OK, nach viel Versuch und Irrtum, habe ich das Problem herausgefunden.

java.groovy vor:

def util = fileLoader.load('lib/util.groovy') 

def getJarVersion() { 
    ... 
    util.checkIfFileExists('myFile.txt') 
    ... 
} 

return this; 

java.groovy nach:

def getJarVersion() { 
    ... 
    util.checkIfFileExists('myFile.txt') 
    ... 
} 

this.util = fileLoader.load('lib/util.groovy') 
return this; 

Die return this; Linie von der ursprünglichen Nachricht weggelassen wurde, weil es in der Dokumentation der Remote-Datei loader-Plugin war, aber es ist der Schlüssel. Wenn java.groovy von build.groovy geladen wird, verwendet es den zurückgegebenen this als den Bereich, der die Aufrufe an java.groovy aufruft. Dieser Bereich enthält util nicht, wenn es als def util = fileLoader.load('lib/util.groovy') definiert wurde. Fügen Sie es vor der Rückgabe hinzu und legen Sie util für alle Funktionsaufrufe in der Datei ab.

0

Wenn Sie von Google eine ähnliche Fehlermeldung erhalten, müssen Sie das Pipeline Remote Loader Plugin installieren. Sonst wird es Ihnen geben:

groovy.lang.MissingPropertyException: No such property: fileLoader for class: groovy.lang.Binding 
Verwandte Themen