run main method using gradle “run” task

前端 未结 2 1289
别那么骄傲
别那么骄傲 2020-12-09 08:09

I want to run my main method via gradle task

This is how I run via the cmd:

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

2条回答
  •  半阙折子戏
    2020-12-09 09:07

    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

    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

    task myRun(type: JavaExec) {
      classpath sourceSets.main.runtimeClasspath
      main = "com.bla.MainRunner"
      args "arg1", "arg2"
    }
    

提交回复
热议问题