Gradle to Execute Java Class (Without Modifying Build.Gradle)

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

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

Gradle Compile & Execute the Single Java Class with third-party jar dependencies using Gradle `buildscript`

It is pretty simple in to compile and execute java code in Gradle with buildscript

buildscript {
dependencies {
classpath files('lib/apache-ant-1.7.0/ant.jar')
}
}

task CreateMessageKeys {
def obj = new mypackage.build.CreateMessageKeysTask();
obj.execute();
}

How to Run Java Code from Gradle at Build Time

The JavaExec Plugin seems to meet your requirements.

This allows you to run a main() method and thereby any Java Code you want – including whatever JSON Schema generation you like.

This other answer also describes pretty much what you want to do.


Adapted from the linked documentation:

apply plugin: 'java'

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

main = 'package.Main'

// arguments to pass to the application
args 'appArg1'
}

As per Jorn's comment below:

You can depend the build task on your custom task: build.dependsOn generateJsonSchema if your custom task is defined as task generateJsonSchema(type: JavaExec) { ... }

Gradle: run a specific java class

You can set gradle project properties with -P, for example:

gradle run -PclassToExecute=com.myClass

and in the script:

mainClassName=classToExecute

Unable to run java class from Gradle

System is not able to find the main class in the mentioned classpath. There could be any one of the below reasons. Please validate all the below

  1. Check Whether the main class name is correct (Including the package name)
  2. Check whether the Result_Generator file is available in src/main
  3. If the Result_Generator file is available in src/test instead of src/main, then the classpath needs to be changed as classpath = sourceSets.test.runtimeClasspath and specify the class name correctly.

Why is it possible to use File class in build.gradle without importing java.io.File?

I think it is feature of Groovy language itself as it imports some java packages by default without needing to specify it:
https://groovy-lang.org/structure.html#_default_imports

Can't find Main when executing a Java class from Gradle

Either move java source files to src/main/java instead of just src. Or set

sourceSet properly
sourceSets.main.java.srcDirs = ['src']

and use

task execute(type:JavaExec) {
main = "myPackage.SomeClass"
classpath = sourceSets.main.runtimeClasspath
}

for more refer : Gradle to execute Java class (without modifying build.gradle)



Related Topics



Leave a reply



Submit