Gradle 实战 - 启动main函数-ApiHug准备-工具篇-012

  🤗 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. 工欲善其事,必先利其器

在 gradle 直接运行 java main 函数, 犹如我们直接运行 gradle bootRun, 整个教程简单看下 gradle 如何直接运行 main 函数。

gradle 有很多种方式运行, 下面我们具体一一分析。

#项目结构

public class MainClass {

  public static void main(String[] args) {
    System.out.println("Goodbye cruel world ...");
  }
}

#Application 插件

Application 插件是 gradle 核心插件之一, 也是我们熟悉的 java 插件隐式引入的插件。

  1. The Application Pluginopen in new window
  2. Building Java Applications Sampleopen in new window

配置如下:


apply plugin: "application"

description = "Java MainClass execution examples"

ext {
    javaMainClass = "com.dearxue.gradle.exec.MainClass"
}

application {
    mainClassName = javaMainClass
}

application 插件自动引入了 run 任务:

Application tasks
-----------------
run - Runs this project as a JVM application

运行结果:


.\gradlew.bat  java-exec:run 

> Task :java-exec:run
Goodbye cruel world ...

#JavaExec

JavaExec 文档open in new window, JavaExec 来启动java 程序。

  1. main 类
  2. class path

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

运行结果:

 .\gradlew.bat  java-exec:runWithJavaExec

> Task :java-exec:runWithJavaExec
Goodbye cruel world ...

#Exec

Exec 文档open in new window, Exec 可以执行任何命令行:

task stopTomcat(type:Exec) {
  workingDir '../tomcat/bin'

  //on windows:
  commandLine 'cmd', '/c', 'stop.bat'

  //on linux
  commandLine './stop.sh'

  //store the output instead of printing to the console:
  standardOutput = new ByteArrayOutputStream()

  //extension method stopTomcat.output() can be used to obtain the output:
  ext.output = {
    return standardOutput.toString()
  }
}

我们的例子:

  1. 依赖 build 任务
  2. 运行时 class path 依赖
  3. main 函数

task runWithExec(type: Exec) {
    dependsOn build
    group = "Execution"
    description = "Run the main class with ExecTask"
    commandLine "java", "-classpath", sourceSets.main.runtimeClasspath.getAsPath(), javaMainClass
}

效果一样:

 .\gradlew.bat  java-exec:runWithExec    

> Task :java-exec:runWithExec
Goodbye cruel world ...

#Exec 输出 jar 包

task runWithExecJarOnClassPath(type: Exec) {
    dependsOn jar
    group = "Execution"
    description = "Run the mainClass from the output jar in classpath with ExecTask"
    commandLine "java", "-classpath", jar.archiveFile.get(), javaMainClass
}

效果一样:

.\gradlew.bat  java-exec:runWithExecJarOnClassPath

> Task :java-exec:runWithExecJarOnClassPath
Goodbye cruel world ...

#Exec 独立打包

jar {
    manifest {
        attributes(
            "Main-Class": javaMainClass
        )
    }
}

task runWithExecJarExecutable(type: Exec) {
    dependsOn jar
    group = "Execution"
    description = "Run the output executable jar with ExecTask"
    commandLine "java", "-jar", jar.archiveFile.get()
}

运行结果:

.\gradlew.bat  java-exec:runWithExecJarExecutable 

> Task :java-exec:runWithExecJarExecutable
Goodbye cruel world ...

#结论

Application 插件提供了最小配置; JavaExec 任务让我们保持最少依赖。

最后的 Exec 任务可以让我们灵活的组合应用,异常的强大方便。

项目地址 java-exec 例子

api-hug-contact

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值