Java 使用 gradle "run" 任务运行 main 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28610015/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 06:31:07  来源:igfitidea点击:

run main method using gradle "run" task

javagradlemain

提问by Elad Benda

I want to run my mainmethod via gradle task

我想main通过 gradle 任务运行我的方法

This is how I run via the cmd:

这是我通过cmd运行的方式:

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

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.

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 运行时,一切正常

采纳答案by Tibor Blenessy

The easiest is probably to use application plugin. Add apply plugin: 'application'to your build.gradle and set mainClassName = com.bla.MainRunner. To add arguments to your main class modify the run task and edit the args property

最简单的可能是使用应用程序插件。添加apply plugin: 'application'到您的 build.gradle 并设置mainClassName = com.bla.MainRunner. 要向主类添加参数,请修改运行任务并编辑 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.

类路径是从主源集自动获取的,如果你想要不同的,你可以编辑运行任务的类路径属性。

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

回答by Navneet kumar

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.

这是一个独立的注册任务,也可以有任务的组和描述等属性。