简体   繁体   中英

Are there any ways to run an arbitrary main method via Gradle?

I realize when I use IntelliJ IDEA CE and make a Gradle-Java project.

When I run a Class.main(), say Hoge.main(), the run window shows like

0:15:03 PM: Executing task 'Hoge.main()'...

and in gradle tasks list

:Hoge.main()

在此处输入图片说明

like this (the bottom line).

How can I do this in a terminal (not using IDEA)? If possible, it may be like this? (I know about https://docs.gradle.org/current/userguide/application_plugin.html , which is not flexible for my purpose)

gradle run Java Hoge 

Is this only in IDEA? I saw some ideas using the application plugin but I could not find very simple way like above...

That's what the gradle wrapper is for. If you have the wrapper (in [ProjectDir]/gradle/wrapper folder), then you can use Gradle without an IDE.

A) In Windows, execute the gradlew.bat script;

B) In Unix, execute the gradlew script.

In your case, you would type in the terminal following: gradlew run or ./gradlew run .

PS If you want to have several "run" tasks, you need to create them like so:

task runHoge(type: JavaExec,group: 'application'){
    classpath(sourceSets.main.runtimeClasspath)
    // set the main class name here
    setMain('package.Hoge')
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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