How to call a helper method from inside another helper method

1,030 views
Skip to first unread message

michael....@sandybeachtech.com

unread,
Mar 8, 2016, 4:34:02 PM3/8/16
to job-dsl-plugin
Taking the following example.  maybe someone can help me with my error.

JOBDSL.groovy

job {

  doConditionalMavenStep(delegate, MAVENGOALS, MAVENVERSION)
  commonMavenStep(delegate, OTHERMAVENGOALS, MAVENVERSION)

}

StepsUtil.groovy

class StepsUtil {

  public static void doConditionalMavenStep(context, String mavenGoals, String mavenVersion) {
    context.with {
            conditionalSteps {
                condition {
                    booleanCondition('MYTOKEN')
                }
                runner('DontRun')
                steps {
                    commonMavenStep(delegate, mavenVersion, mavenGoals)
                }
            }
        }
  }

  public commonMavenStep(context, String mavenGoals, String mavenVersion) {
    context.with {
      maven {
                  mavenInstallation(mavenVersion)
                  goals(mavenGoals)
             }
    }
  }
}

Fails with:

Expected no exception to be thrown, but got 'javaposse.jobdsl.dsl.DslScriptException'
    at spock.lang.Specification.noExceptionThrown(Specification.java:118)
    at JobScriptsSpec.test script #file.name(JobScriptsSpec.groovy:32)
Caused by: javaposse.jobdsl.dsl.DslScriptException: (StepsUtil.groovy, line 29) No such property: context for class: javaposse.jobdsl.dsl.helpers.step.StepContext
    at javaposse.jobdsl.dsl.DslScriptLoader.runDslEngineForParent(DslScriptLoader.groovy:60)
    at javaposse.jobdsl.dsl.DslScriptLoader.runDslEngine(DslScriptLoader.groovy:112)
    at javaposse.jobdsl.dsl.DslScriptLoader.runDslEngine(DslScriptLoader.groovy:108)
    at JobScriptsSpec.test script #file.name(JobScriptsSpec.groovy:29)
Caused by: groovy.lang.MissingPropertyException: No such property: context for class: javaposse.jobdsl.dsl.helpers.step.StepContext

I'm guess that passing it delegate in this case is not correct.  But passing it context fails in much the same way as well.  Really not sure what to pass it.

Thx for any help.

Daniel Spilker

unread,
Mar 8, 2016, 4:46:10 PM3/8/16
to job-dsl...@googlegroups.com
It's not possible to tell what's StepsUtil.groovy line 29 in your code snippted, but commonMavenStep should be a static method.

--
You received this message because you are subscribed to the Google Groups "job-dsl-plugin" group.
To unsubscribe from this group and stop receiving emails from it, send an email to job-dsl-plugi...@googlegroups.com.
To post to this group, send email to job-dsl...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/job-dsl-plugin/7b8a5197-e3c7-481e-9d7a-6e02bdef677f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Matt Sheehan

unread,
Mar 8, 2016, 4:47:27 PM3/8/16
to job-dsl-plugin
Both of your methods should be static. Something like this should work:

class StepsUtil {

 
static doConditionalMavenStep(context, String mavenGoals, String mavenVersion) {

    context
.with {
      conditionalSteps
{
        condition
{
          booleanCondition
('MYTOKEN')
       
}
        runner
('DontRun')
        steps
{
          commonMavenStep
(delegate, mavenVersion, mavenGoals)
       
}
     
}
   
}
 
}


 
static commonMavenStep(context, String mavenGoals, String mavenVersion) {
    context
.with {
      maven
{
        mavenInstallation
(mavenVersion)
        goals
(mavenGoals)
     
}
   
}
 
}
}

job
('test') {
  steps
{
   
StepsUtil.doConditionalMavenStep(delegate, 'MAVENGOALS', 'MAVENVERSION')
   
StepsUtil.commonMavenStep(delegate, 'OTHERMAVENGOALS', 'MAVENVERSION')
 
}
}

michael....@sandybeachtech.com

unread,
Mar 8, 2016, 5:00:09 PM3/8/16
to job-dsl-plugin
Line 29 is commonMavenStep(delegate, mavenVersion, mavenGoals) from the doConditionalMavenStep

All methods are static void (my mistake in typing it out)

Error is still the same, I updated your code to look more like what i'm doing.

