Home > Back-end >  How to pass command line arument in spring boot app when running from command line
How to pass command line arument in spring boot app when running from command line

Time:04-20

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?

CodePudding user response:

Firstly, you should add the following configuration into your pom file.

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

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:

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

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":

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

CodePudding user response:

Command line arguments should be passed with the command

mvn spring-boot:run '-Dspring-boot.run.arguments=CALLBACK_PORT=8000'
  • Related