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

vsmadaxz  于 5个月前  发布在  Java
关注(0)|答案(5)|浏览(68)

simple Eclipse plugin来运行Gradle,它只是使用命令行方式来启动Gradle。
什么是Gradle Analog for Maven compile and run mvn compile exec:java -Dexec.mainClass=example.Example
这样,任何带有gradle.build的项目都可以运行。
更新:以前也有类似的问题What is the gradle equivalent of maven's exec plugin for running Java apps?,但解决方案建议更改每个项目build.gradle

package runclass;

public class RunClass {
    public static void main(String[] args) {
        System.out.println("app is running!");
    }
}

字符串
然后执行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

rta7y2nd

rta7y2nd1#

在gradle中没有与mvn exec:java直接等价的东西,你需要应用application插件或者有一个JavaExec任务。

application插件

激活插件:

plugins {
    id 'application'
    ...
}

字符串
按如下方式配置它:

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


在命令行上,

$ gradle -PmainClass=Boo run

JavaExec任务

定义一个任务,比如execute

task execute(type:JavaExec) {
   main = project.hasProperty("mainClass") ? getProperty("mainClass") : "NULL"
   classpath = sourceSets.main.runtimeClasspath
}


要运行,编写gradle -PmainClass=Boo execute

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


mainClass是在命令行动态传入的属性。classpath被设置为拾取最新的类。
如果您没有传入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'.

p8h8hvxi

p8h8hvxi2#

你只需要使用Gradle Application plugin

apply plugin:'application'
mainClass = "org.gradle.sample.Main"

字符串
然后简单地gradle run
正如Teresa所指出的,您还可以将mainClass配置为系统属性,并使用命令行参数运行。

cfh9epnr

cfh9epnr3#

扩展First Zero的答案,我猜你想要的东西,你也可以运行gradle build没有错误。
gradle buildgradle -PmainClass=foo runApp都可以使用:

task runApp(type:JavaExec) {
    classpath = sourceSets.main.runtimeClasspath

    main = project.hasProperty("mainClass") ? project.getProperty("mainClass") : "package.MyDefaultMain"
}

字符串
在这里设置默认的主类。

twh00eeo

twh00eeo4#

你可以将其参数化并传递gradle clean build -Pprokey=再见

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

字符串

dhxwm5r4

dhxwm5r45#

如果使用KotlinDSL和gradle 8.5(或者更新的版本),似乎我需要这样写:
如果你使用“应用程序”插件,写入build.gradle.kts并运行为:./gradlew run

plugins {
    application
}

application {
    mainClass.set("org.example.Main")
}

字符串
或者,如果你使用“java”插件,它通过JavaExec任务类型定义了一个名为“runMe”的自定义任务,当运行./gradlew runMe时,它做的事情和上面一样。好的一面是,如果你有更多的带有main方法的类,现在你可以编写更多的任务。

plugins {
    java
}

tasks.register<JavaExec>("runMe") {
    classpath = sourceSets["main"].runtimeClasspath
    mainClass = "org.example.Main"
}


关于JavaExec的更多信息。API文档中的groovy示例不是很有帮助.

相关问题