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

JavaGradleExecution

Java Problem Overview


There is simple Eclipse plugin to run Gradle, that just uses command line way to launch gradle.

What is gradle analog for maven compile and run mvn compile exec:java -Dexec.mainClass=example.Example

This way any project with gradle.build could be run.

UPDATE: There was similar question https://stackoverflow.com/questions/16350757/what-is-the-gradle-equivalent-of-mavens-exec-plugin-for-running-java-apps?rq=1 asked before, but solution suggested altering every project 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

:run FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':run'.
> No main class specified	

Java Solutions


Solution 1 - Java

There is no direct equivalent to mvn exec:java in gradle, you need to either apply the application plugin or have a JavaExec task.

application plugin

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

Define a task, let's say 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
:compileJava
:compileGroovy UP-TO-DATE
:processResources UP-TO-DATE
:classes
:execute
I am BOO!

mainClass is a property passed in dynamically at command line. classpath is set to pickup the latest classes.


If you do not pass in the mainClass property, both of the approaches fail as expected.

$ 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'.

Solution 2 - Java

You just need to use the Gradle Application plugin:

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

And then simply gradle run.

As Teresa points out, you can also configure mainClass as a system property and run with a command line argument.

Solution 3 - Java

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

Both gradle build and gradle -PmainClass=foo runApp work with this:

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

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

where you set your default main class.

Solution 4 - Java

You can parameterise it and pass 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;
    }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPaul VerestView Question on Stackoverflow
Solution 1 - JavaFirst ZeroView Answer on Stackoverflow
Solution 2 - JavaVidyaView Answer on Stackoverflow
Solution 3 - JavaMattView Answer on Stackoverflow
Solution 4 - JavaSameera De SilvaView Answer on Stackoverflow