从命令行运行时如何在spring boot应用程序中传递命令行参数

马西布

我正在尝试从命令行运行 spring boot 应用程序并传递一个命令行参数。我尝试了几种方法都不起作用:-

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

如何使用此命令行参数运行它?

狂热者

首先,您应该将以下配置添加到您的 pom 文件中。

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <environmentVariables>
                <CALLBACK_PORT>${env.callbackport}</CALLBACK_PORT>
                </environmentVariables>
            </configuration>
        </plugin>
    </plugins>
</build>

在 pom 文件中,您通过 environmentVariables parameter.ref 定义应用程序的环境变量:https ://docs.spring.io/spring-boot/docs/current/maven-plugin/reference/htmlsingle/#goals-run-parameters -details-arguments

其次,当你运行你的应用程序时,在你的命令行中添加相应的参数来填充pom文件中的占位符,在这个例子中是“${env.callbackport}”,对应的命令行参数是-Denv.callbackport= “3221”类似于以下命令行:

mvn spring-boot:run -Denv.callbackport="3221"

您可以参考示例项目https://github.com/bluezealot/mvnparam/tree/master/java2ets上面命令行的输出是,注意输出“CALLBACK_PORT: 3221”:

$ mvn spring-boot:run -Denv.callbackport="3221"
/usr/lib/jvm/java-11-openjdk-amd64/bin/java
[INFO] Scanning for projects...
[INFO] 
[INFO] -------------------< com.hoperun.java2ets:java2ets >--------------------
[INFO] Building java2ets 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] >>> spring-boot-maven-plugin:2.6.4:run (default-cli) > test-compile @ java2ets >>>
[INFO] 
[INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ java2ets ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] Copying 0 resource
[INFO] Copying 1 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ java2ets ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:3.2.0:testResources (default-testResources) @ java2ets ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ java2ets ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] <<< spring-boot-maven-plugin:2.6.4:run (default-cli) < test-compile @ java2ets <<<
[INFO] 
[INFO] 
[INFO] --- spring-boot-maven-plugin:2.6.4:run (default-cli) @ java2ets ---
[INFO] Attaching agents: []
20:57:14.223 [main] INFO com.hoperun.java2ets.java2ets.Java2etsApplication - args: 0
20:57:14.231 [main] INFO com.hoperun.java2ets.java2ets.Java2etsApplication - CALLBACK_PORT: 3221

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.6.4)

2022-04-20 20:57:14.702  INFO 99391 --- [           main] c.h.j.java2ets.Java2etsApplication       : Starting Java2etsApplication using Java 11.0.14.1 on qxz-ubuntu with PID 99391 (/home/qinxizhou/work/jtekt/mvnparam/java2ets/target/classes started by qinxizhou in /home/qinxizhou/work/jtekt/mvnparam/java2ets)
2022-04-20 20:57:14.703  INFO 99391 --- [           main] c.h.j.java2ets.Java2etsApplication       : No active profile set, falling back to 1 default profile: "default"
2022-04-20 20:57:14.917  INFO 99391 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2022-04-20 20:57:14.918  INFO 99391 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data Redis repositories in DEFAULT mode.
2022-04-20 20:57:14.929  INFO 99391 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 3 ms. Found 0 Redis repository interfaces.
2022-04-20 20:57:15.212  INFO 99391 --- [           main] c.h.j.java2ets.Java2etsApplication       : Started Java2etsApplication in 0.913 seconds (JVM running for 1.135)
2022-04-20 20:57:15.213  INFO 99391 --- [           main] c.h.java2ets.java2ets.EntryService       : Console Start---
2022-04-20 20:57:15.214  INFO 99391 --- [           main] c.h.java2ets.java2ets.EntryService       : Console End---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.144 s
[INFO] Finished at: 2022-04-20T20:57:15+08:00
[INFO] ------------------------------------------------------------------------

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

从命令行运行时出现ModuleNotFoundError

从命令行运行而不是在Netbeans IDE中运行时,Maven Java应用程序崩溃

从命令行运行时,如何告诉赛普拉斯隐藏Chrome?

Spring Boot应用程序:将应用程序拆分为要从命令行运行的单独任务?

在命令行上运行时Spring Boot应用程序挂起

为什么从命令行运行时导入失败,而从PyCharm运行则导入成功?

如何从命令行运行.NET Core控制台应用程序

如何从命令行运行GUI应用程序?

如何部署从命令行运行的Javascript应用程序?

如何从命令行运行 delphi VCL 应用程序

如何从命令行运行 MSIX 安装的应用程序

如何从命令行运行程序?

如何在运行时从命令行更改KVM VNC端口?

Gradle - 从命令行运行时 Corda 流测试失败

从命令行运行时混合Java和Kotlin代码的ClassNotFoundException

从命令行运行时忽略单个PHPCS规则

StartupObject 从命令行运行时的行为与修改 CSPROJ 文件时的行为不同

尝试从命令行运行时出现NoClassDefFoundError

从命令行运行时,Powershell脚本未按预期记录日志

从命令行运行时,使用退出代码“ 1”终止Blender

从命令行运行时,mstest v2 忽略了 ExpectedException

从命令行运行时,Docker容器不起作用

Swift:从命令行运行时,UI 测试看不到资产

从命令行运行时如何隐藏“快速比较”对话框窗口?

从命令行运行PackageMaker

从命令行运行功能

从命令行运行Scheme

无法从命令行运行

从命令行运行Python