Preserving argument spacing, etc when passing into mvn exec:java - bash

I have a shell script that launches a Maven exec:java process -
exec mvn exec:java -Dexec.mainClass=... -Dexec.args="$*"
Now sadly if I run
./myMagicShellScript arg1 "arg 2"
the single string arg 2 doesn't make it through as a single argument as I'd like.
Any thoughts as to how to escape / pass things through properly (perferably in a clean way)?

I took a look at the mvn script and did some testing. This is what I came up with:
Try changing your script to look like this:
args=(${#// /\\ })
exec mvn exec:java -Dexec.mainClass=... -Dexec.args="${args[*]}"
That changes all spaces which are within each array element to be escaped with a backslash.

Related

Bash script pass arguments to maven

I have a bash script called runMain that is shown below:
#!/bin/bash
mvn exec:java -Dexec.mainClass=org.fjx.main.Main -f fjxRepo/pom.xml -Dexec.args="$#"
So I want to pass the arguments of the bash script to the maven exec. Even though this works when there is only one argument, it does not work for more than one argument e.g. 2 arguments.
Please help me fix this.
If you want to pass the whole argument string and none of the arguments have whitespace in them, you can use $* instead of $#.

Bash script: passing a "string with empty space" onto a command

I have following bash script:
set -o xtrace
TEST_CASE=#Login
mvn clean install --Dcucumber.options="--tags ${TEST_CASE}"
When i run the script the mvn command become:
mvn clean install '-Dcucumber.options=--tags #Login'
Note that the single quote is wrapping the whole -D argument, and the original double quote has disappear. But what i want to see is below:
mvn clean install -Dcucumber.options="--tags #Login"
What is wrong with my original script? how should I update to make it right?
The quotes are not part of the argument; they are there solely to prevent the shell from breaking the string beginning with --D into two arguments to be passed to mvn. The following are all equivalent, creating three arguments:
mvn clean install --Dcucumber.options="--tags ${TEST_CASE}"
mvn clean install "--Dcucumber.options=--tags ${TEST_CASE}"
mvn clean install --Dcucumber.options=--tags" ${TEST_CASE}"
The following creates 4 arguments, since the whitespace between tags and the parameter is not quoted:
mvn clean install --Dcucumber.options=--tags "${TEST_CASE}"
You can quote the space individually in a variety of ways to preserve the 3-argument command. (Remember, adjacent strings are automatically concatenated):
mvn clean install --Dcucumber.options=--tags" ""${TEST_CASE}"
mvn clean install --Dcucumber.options=--tags' '"${TEST_CASE}"
mvn clean install --Dcucumber.options=--tags\ "${TEST_CASE}"
For consistency, set -x always shows a string that would need to be quoted using '...' and quoting the entire string, not just a portion of it.
Is the bash script not working? I'd say use single quotes for literal strings and double quotes for strings that are going to be expanded.
TEST_CASE='#Login'
mvn clean install --Dcucumber.options="--tags ${TEST_CASE}"
What are the error messages?
I think problem is that the quotes aren't actually being included int he cucumber options. So Try this as well:
TEST_CASE='#Login'
CUCE_OPTS='"--tags '${TEST_CASE}'"'
mvn clean install --Dcucumber.options=${CUCE_OPTS}

Cannot access mvn command from bash script

I want to run the following script as:
bash ./scripts/startapp.sh
Here is the script:
#!/bin/bash
str=$HOSTNAME
PREV_IFS=$IFS
IFS=. components=(${str})
ORIGINAL_DIRECTORY=`pwd`
REP_DIRECTORY=$ORIGINAL_DIRECTORY/src/main/resources
for part in "${components[#]}"
do
PATH=$REP_DIRECTORY/"$part"
REP_DIRECTORY=$PATH
done
IFS=$PREV_IFS
CONFIG_PATH=$REP_DIRECTORY/application.yaml
# Below is the final command I want to run from the terminal
`SPRING_CONFIG_LOCATION=$CONFIG_PATH mvn spring-boot:run`
I am getting
mvn: command not found
Without starting the script, I can use mvn spring-boot:run without any problem.
Make sure you have defined:
M2_HOME pointing to the base directory of your Maven installation
PATH must include $M2_HOME/bin
In your script you're overwriting the value of PATH on every iteration. You should change it to:
PATH=$PATH:$REP_DIRECTORY/"$part"

Escaping double quotes in exec java in Maven while executing through command line

I am trying to execute a Maven exec command as follows:
mvn exec:java -Dexec.args="{\"parameters\":[{\"name\":\"parameter1\",\"value\":\"1\"}]}"
The argument is a JSON string which I need to process in my Java program. But when the string is read by the Java class, the double quotes are removed and it reads as:
{parameters:[{name:parameter1,value:1}]}
I have tried different escape sequences like \\ and \\\, but it doesn't work. I need to preserve the double quotes in the string.
Could someone please help me resolve this?
Try this instead:
mvn exec:java -Dexec.args="'{\"parameters\":[{\"name\":\"parameter1\",\"value\":\"1\"}]}'"
try to use " instead " (double quotes)

Convert a list of bash arguments to a quoted single string

I'm trying to create a simple bash script to run a mvn command with some specific arguments and "sub" arguments, using the exec plugin. Seems simple, but I'm having problems to achieve the desired effect:
here is the command to run:
mvn exec:java -Dexec.args="-a val1 -b val2 -c val3"
it works fine when run like this, but I want to wrap it in a bash script, easy to run for everyone:
myapp -a val1 -b val2 -c val3
The script would be as easy as:
mvn exec:java -Dexec.args="$#"
But "$#" doesn't make it a single quoted value, so I tried to quote it:
mvn exec:java -Dexec.args=\""$#"\"
I also tried a second script I called quoter:
./myapp \""$#"\"
This doesn't work either, after a good combination playing time around, it feels like no matter how many times I quote it, it still treats it as a list of arguments, being the first element "-a, then the second element val1, then third -b, and so on.
curiously, if I don't quote the arguments in the quoter script, and run it like this:
./quoter "-a val1 -b val2 -c val3"
It works fine, it is passed as a single parameter to the second script and then to the first script executing the maven command.
It seems like, "$#" is not quite a string made of the stringficated concatenation of all the parameters (which is cool if you think about it), nor echoing it seems to convert it into one (not cool). Is there a way to make this possible?
PS: yes I'm trying to pass options to the main program, which is a heavily dependent spring standalone application.
man bash...
Special Parameters
The shell treats several parameters specially. These parameters may only be
referenced; assignment to them is not allowed.
* Expands to the positional parameters, starting from one. When the expansion
occurs within double quotes, it expands to a single word with the value of
each parameter separated by the first character of the IFS special variable.
That is, "$*" is equivalent to "$1c$2c...", where c is the first character
of the value of the IFS variable. If IFS is unset, the parameters are
separated by spaces. If IFS is null, the parameters are joined without
intervening separators.
Ok, after some more hours I found a solution, here it is. I created whole new script so I'll start from the scratch.
Create a simple script that the final user will execute and pass the parameters as a normal command-line app:
$./myapp -a val1 -b val2 -c val3
which will contain the following:
mvn exec:java -Dexec.args="$*"
The difference between $* and $#, is that $# will pass the arguments as a list and will also do some formatting to those values, like removing quotes and so. In the other hand, $* will treat all the arguments as a single one, grouping them and letting you pass them altogether as a string value to the inner command.

Resources