🤗 ApiHug × {Postman|Swagger|Api...} = 快↑ 准√ 省↓
- GitHub - apihug/apihug.com: All abou the Apihug
- apihug.com: 有爱,有温度,有质量,有信任
- 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
插件隐式引入的插件。
- The Application Pluginopen in new window
- 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 程序。
- main 类
- 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()
}
}
我们的例子:
- 依赖
build
任务 - 运行时 class path 依赖
- 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 例子