当从命令行运行时,如何在spring引导应用程序中传递命令行参数



我试图从命令行运行spring引导应用程序并传递命令行参数。我尝试了几种方法都没有效果:-

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参数定义应用程序的环境变量。裁判:https://docs.spring.io/spring-boot/docs/current/maven-plugin/reference/htmlsingle/goals-run-parameters-details-arguments

其次,当您运行应用程序时,在命令行中添加相应的参数以填充pom文件中的占位符,在本例中为"${env.callbackport}"对应的命令行参数是- denver .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] ------------------------------------------------------------------------

对于任何希望通过属性的人(而不是env vars),语法略有不同(来源):

<project>
<build>
<properties>
<my.value>42</my.value>
</properties>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<property1>test</property1>
<property2>${my.value}</property2>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
</project>

也可以通过命令行来完成,它优先于上面的配置:

$ mvn spring-boot:run -Dspring-boot.run.jvmArguments="-Dproperty1=overridden"

最新更新