我想通过渐变任务运行我的main
方法
这就是我通过cmd:运行的方式
java -cp RTMonitor.jar com.bla.MainRunner first_arg
它应该怎么写?
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运行时,每个thig都可以正常工作
最简单的方法可能是使用应用程序插件。将apply plugin: 'application'
添加到您的build.gradle并设置mainClassName = com.bla.MainRunner
。要向主类添加参数,请修改运行任务并编辑参数属性
run {
args += 'first_arg'
}
Classpath是自动从主sourceSet中获取的,如果您想要不同的Classpath,您可以编辑运行任务的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"
}
这是一个独立的注册任务,也可以具有任务的组、描述和其他属性。