简体   繁体   English

使用gradle“run”任务运行main方法

[英]run main method using gradle “run” task

I want to run my main method via gradle task 我想通过gradle任务运行我的main方法

This is how I run via the cmd: 这是我通过cmd运行的方式:

java -cp RTMonitor.jar com.bla.MainRunner first_arg

how should it be written in gradle? 应该如何用gradle写?

run {
    args += ['java -cp RTMonitor.jar com.bla.MainRunner first_arg']
}

Update 更新

I have tried 我试过了

task myRun(type: JavaExec) {
    classpath configurations.main
    main = "com.bla.runners.StatsLogGenerator"
    args "arg1", "arg2"
}

and I got: 我得到了:

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"
    }

and i got an error: 我收到一个错误:

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)

but when I run via Intellij, every thig works OK 但是当我通过Intellij运行时,每个thig工作正常

The easiest is probably to use application plugin. 最简单的可能就是使用应用程序插件。 Add apply plugin: 'application' to your build.gradle and set mainClassName = com.bla.MainRunner . apply plugin: 'application'添加到build.gradle并设置mainClassName = com.bla.MainRunner To add arguments to your main class modify the run task and edit the args property 要向主类添加参数,请修改运行任务并编辑args属性

run {
  args += 'first_arg'
}

Classpath is taken automatically from main sourceSet, if you want different one, you can edit classpath property of the run task. Classpath是从主sourceSet自动获取的,如果你想要不同的,你可以编辑run task的classpath属性。

If you need more customization, you can define your own task of type JavaExec like this 如果您需要更多自定义,可以像这样定义自己的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"
}

This is a independent registered task and can also have group and description and other properties of task. 这是一个独立的注册任务,也可以具有组和描述以及任务的其他属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM