有什么方法可以通过Gradle运行任意的main方法吗? [英] Are there any ways to run an arbitrary main method via Gradle?

查看:581
本文介绍了有什么方法可以通过Gradle运行任意的main方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我意识到当我使用IntelliJ IDEA CE并创建Gradle-Java项目时.

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

当我运行Class.main()时,说Hoge.main(), 运行窗口显示为

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

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

以及gradle任务列表中的

and in gradle tasks list

:Hoge.main()

像这样(最底行).

如何在终端(不使用IDEA)中执行此操作?如果可能,可能是这样吗? (我了解 https://docs.gradle.org/current/userguide/application_plugin. html ,这对我而言并不灵活)

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 

这仅在IDEA中吗? 我看到了一些使用应用程序插件的想法,但找不到像上面这样的简单方法...

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

推荐答案

这就是gradle包装器的用途.如果有包装器(在 [ProjectDir]/gradle/wrapper 文件夹中),则可以在不使用IDE的情况下使用Gradle.

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)在Windows中,执行gradlew.bat脚本;

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

B)在Unix中,执行gradlew脚本.

B) In Unix, execute the gradlew script.

在您的情况下,您将在终端中输入以下内容: gradlew run./gradlew run.

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

P.S. 如果要执行多个运行"任务,则需要像这样创建它们:

P.S. 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')
}

这篇关于有什么方法可以通过Gradle运行任意的main方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