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 astask 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
- Check Whether the main class name is correct (Including the package name)
- Check whether the Result_Generator file is available in src/main
- 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
Checked VS Unchecked Exception
Why Do == Comparisons with Integer.Valueof(String) Give Different Results for 127 and 128
Why Is This Java Stream Operated Upon Twice
Why Only 1 Public Class in Java File
Java.Lang.Noclassdeffounderror: Org/Hamcrest/Selfdescribing
How to Run a Java Program Without Main Method
Test for Floating Point Equality. (Fe_Floating_Point_Equality)
Convert JSON Date to Java Date
Best Way to Build a Plugin System with Java
Why Can't You Have Multiple Interfaces in a Bounded Wildcard Generic
Consider Defining a Bean of Type 'Package' in Your Configuration [Spring-Boot]
How to Parse Case-Insensitive Strings with Jsr310 Datetimeformatter
Parallel Streams, Collectors and Thread Safety
Is There a MACro Recorder for Eclipse