本文介绍了Maven的exec插件用于运行Java应用程序的等价物是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用maven,我可以创建一个项目,设置我的pom及其依赖关系,用main方法编写一个类,然后运行它:

With maven I can create a project, set up my pom with its dependencies, write a class with a main method and then to run it type:

mvn compile exec:java -Dexec.mainClass=thornydev.App

是这样做的gradle等价物吗?

What is the gradle equivalent of doing this?

我可以做 gradle build ,它为我建立一个jar文件,但是如果主类对其他jar有任何依赖关系,那么只要运行jar就不会设置类路径。 gradle java插件可以运行应用程序并为我设置类路径吗?

I can do gradle build, which builds a jar file for me, but if the main class has any dependencies on other jars, just running the jar won't work without setting up the classpath. Can the gradle java plugin run the app and set up the classpath for me?

我正在寻找一种简单的一次性使用的命令行解决方案,而不是IDE集成(我知道该怎么做)。

I'm looking for a command line solution for simple one-off uses, not IDE integration (I know how to do that).

推荐答案

最简单的解决方案是使用,其中包括 run 任务。要使主类可以从命令行配置,您必须将 mainClassName 设置为某个系统(或项目)属性的值,然后将该属性从命令行:

The easiest solution is to use the Application Plugin, which among other things provides a run task. To make the main class configurable from the command line, you'll have to set mainClassName to the value of some system (or project) property, and then pass that property from the command line:

apply plugin: "application"

mainClassName = System.getProperty("mainClass")

现在您可以使用 gradle run -DmainClass = thornydev运行应用程序。 App

这篇关于Maven的exec插件用于运行Java应用程序的等价物是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 00:55