Gradle 实战 - 命令行传递-ApiHug准备-工具篇-013

  🤗 ApiHug × {Postman|Swagger|Api...} = 快↑ 准√ 省↓

  1. GitHub - apihug/apihug.com: All abou the Apihug   
  2. apihug.com: 有爱,有温度,有质量,有信任
  3. ApiHug - API design Copilot - IntelliJ IDEs Plugin | Marketplace

ApiHug 整个工具链基于 Gradle, 使用 ApiHug 准备工作最先需要学习的就是 gradle. 工欲善其事,必先利其器

命令行参数是动态控制查询运行时 profile 主要方式之一, 同理 gradle 也提供了参数传入机制。

#参数类型

两种类型的参数:

  1. 系统配置参数 -D 标志
  2. 项目配置参数 -P 标志

添加参数打印:

apply plugin: "java"
description = "Gradle Command Line Arguments examples"

task propertyTypes(){
    doLast{
        if (project.hasProperty("args")) {
            println "Our input argument with project property ["+project.getProperty("args")+"]"
        }
        println "Our input argument with system property ["+System.getProperty("args")+"]"
    }
}

运行操作如下:

> .\gradlew.bat  cmd-line-args:propertyTypes -Dargs=dear -Pargs=xue   

> Task :cmd-line-args:propertyTypes
Our input argument with project property [xue]
Our input argument with system property [dear]

#命令行参数传递

上面有读取参数的方式, 那么如何参数传递到我们的 main 函数?

apply plugin: "java"
apply plugin: "application"
description = "Gradle Command Line Arguments examples"

ext.javaMainClass = "com.dearxue.cmd.MainClass"

application {
    mainClassName = javaMainClass
}

java 方法如下:


public class MainClass {
  public static void main(String[] args) {
    System.out.println("JAVA - Gradle command line arguments example");
    for (String arg : args) {
      System.out.println("JAVA - Got argument [" + arg + "]");
    }
  }
}

运行如下:

> .\gradlew.bat  cmd-line-args:run --args="may the force be with you"

> Task :cmd-line-args:run
JAVA - Gradle command line arguments example
JAVA -  Got argument [may]
JAVA -  Got argument [the]
JAVA -  Got argument [force]
JAVA -  Got argument [be]
JAVA -  Got argument [with]
JAVA -  Got argument [you]


Application 的 run 任务自动你参数处理了, JavaExec 就不一样, 需要写操作如下:

if (project.hasProperty("args")) {
    ext.cmdargs = project.getProperty("args")
} else {
    ext.cmdargs = "ls"
}

task cmdLineJavaExec(type: JavaExec) {
    group = "Execution"
    description = "Run the main class with JavaExecTask"
    classpath = sourceSets.main.runtimeClasspath
    mainClass = javaMainClass
    args cmdargs.split()
}

输出结果同样:

> .\gradlew.bat  cmd-line-args:cmdLineJavaExec  -Pargs="may the force be with you"

> Task :cmd-line-args:cmdLineJavaExec
JAVA - Gradle command line arguments example
JAVA -  Got argument [may]
JAVA -  Got argument [the]
JAVA -  Got argument [force]
JAVA -  Got argument [be]
JAVA -  Got argument [with]
JAVA -  Got argument [you]

#Exec 传递参数到第三方应用

task cmdLineExec(type: Exec) {
    group = "Execution"
    description = "Run an external program with ExecTask"
    commandLine cmdargs.split()
}

执行:

$ ./gradlew cmdLineExec -Pargs="ls -ll"

> Task :cmd-line-args:cmdLineExec
total 4
drwxr-xr-x 1 user 1049089    0 Sep  1 17:59 bin
drwxr-xr-x 1 user 1049089    0 Sep  1 18:30 build
-rw-r--r-- 1 user 1049089 1016 Sep  3 15:32 build.gradle
drwxr-xr-x 1 user 1049089    0 Sep  1 17:52 src

#结论

这个例子演示如何通过 gradle 传递 参数。

有多种方式来传递参数, 通过系统参数, 或者项目参数, 通过项目参数是个更推荐的方式。

然后 如何传递 command-line 给 Java 或者 其他第三方应用。

项目地址: cmd-line-args 项目例子

我们

api-hug-contact

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值