0

things I tried in my script run.sh:

mvn exec:java -Dexec.mainClass=my.App -Dexec.args="$@"
mvn exec:java -Dexec.mainClass=my.App -Dexec.args=$@

when I call the script like:

./run.sh arg1 arg2 arg3

the arguments are not passed to Java application. instead Maven tries to interpret them as if they were maven options.

EDIT: I used -x option. I see this:

mvn exec:java -Dexec.mainClass=my.App -Dexec.args=arg1 arg2 arg3

How can I get it to evaluate to:

mvn exec:java -Dexec.mainClass=my.App -Dexec.args="arg1 arg2 arg3"

that will fix my problem. I tried many variations, can't get it to work.

2
  • What is the output of debug using YourShell -x? Commented Sep 22, 2023 at 23:39
  • 1
    If none of the arguments can contain spaces or other funny characters, this might be one of the very few places you want "$*" instead of "$@" (i.e. -Dexec.args="$*" -- see my answer here). If the arguments might need quoting or something similar it's more compicated. bash has a way to quote things in a bash-compatible way, but this probably requires different quoting/escaping syntax (and I don't know what that syntax is). Commented Sep 23, 2023 at 0:11

1 Answer 1

2

When the shell expands -Dexec.args="$@" and the list of positional parameters is arg1, arg2, arg3, then you get the list -Dexec.args=arg1, arg2, arg3 (three separate arguments for mvn). The "$@" expansion is always a list, and the result of concatenating that with a string, as you do in your code, is not actually well defined (most shells behave as I describe here).

What you likely want is "$*" in place of "$@". The expansion of "$*" is always a single string made up of the elements of the list of positional parameters, concatenated with the first character from $IFS (usually a space) as the delimiter. With -Dexec.args="$*" you get the string -Dexec.args=arg1 arg2 arg3 (one single argument for mvn).

So:

mvn exec:java -Dexec.mainClass=my.App -Dexec.args="$*"

Note that if any argument contains spaces, it will be difficult for mvn to parse it correctly unless the tool supports some special escaping, quoting, or other encoding for handling that (and you use that in the argument list). I'm not a Maven user, so I don't know how it parses the exec.args value.

See also What is the difference between $* and $@?

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .