run main method using gradle “run” task

前端 未结 2 1275
别那么骄傲
别那么骄傲 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"
    }
    
    0 讨论(0)
  • 2020-12-09 09:11
    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.

    0 讨论(0)
提交回复
热议问题