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

smdncfj3  于 2022-11-02  发布在  Maven
关注(0)|答案(2)|浏览(211)

我试图从命令行运行Sping 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);

它输出回叫端口:零值
如何使用此命令行参数运行它?

luaexgnf

luaexgnf1#

首先,您应该将以下配置添加到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参数定义应用程序的环境变量。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:三二二一”:

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

qqrboqgw2#

对于希望传递Properties(而不是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"

相关问题