1

I'm trying to use this maven command in a Jenkinsfile

mvn -q -Dexec.executable=echo -Dexec.args='${project.version}' --non-recursive exec:exec

I put this command in a variable in my jenkinsfile to use it later this way

def myCommand = 'mvn -q -Dexec.executable=echo -Dexec.args=\"${project.version}\" --non-recursive exec:exec'

...

def version = sh(${myCommand})

My problem is that Jenkins don't correctly escape my '${project.version}' and outputs java.lang.NoSuchMethodError: No such DSL method '$' found among steps

How do I correctly include '${project.version}' as a string in my command variable ?

matt
  • 19
  • 5
  • In most interpretive languages, including this one, `'` denotes string literals and `"` denotes string interpolation. You want the latter, but are using the former. Sorry to say, but this is also a question with at least ten duplicates on SO right now. As a side node, in string literals you do not need to escape a `"` within the string like you are doing, but that point is moot for your given usage. – Matthew Schuchard Mar 15 '19 at 13:34
  • 1
    I recommend to use: `mvn org.apache.maven.plugins:maven-help-plugin:3.1.1:evaluate -Dproject.version -DforceStdout -q` instead of exec plugin... – khmarbaise Mar 15 '19 at 13:41

2 Answers2

0

There's a problem with single quote in groovy - it doesn't substitute the variables. This should work:

def myCommand = "mvn -q -Dexec.executable=echo -Dexec.args='${project.version}' --non-recursive exec:exec"

More on the quotes: What's the difference of strings within single or double quotes in groovy?

hakamairi
  • 4,464
  • 4
  • 30
  • 53
  • Nope... Now it's trying to resolve project inside the Jenkinsfile `groovy.lang.MissingPropertyException: No such property: project for class: groovy.lang.Binding` – matt Mar 15 '19 at 13:58
  • No... Your solution prevent the pipeline from running. I tried to echo mine and I got the same output : `java.lang.NoSuchMethodError: No such DSL method '$' found among steps ` – matt Mar 15 '19 at 14:48
0

Just replace the single quotes with double quotes:

def myCommand = "mvn -q -Dexec.executable=echo -Dexec.args=\"${project.version}\" --non-recursive exec:exec"
uncletall
  • 6,609
  • 1
  • 27
  • 52
  • It's not working : Jenkins tries to resolve project `groovy.lang.MissingPropertyException: No such property: project for class: groovy.lang.Binding` I'm not trying to inject project.version, I'm trying to include ${project.version} in the myCommand var. – matt Mar 15 '19 at 15:24