[英]Gradle to execute Java class (without modifying build.gradle)
There is simple Eclipse plugin to run Gradle, that just uses command line way to launch gradle.有一个简单的 Eclipse 插件来运行 Gradle,它只是使用命令行方式来启动 gradle。
What is gradle analog for maven compile and run mvn compile exec:java -Dexec.mainClass=example.Example
什么是 maven 编译和运行的 gradle 模拟mvn compile exec:java -Dexec.mainClass=example.Example
This way any project with gradle.build
could be run.这样任何带有gradle.build
项目都可以运行。
UPDATE: There was similar question What is the gradle equivalent of maven's exec plugin for running Java apps?更新:有一个类似的问题什么是运行 Java 应用程序的 maven exec 插件的 gradle 等价物? asked before, but solution suggested altering every project build.gradle
之前问过,但解决方案建议更改每个项目build.gradle
package runclass;
public class RunClass {
public static void main(String[] args) {
System.out.println("app is running!");
}
}
Then executing gradle run -DmainClass=runclass.RunClass
然后执行gradle run -DmainClass=runclass.RunClass
:run FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':run'.
> No main class specified
There is no direct equivalent to mvn exec:java
in gradle, you need to either apply the application
plugin or have a JavaExec
task. gradle 中没有与mvn exec:java
直接等效的方法,您需要应用application
插件或拥有JavaExec
任务。
application
plugin application
插件Activate the plugin:激活插件:
plugins {
id 'application'
...
}
Configure it as follows:配置如下:
application {
mainClassName = project.hasProperty("mainClass") ? project.getProperty("mainClass") : "NULL"
}
On the command line, write在命令行上写
$ gradle -PmainClass=Boo run
JavaExec
task JavaExec
任务Define a task, let's say execute
:定义一个任务,让我们说execute
:
task execute(type:JavaExec) {
main = project.hasProperty("mainClass") ? getProperty("mainClass") : "NULL"
classpath = sourceSets.main.runtimeClasspath
}
To run, write gradle -PmainClass=Boo execute
.要运行,请编写gradle -PmainClass=Boo execute
。 You get你得到
$ gradle -PmainClass=Boo execute
:compileJava
:compileGroovy UP-TO-DATE
:processResources UP-TO-DATE
:classes
:execute
I am BOO!
mainClass
is a property passed in dynamically at command line. mainClass
是在命令行动态传入的属性。 classpath
is set to pickup the latest classes. classpath
设置为拾取最新的类。
If you do not pass in the mainClass
property, both of the approaches fail as expected.如果您不传入mainClass
属性,则两种方法都会按预期失败。
$ gradle execute
FAILURE: Build failed with an exception.
* Where:
Build file 'xxxx/build.gradle' line: 4
* What went wrong:
A problem occurred evaluating root project 'Foo'.
> Could not find property 'mainClass' on task ':execute'.
You just need to use the Gradle Application plugin :您只需要使用Gradle 应用程序插件:
apply plugin:'application'
mainClassName = "org.gradle.sample.Main"
And then simply gradle run
.然后简单地gradle run
。
As Teresa points out, you can also configure mainClassName
as a system property and run with a command line argument.正如 Teresa 指出的那样,您还可以将mainClassName
配置为系统属性并使用命令行参数运行。
Expanding on First Zero's answer, I'm guess you want something where you can also run gradle build
without errors.扩展第一个零的答案,我猜你想要一些你也可以运行gradle build
没有错误的东西。
Both gradle build
and gradle -PmainClass=foo runApp
work with this: gradle build
和gradle -PmainClass=foo runApp
可以使用:
task runApp(type:JavaExec) {
classpath = sourceSets.main.runtimeClasspath
main = project.hasProperty("mainClass") ? project.getProperty("mainClass") : "package.MyDefaultMain"
}
where you set your default main class.设置默认主类的位置。
You can parameterise it and pass gradle clean build -Pprokey=goodbye您可以对其进行参数化并通过 gradle clean build -Pprokey=goodbye
task choiceMyMainClass(type: JavaExec) {
group = "Execution"
description = "Run Option main class with JavaExecTask"
classpath = sourceSets.main.runtimeClasspath
if (project.hasProperty('prokey')){
if (prokey == 'hello'){
main = 'com.sam.home.HelloWorld'
}
else if (prokey == 'goodbye'){
main = 'com.sam.home.GoodBye'
}
} else {
println 'Invalid value is enterrd';
// println 'Invalid value is enterrd'+ project.prokey;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.