运行 使用 gradle "run" 任务的主要方法
run main method using gradle "run" task
我想 运行 我的 main
方法通过 gradle 任务
这就是我通过 cmd 运行 的方式:
java -cp RTMonitor.jar com.bla.MainRunner first_arg
gradle应该怎么写?
run {
args += ['java -cp RTMonitor.jar com.bla.MainRunner first_arg']
}
更新
我试过了
task myRun(type: JavaExec) {
classpath configurations.main
main = "com.bla.runners.StatsLogGenerator"
args "arg1", "arg2"
}
我得到了:
Error:(71, 0) Could not find property 'main' on configuration container.
the I have tried:
task myRun(type: JavaExec) {
classpath "configurations.main"
main = "com.bla.runners.StatsLogGenerator"
args "arg1", "arg2"
}
我得到一个错误:
FAILURE: Build failed with an exception.
17:49:21.855 [ERROR] [org.gradle.BuildExceptionReporter]
17:49:21.856 [ERROR] [org.gradle.BuildExceptionReporter] * What went wrong:
17:49:21.856 [ERROR] [org.gradle.BuildExceptionReporter] Execution failed for task ':myRun'.
17:49:21.856 [ERROR] [org.gradle.BuildExceptionReporter] > Process 'command '/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/bin/java'' finished with non-zero exit value 1
17:49:21.864 [ERROR] [org.gradle.BuildExceptionReporter]
17:49:21.865 [ERROR] [org.gradle.BuildExceptionReporter] * Exception is:
17:49:21.866 [ERROR] [org.gradle.BuildExceptionReporter] org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':myRun'.
17:49:21.867 [ERROR] [org.gradle.BuildExceptionReporter] at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:69)
17:49:21.882 [ERROR] [org.gradle.BuildExceptionReporter] at org.gradle.process.internal.DefaultExecHandle$ExecResultImpl.assertNormalExitValue(DefaultExecHandle.java:361)
17:49:21.882 [ERROR] [org.gradle.BuildExceptionReporter] at org.gradle.process.internal.DefaultJavaExecAction.execute(DefaultJavaExecAction.java:31)
但是当我通过 Intellij 运行 时,每件事情都正常
最简单的可能是使用应用程序插件。将 apply plugin: 'application'
添加到您的 build.gradle 并设置 mainClassName = com.bla.MainRunner
。要向主 class 添加参数,请修改 运行 任务并编辑参数 属性
run {
args += 'first_arg'
}
Classpath 是从 main sourceSet 自动获取的,如果你想要不同的,你可以编辑 运行 任务的 classpath 属性。
如果您需要更多自定义,您可以像这样定义自己的 JavaExec 类型的任务
task myRun(type: JavaExec) {
classpath sourceSets.main.runtimeClasspath
main = "com.bla.MainRunner"
args "arg1", "arg2"
}
task run(type: JavaExec) {
group = 'Run' // <-- change the name as per your need
description = 'Small description what this run will do'
classpath sourceSets.main.runtimeClasspath // <-- Don't change this
main = "com.mypackage.myclassNameContaingMainMethod"
args "arg1", "arg2"
}
这是一个独立注册的任务,也可以有任务的分组和描述等属性。
我想 运行 我的 main
方法通过 gradle 任务
这就是我通过 cmd 运行 的方式:
java -cp RTMonitor.jar com.bla.MainRunner first_arg
gradle应该怎么写?
run {
args += ['java -cp RTMonitor.jar com.bla.MainRunner first_arg']
}
更新
我试过了
task myRun(type: JavaExec) {
classpath configurations.main
main = "com.bla.runners.StatsLogGenerator"
args "arg1", "arg2"
}
我得到了:
Error:(71, 0) Could not find property 'main' on configuration container.
the I have tried:
task myRun(type: JavaExec) {
classpath "configurations.main"
main = "com.bla.runners.StatsLogGenerator"
args "arg1", "arg2"
}
我得到一个错误:
FAILURE: Build failed with an exception.
17:49:21.855 [ERROR] [org.gradle.BuildExceptionReporter]
17:49:21.856 [ERROR] [org.gradle.BuildExceptionReporter] * What went wrong:
17:49:21.856 [ERROR] [org.gradle.BuildExceptionReporter] Execution failed for task ':myRun'.
17:49:21.856 [ERROR] [org.gradle.BuildExceptionReporter] > Process 'command '/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/bin/java'' finished with non-zero exit value 1
17:49:21.864 [ERROR] [org.gradle.BuildExceptionReporter]
17:49:21.865 [ERROR] [org.gradle.BuildExceptionReporter] * Exception is:
17:49:21.866 [ERROR] [org.gradle.BuildExceptionReporter] org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':myRun'.
17:49:21.867 [ERROR] [org.gradle.BuildExceptionReporter] at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:69)
17:49:21.882 [ERROR] [org.gradle.BuildExceptionReporter] at org.gradle.process.internal.DefaultExecHandle$ExecResultImpl.assertNormalExitValue(DefaultExecHandle.java:361)
17:49:21.882 [ERROR] [org.gradle.BuildExceptionReporter] at org.gradle.process.internal.DefaultJavaExecAction.execute(DefaultJavaExecAction.java:31)
但是当我通过 Intellij 运行 时,每件事情都正常
最简单的可能是使用应用程序插件。将 apply plugin: 'application'
添加到您的 build.gradle 并设置 mainClassName = com.bla.MainRunner
。要向主 class 添加参数,请修改 运行 任务并编辑参数 属性
run {
args += 'first_arg'
}
Classpath 是从 main sourceSet 自动获取的,如果你想要不同的,你可以编辑 运行 任务的 classpath 属性。
如果您需要更多自定义,您可以像这样定义自己的 JavaExec 类型的任务
task myRun(type: JavaExec) {
classpath sourceSets.main.runtimeClasspath
main = "com.bla.MainRunner"
args "arg1", "arg2"
}
task run(type: JavaExec) {
group = 'Run' // <-- change the name as per your need
description = 'Small description what this run will do'
classpath sourceSets.main.runtimeClasspath // <-- Don't change this
main = "com.mypackage.myclassNameContaingMainMethod"
args "arg1", "arg2"
}
这是一个独立注册的任务,也可以有任务的分组和描述等属性。