簡體   English   中英

從 spring-boot:run 獲取命令行參數

[英]Get command-line arguments from spring-boot:run

從命令行啟動 spring-boot 應用程序 (mvn spring-boot:run) 時有沒有辦法輸入參數,然后在 main() 中獲取它們?

查看spring-boot-maven-plugin的源碼發現你需要這樣做:

mvn spring-boot:run -Drun.arguments="arg1,arg2"

獲取有關spring-boot插件的run目標支持哪些選項的更多信息的另一種方法是執行以下命令:

mvn help:describe -Dcmd=spring-boot:run -Ddetail

對於 Spring Boot 2.x,源代碼在這里,您現在需要使用-Dspring-boot.run.arguments="args1,args2"

(從 2021 年 4 月開始編輯)對於 Spring Boot 2.2+,您現在需要使用-Dspring-boot.run.arguments="args1 args2"

如果您正在使用 Gradle 並且希望能夠將命令行參數傳遞給 Gradle bootRun任務,您首先需要進行配置,例如像這樣:

bootRun {
    if ( project.hasProperty('args') ) {
        args project.args.split('\\s+')
    }
}

並使用gradle bootRun -Pargs="arg1 arg2"運行任務

使用 -Drun.arguments 傳遞多個參數時,如果參數依次具有“逗號分隔”值,則僅使用每個參數的第一個值。 為了避免這種情況,重復參數與值的數量一樣多。

這更像是一種解決方法。 不確定是否有替代方案,除非分隔符不同 - 如“|”。

例如問題:

mvn spring-boot:run -Drun.arguments="--spring.profiles.active=test,dev"

僅為上述命令選擇“測試”配置文件。

解決方法:

mvn spring-boot:run -Drun.arguments="--spring.profiles.active=test,--spring.profiles.active=dev"

為上述命令選擇 'dev' 和 'test' 配置文件。

請注意:傳遞參數的方式取決於spring-boot major.minor 版本。

TLDR

對於 Spring Boot 1:

mvn spring-boot:run -Drun.arguments="argOne,argTwo"

對於 Spring Boot 2.0 和 2.1:

mvn spring-boot:run -Dspring-boot.run.arguments="argOne,argTwo"

(從 2021 年 4 月開始編輯)對於 Spring Boot 2.2 及更高版本:

mvn spring-boot:run -Dspring-boot.run.arguments="argOne argTwo"

  1. spring-boot-maven-plugin版本和你使用的Spring Boot版本必須對齊。

根據使用的 Spring Boot 主要版本( 12 ),確實應該使用12版本中的spring-boot-maven-plugin
如果您的pom.xml繼承自spring-boot-starter-parent

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>ONE_OR_TWO_VERSION</version>
</parent>

在你的 pom 中,甚至不應該指定使用的插件版本,因為這個插件依賴是繼承的:

<plugins>
    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>                   
        <configuration>
           ...
        </configuration>
    </plugin>
</plugins>

如果您的pom.xml不是從spring-boot-starter-parent繼承的,請不要忘記將spring-boot-maven-plugin的版本與您要使用的spring boot的確切版本對齊。

  1. 使用spring-boot-maven-plugin:1.XX在命令行中傳遞參數

對於一個論點:

mvn spring-boot:run -Drun.arguments="argOne"

對於多個:

mvn spring-boot:run -Drun.arguments="argOne,argTwo"

Maven 插件頁面記錄了它:

  Name         Type       Since           Description
arguments  |  String[]  |  1.0  | Arguments that should be passed 
                                  to the application. On command line use 
                                  commas to separate multiple arguments.
                                  User property is: run.arguments.
  1. 使用spring-boot-maven-plugin:2.XX在命令行中傳遞參數

對於一個論點:

mvn spring-boot:run -Dspring-boot.run.arguments="argOne"

對於多個:

mvn spring-boot:run -Dspring-boot.run.arguments="argOne,argTwo"

我沒有找到 2.XX 版本的插件文檔。
但是spring-boot-maven-plugin:2.0.0.M3插件的org.springframework.boot.maven.AbstractRunMojo類引用了這個用戶屬性:

public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo
   ...
   @Parameter(property="spring-boot.run.arguments")
   private String[] arguments;
   ...
   protected RunArguments resolveApplicationArguments(){
     RunArguments runArguments = new RunArguments(this.arguments);
     addActiveProfileArgument(runArguments);
     return runArguments;
   }
   ...
 }
  1. 提示:當您傳遞多個參數時,會考慮逗號之間的空格。

    mvn spring-boot:run -Dspring-boot.run.arguments="argOne,argTwo"

將被解釋為["argOne", "argTwo"]

但是這個 :

mvn spring-boot:run -Dspring-boot.run.arguments="argOne, argTwo"

將被解釋為["argOne", " argTwo"]

(2021 年 3 月編輯)

空格現在用作多參數命令的分隔符,請參閱相關問題

正如我今天檢查的那樣,Spring Boot 2.2.5 的正確用法是:

mvn spring-boot:run -Dspring-boot.run.arguments="--arg1=value --arg2=value"

因為幫助說:

 commandlineArguments User property: spring-boot.run.arguments Arguments from the command line that should be passed to the application. Use spaces to separate multiple arguments and make sure to wrap multiple values between quotes. When specified, takes precedence over arguments.

Spring Boot 1 as 2 提供了一種將多個配置文件作為參數傳遞的方法,並避免了與用作參數之間的分隔符和作為活動配置文件傳遞的值的逗號相關的問題。

所以而不是寫:

mvn spring-boot:run -Dspring-boot.run.arguments=--spring.profiles.active=test,--spring.profiles.active=dev

使用Spring Boot Maven profiles屬性,它是spring.profiles.active的便捷快捷方式,例如以下內容:

Maven 用戶屬性根據 Spring Boot 版本而有所不同。

對於 Spring Boot 1.4+,即run.profiles

mvn spring-boot:run -Drun.profiles=dev,test

對於 Spring Boot 2,即spring-boot.run.profiles

mvn spring-boot:run -Dspring-boot.run.profiles=dev,test

從插件文檔:

簡介

要激活的彈簧輪廓。 指定 'spring.profiles.active' 參數的便捷快捷方式。 在命令行上使用逗號分隔多個配置文件。

類型:java.lang.String[]

自:1.3

要求:否

用戶屬性: spring-boot.run.profiles

如果您使用的是 Eclipse ......

| Parameter Name | Value         |
| run.arguments  | "--name=Adam" |

這對我spring-boot v1.4.3.RELEASEspring-boot v1.4.3.RELEASE ),。

mvn spring-boot:run -Dspring.profiles.active=test,local -Dlogback-debug=true

對於最新版本的 spring,請使用-Dspring-boot.run.arguments= ,如下例所示

spring-boot:run -Djasypt.encryptor.password=temp -Dspring-boot.run.arguments="OU,Grade"

我正在使用 spring.boot 2.4.2,我用空格分隔參數並將值放在雙引號之間。

mvn spring-boot:run -Dspring-boot.run.arguments="--param1=value2 --param2=value2"

對 Spring Boot 應用程序使用以下命令。

mvn spring-boot:run -Dspring-boot.run.arguments="--java.net.preferIPv4Stack=true --config.password=PASSWORD --config.token=s.TOKEN --spring.application.name=ENV --config.server.ip=IP_ADDRESS --spring.profiles.active=ENV --spring.profiles.active.custom=ENV_custom"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM