Search code examples
shellmavenmvn-repo

shorten maven command in shell script


I have created a shell script which executes maven command.

The command looks like this:

mvn --ntp  gatling:test -Dgatling.simulationClass=scripts.$SIMULATION_CLASS -Denv=${TEST_ENVIRONMENT} -Dscenario="ownerPairing"  -Dgatling.core.directory.results="/home/vsts/work/r1/a/_digital-key-core-gatling/gatlingResults" -Dgatling.core.outputDirectoryBaseName=dkc_gatling_test_emea_dev_$SIMULATION_CLASS -Dgatling.charting.noReports=true -DclientPath=${clientPath} -DclientPWD=${CLIENT_CERTIFICATES_PASSWORD} -DuseProxy=false -Dusers=${USERS} -DrampUpTime=${DURATION} -DexecuteFrom="pipeline"

Is there a way, I am assign all the inputs into a variable and access it in mvn command.

I tried something like this but didnt work:

mavenInput1="-Dgatling.simulationClass=scripts.$SIMULATION_CLASS -Denv=${TEST_ENVIRONMENT} -Dscenario="ownerPairing"  
mavenInput2=-Dgatling.core.directory.results="/home/vsts/work/r1/a/_digital-key-core-gatling/gatlingResults" -Dgatling.core.outputDirectoryBaseName=dkc_gatling_test_emea_dev_$SIMULATION_CLASS -Dgatling.charting.noReports=true 
mavenInput3=-DclientPath=${clientPath} -DclientPWD=${CLIENT_CERTIFICATES_PASSWORD} -DuseProxy=false -Dusers=${USERS} -DrampUpTime=${DURATION} -DexecuteFrom="pipeline"
mvn --ntp  gatling:test ${mavenInput1} ${mavenInput2} ${mavenInput3}

Solution

  • It is all about quotes.

    mavenInput1='-Dgatling.simulationClass=scripts.'"${SIMULATION_CLASS}"' -Denv='"${TEST_ENVIRONMENT}"' -Dscenario=ownerPairing'  
    mavenInput2='-Dgatling.core.directory.results=/home/vsts/work/r1/a/_digital-key-core-gatling/gatlingResults -Dgatling.core.outputDirectoryBaseName=dkc_gatling_test_emea_dev_'"${SIMULATION_CLASS}"' -Dgatling.charting.noReports=true'
    mavenInput3='-DclientPath='"${clientPath}"' -DclientPWD='"${CLIENT_CERTIFICATES_PASSWORD}"' -DuseProxy=false -Dusers='"${USERS}"' -DrampUpTime='"${DURATION}"' -DexecuteFrom=pipeline'
    
    mvn --ntp  gatling:test ${mavenInput1} ${mavenInput2} ${mavenInput3}
    

    You could also make them as a single variable like so:

    mavenInputs='-Dgatling.simulationClass=scripts.'"${SIMULATION_CLASS}"\
    ' -Denv='"${TEST_ENVIRONMENT}"\
    ' -Dscenario=ownerPairing'\
    ' -Dgatling.core.directory.results=/home/vsts/work/r1/a/_digital-key-core-gatling/gatlingResults'\
    ' -Dgatling.core.outputDirectoryBaseName=dkc_gatling_test_emea_dev_'"${SIMULATION_CLASS}"\
    ' -Dgatling.charting.noReports=true'\
    ' -DclientPath='"${clientPath}"\
    ' -DclientPWD='"${CLIENT_CERTIFICATES_PASSWORD}"\
    ' -DuseProxy=false'\
    ' -Dusers='"${USERS}"\
    ' -DrampUpTime='"${DURATION}"\
    ' -DexecuteFrom=pipeline'
    
    mvn --ntp  gatling:test ${mavenInputs}