问题
Can any one tell how to run a jar file using java -jar command without knowing the Jar's full name, I know the prefix but the version of the jar file is dynamic,so I don't want to update my sh for every deplolyment
#!/bin/bash
PROJECT_HOME=/opt/services/testing-batchp
PROJECT_JAR=batch-1.0.8-SNAPSHOT.jar
[ -f /etc/environment ] && . /etc/environment
nohup java -jar -Dspring.config.location=${PROJECT_HOME}/config/ ${PROJECT_HOME}/${PROJECT_JAR} $1 $2 >/dev/null 2>&1 &
I just modified the variable PROJECT_JAR="batch*.*-SNAPSHOT.jar" but its not working. Here the version of the jar will change in each deployment. Please help
回答1:
Try the below approach for executing script without mentioning the jar's release version.
#!/bin/bash
PROJECT_HOME=/opt/services/testing-batchp
PROJECT_JAR=batch-*.*.jar
[ -f /etc/environment ] && . /etc/environment
nohup java -jar -Dspring.config.location=${PROJECT_HOME}/config/ ${PROJECT_HOME}/${PROJECT_JAR} $1 $2 >/dev/null 2>&1 &
Make sure that only one valid jar is there in the deployment folder.
回答2:
You can try setting the dynamic variable as below;
PROJECT_JAR=`ls -r batch*.*-SNAPSHOT.jar | head -1`
This must do the job. However, please note the ticks(`) enclosing the command to enable substitution.
来源:https://stackoverflow.com/questions/61361064/can-run-a-jar-file-without-knowing-the-jar-file-name