Expected no exception to be thrown, but got 'javaposse.jobdsl.dsl.DslScriptException'
	at spock.lang.Specification.noExceptionThrown(Specification.java:118)
	at JobScriptsSpec.test script #file.name
(JobScriptsSpec.groovy:32)
Caused by: javaposse.jobdsl.dsl.DslScriptException: (StepsUtil.groovy, line 29) No signature of method: javaposse.jobdsl.dsl.helpers.step.StepContext.commonMavenStep() is applicable for argument types: (javaposse.jobdsl.dsl.helpers.step.StepContext, java.lang.String, java.lang.String) values: [javaposse.jobdsl.dsl.helpers.step.StepContext@230f5ade, ...]
	at javaposse.jobdsl.dsl.DslScriptLoader.runDslEngineForParent(DslScriptLoader.groovy:60)
	at javaposse.jobdsl.dsl.DslScriptLoader.runDslEngine(DslScriptLoader.groovy:112)
	at javaposse.jobdsl.dsl.DslScriptLoader.runDslEngine(DslScriptLoader.groovy:108)
	at JobScriptsSpec.test script #file.name(JobScriptsSpec.groovy:29)
Caused by: groovy.lang.MissingMethodException: No signature of method: javaposse.jobdsl.dsl.helpers.step.StepContext.commonMavenStep() is applicable for argument types: (javaposse.jobdsl.dsl.helpers.step.StepContext, java.lang.String, java.lang.String) values: [javaposse.jobdsl.dsl.helpers.step.StepContext@230f5ade, ...]





class StepsUtil {

  
static doConditionalMavenStep(context, String mavenGoals, String mavenVersion) {

    context
.with {
      conditionalSteps 
{
        condition 
{
          booleanCondition
('MYTOKEN')
        
}
        runner
('DontRun')
        steps 
{
          commonMavenStep
(delegate, mavenVersion, mavenGoals)
        
}
      
}
    
}
  
}


  
static commonMavenStep(context, String mavenGoals, String mavenVersion) {
    context
.with {
      maven 
{
        mavenInstallation
(mavenVersion)
        goals
(mavenGoals)
      
}
    
}
  
}
}

mavenJob
('test') {
  preBuildSteps 
{

    
StepsUtil.doConditionalMavenStep(delegate, 'MAVENGOALS', 'MAVENVERSION')
    
StepsUtil.commonMavenStep(delegate, 'OTHERMAVENGOALS', 'MAVENVERSION')
  
}
}

Matt Sheehan

unread,
Mar 8, 2016, 11:32:10 PM3/8/16
to job-dsl-plugin
Strange. The runs fine for me in the playground. Maybe try calling in with StepsUtil.commonMavenStep

michael....@sandybeachtech.com

unread,
Mar 9, 2016, 1:42:58 AM3/9/16
to job-dsl-plugin
Yes, you are correct,  In the playground this works without a hitch.  But on my machine it won't.  I updated the example to be exactly what I have.  Three files are involved Util.groovy, StepsUtil.groovy and jobsdsl.groovy

The highlighted line is line 29 from the error.

class Util {
 
 
public Util() {}
 
 
static void commonMavenStep(context, String mavenGoals, String mavenVersion) {
    context
.with {
      maven
{
        mavenInstallation
(mavenVersion)
        goals
(mavenGoals)
        configure
{
           it
/ 'usePrivateRepository'('true')
       
}
     
}
   
}
 
}
}




class StepsUtil extends Util{
 
 
public StepsUtil() {}



 
static void doConditionalMavenStep(context, String mavenGoals, String mavenVersion) {


    context
.with {
      conditionalSteps
{
        condition
{
          booleanCondition
('MYTOKEN')
       
}
        runner
('DontRun')
        steps
{
         
commonMavenStep(delegate, mavenVersion, mavenGoals)
       
}
     
}
   
}
 
}




 
}




def jobs = [:]


jobs
['build'] = mavenJob('test') {

  preBuildSteps
{


   
StepsUtil.doConditionalMavenStep(delegate, 'MAVENGOALS', 'MAVENVERSION')

   
Util.commonMavenStep(delegate, 'OTHERMAVENGOALS', 'MAVENVERSION')
 
}
}


jobs
.values().each { mavenJob ->
    mavenJob
.jdk('JDK1.7')


}



On Tuesday, March 8, 2016 at 4:34:02 PM UTC-5, meggl...@gmail.com wrote:
Reply all
Reply to author
Forward
0 new messages