Gradle执行Java类(无需修改build.gradle)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21358466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:16:19  来源:igfitidea点击:

Gradle to execute Java class (without modifying build.gradle)

javagradleexecution

提问by Paul Verest

There is simple Eclipse pluginto 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.buildcould be run.

这样任何项目gradle.build都可以运行。

UPDATE: There was similar question What is the gradle equivalent of maven's exec plugin for running Java apps?asked before, but solution suggested altering every project build.gradle

更新:有一个类似的问题什么是运行 Java 应用程序的 maven exec 插件的 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   

采纳答案by First Zero

There is no direct equivalent to mvn exec:javain gradle, you need to either apply the applicationplugin or have a JavaExectask.

mvn exec:java在 gradle 中没有直接等价物,您需要应用application插件或有JavaExec任务。

applicationplugin

application插入

Activate the plugin:

激活插件:

plugins {
    id 'application'
    ...
}

Configure it as follows:

配置如下:

application {
    mainClassName = project.hasProperty("mainClass") ? getProperty("mainClass") : "NULL"
}

On the command line, write

在命令行上写

$ gradle -PmainClass=Boo run

JavaExectask

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. You get

要运行,请编写gradle -PmainClass=Boo execute. 你得到

$ gradle -PmainClass=Boo execute
:compileJava
:compileGroovy UP-TO-DATE
:processResources UP-TO-DATE
:classes
:execute
I am BOO!

mainClassis a property passed in dynamically at command line. classpathis set to pickup the latest classes.

mainClass是在命令行动态传入的属性。classpath设置为选择最新的课程。



If you do not pass in the mainClassproperty, 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'.

回答by Vidya

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 mainClassNameas a system property and run with a command line argument.

正如 Teresa 指出的那样,您还可以配置mainClassName为系统属性并使用命令行参数运行。

回答by Matt

Expanding on First Zero's answer, I'm guess you want something where you can also run gradle buildwithout errors.

扩展第一个零的答案,我猜你想要一些你也可以gradle build无错误运行的东西。

Both gradle buildand gradle -PmainClass=foo runAppwork with this:

两者gradle buildgradle -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.

设置默认主类的位置。

回答by Sameera De Silva

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;
    }