从命令行运行时,如何通过Spring Boot应用中的命令行放电
我正在尝试从命令行运行Spring Boot应用程序,并通过Commnd Line参数。我尝试了几种方法: -
Try 1: mvn spring-boot:run -DCALLBACK_PORT="8000"
Try 2: mvn spring-boot:run -D CALLBACK_PORT="8000"
Try 3: mvn spring-boot:run -DargLine="CALLBACK_PORT=8000"
Try 4: mvn -DargLine="CALLBACK_PORT=8000" spring-boot:run
在所有情况下,应用程序运行。我试图将其读取为: -
String evnCallBackPort = System.getenv("CALLBACK_PORT");
System.out.println("CALLBACK_PORT: "+evnCallBackPort);
它打印callback_port:null
如何使用此命令行参数运行它?
I am trying to run spring boot app from command line and pass a commnd line argument. I tried several ways none of the works:-
Try 1: mvn spring-boot:run -DCALLBACK_PORT="8000"
Try 2: mvn spring-boot:run -D CALLBACK_PORT="8000"
Try 3: mvn spring-boot:run -DargLine="CALLBACK_PORT=8000"
Try 4: mvn -DargLine="CALLBACK_PORT=8000" spring-boot:run
In all case the app runs. I am trying to read it as:-
String evnCallBackPort = System.getenv("CALLBACK_PORT");
System.out.println("CALLBACK_PORT: "+evnCallBackPort);
It prints CALLBACK_PORT: null
How do I run it with this commandline argument?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,您应该将以下配置添加到POM文件中。
在POM文件中,您可以按Environment Variables参数来定义应用程序的环境变量: https://docs.spring.io/spring-boot/doc/doc/docs/current/maven-plugin/maven-plugin/reference/htmlsing/htmlsingle/htmlsingle/wgoals-un-parameter--run-parameters-run-parameters-run-parameters-run-parameters-run-parameters-run-parameters-run-parameters-run-parameters-run-parameters-run-parameters-run-parameters-run-parameters-详细信息 - arguments
其次,在运行应用程序时,在命令行中添加相应的参数以填充POM文件中的占位符,在此示例中,它是“ $ {env.callbackport}”参数为-denv.callbackport =“ 3221”,例如以下命令行:
您可以参考示例项目 https://github.com/bluezealot/mvnparam/tree/master/master/java2ets
上面命令行的输出是,请注意输出“ callback_port:3221”:
Firstly, you should add the following configuration into your pom file.
In the pom file you define the environment variables of you application by environmentVariables parameter.ref: https://docs.spring.io/spring-boot/docs/current/maven-plugin/reference/htmlsingle/#goals-run-parameters-details-arguments
Secondly, when you run your application, add the corresponding argument in your command line to fill in the placeholder in the pom file, in this example it is "${env.callbackport}" the correponding command line argument is -Denv.callbackport="3221" like the following command line:
You can refer to the sample project https://github.com/bluezealot/mvnparam/tree/master/java2ets
The output of above command line is, note the output "CALLBACK_PORT: 3221":
对于任何想要通过属性(而不是env vars)的人,语法略有不同( source ):
也可以通过命令行进行此操作,这比上面的配置优先:
For anyone looking to pass Properties (instead of env vars), the syntax is slightly different (source):
Also, it is possible to do it via command line, which takes precedence over the configuration above: