使用gradle“运行”任务运行主要方法

我想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运行时,每个thig都可以正常工作

回答:

最简单的方法可能是使用应用程序插件。添加apply plugin:

'application'到您的build.gradle并设置mainClassName =

com.bla.MainRunner。要将参数添加到主类,请修改运行任务并编辑args属性

run {

args += 'first_arg'

}

类路径是从主sourceSet中自动获取的,如果需要其他类,则可以编辑运行任务的类路径属性。

如果需要更多自定义,则可以这样定义JavaExec类型的任务:

task myRun(type: JavaExec) {

classpath sourceSets.main.runtimeClasspath

main = "com.bla.MainRunner"

args "arg1", "arg2"

}

以上是 使用gradle“运行”任务运行主要方法 的全部内容, 来源链接: utcz.com/qa/421345.html

回到顶部