从spring-boot:run获取命令行参数

75

有没有办法在命令行启动spring-boot应用程序(mvn spring-boot:run)时输入参数,并在main()函数中获取它们?

10个回答

112

查看 spring-boot-maven-plugin 的源代码,我发现你需要执行以下操作:

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

获取有关spring-boot插件的run目标支持哪些选项的更多信息的另一种方法是执行以下命令:

另一种获取有关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"运行任务


1
您IP地址为143.198.54.68,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。 - Ravindranath Akila
1
使用 Gradle 3.3,以下修改对我有效: // Usage: gradle bootRun -Pargs="arg1 arg2" if ( project.hasProperty('args') ) { args = (project.args.split("\\s+") as List) } } - Farrukh Najmi
@FarrukhNajmi 我会检查并更新答案,谢谢。 - geoand
4
从Spring Boot 2开始,你应该使用-Dspring-boot.run.arguments= - mapm
请注意,目前接受的答案已经过时,对于Spring Boot 2.2及更高版本,请参见此问题下的更新答案 - 参数现在通过空格分隔,而不是逗号。 - Martin
显示剩余2条评论

40

当使用-Drun.arguments传递多个参数时,如果一个参数又包含了“逗号分隔”的值,则只使用每个参数的第一个值。为避免此问题,请重复该参数的次数与值的数量相同。

这更像是一种解决方法。不确定是否有其他替代方案,除非分隔符是不同的 - 如“|”。

例如,问题:

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

以上命令仅选择“test”配置文件。

解决方法:

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

该命令会同时选择“dev”和“test”配置文件。


1
从Spring Boot 2开始,您应该使用-Dspring-boot.run.arguments= - mapm

27

请注意:传递参数的方式取决于spring-boot的主版本和次版本。

简而言之:

对于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),需要使用对应的 spring-boot-maven-plugin,即 1 版本或 2 版本。
如果你的 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.X.X在命令行中传递参数

对于一个参数:

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.X.X在命令行中传递参数

对于一个参数:

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

对于多个:

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

我没有找到关于那个的版本为2.X.X的插件文档。
但是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月的更新)

现在,多参数命令使用空格作为分隔符,请参见相关问题


你如何在pom.xml的插件“configuration”部分中指定参数?我尝试了<arguments><argument>-Dprop=value</argument></arguments>,但是这种方式无法获取属性值。 - user1902183
@user1902183 使用 <arguments><argument>--prop=value</argument></arguments> - splash

12

根据我今天的检查,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.

1
你可以像这样混合使用未命名参数和命名参数:mvn spring-boot:run -Dspring-boot.run.arguments="parameter1 --arg2=value"。此外,建议将所有参数用单引号括起来。 - Martin

3
我正在使用spring.boot 2.4.2,我使用空格分隔参数,并将值放在双引号之间。
mvn spring-boot:run -Dspring-boot.run.arguments="--param1=value2 --param2=value2"

3
Spring Boot 1和2都提供了一种以参数形式传递多个配置文件的方法,避免了在参数中使用逗号作为分隔符的问题。因此,不需要像下面这样书写:
```bash java -jar myproject.jar --spring.profiles.active=dev,logging ```
而是可以用以下方式书写:
mvn spring-boot:run -Dspring-boot.run.arguments=--spring.profiles.active=test,--spring.profiles.active=dev

使用 Spring Boot Maven profiles 属性,它是一个方便的快捷方式,可以代替 spring.profiles.active,如下所示:

根据 Spring Boot 版本的不同,Maven 用户属性也有所不同。

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

来自插件文档:

profiles:

激活的Spring配置文件。是指定'spring.profiles.active'参数的便捷快捷方式。在命令行上使用逗号分隔多个配置文件。

类型:java.lang.String[]

自版本1.3起

必需:否

用户属性:spring-boot.run.profiles


1
如果您正在使用Eclipse... | 参数名称 | 值 | | ---------- | ------------ | | run.arguments | "--name=Adam" |

0

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

Bean加载顺序检索

f1f3d6f3-64b8-4fe1-8e50-9747d24557e3

ApplicationContext ctx = SpringApplication.run(MyApp.class, args);
        String[] beanNames = ctx.getBeanDefinitionNames();
        for (String beanName : beanNames) {
            System.out.println(beanName);
        }

0

这是对我有效的解决方案(spring-boot v1.4.3.RELEASE)。

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

0

对于最新版本的Spring,请使用以下示例中显示的-Dspring-boot.run.arguments=

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接