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

How to pass command line arument in spring boot app when running from command line

我正在尝试 运行 spring 从命令行启动应用程序并传递命令行参数。我尝试了几种方法none的作品:-

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 

在所有情况下,应用程序 运行s。我试图将其解读为:-

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] ------------------------------------------------------------------------