Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I call a static Java method from Gradle

I have a gradle build script which currently works by simply executing a Java class through it's main method. What I want to know is, how can I call a static method in the same class but not have to go through the main method. The current gradle code is as follows:

import org.apache.tools.ant.taskdefs.condition.Os
apply plugin: 'java'

defaultTasks 'runSimple'

project.ext.set("artifactId", "test-java")

File rootDir = project.getProjectDir()
File targetDir = file("${rootDir}/target")
FileCollection javaClasspath = files("${targetDir}/tools.jar")

task(runSimple, dependsOn: 'classes', type: JavaExec) {
    main = 'com.test.model.JavaTest'
    classpath = javaClasspath
    args 'arg1'
    args 'arg2'
}

And my Java class as follows:

package com.test.model;

public class JavaTest {

    public static void main(String[] args) throws Exception {
        System.out.println("In main");
        anotherMethod(args[0], args[1]);
    }

    public static void anotherMethod(String arg1, String arg2) {
        System.out.println("In anotherMethod");
        System.out.println(arg1 + " " + arg2);
    }
}

This gives me the output:

:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:runSimple
In main
In anotherMethod
arg1 arg2

BUILD SUCCESSFUL

Total time: 2.344 secs

My question is simply how can I skip the main method, and call the method "anotherMethod" directly from the gradle script? The output would then simply be:

In anotherMethod
arg1 arg2

Thanks

like image 995
danielbeddoe Avatar asked Jul 11 '13 10:07

danielbeddoe


People also ask

How do I run a Java program in Gradle?

Run the Java Application The gradle build uses the application plugin so we can run the application from the command line. The gradlew run command will be used to run the application from the command line. First, use the task command to display the added tasks by the plugin: gradlew tasks.

What is JavaExec?

JavaExec. Executes a Java application in a child process. Similar to Exec , but starts a JVM with the given classpath and application class.

How does Gradle know which Java?

Gradle uses whatever JDK it finds in your path.


2 Answers

you have to add the jar or class to the classpath. here is an example with a jar file who contains the class. Inside the file build.gradle add the dependencies. My jar file is in the lib folder the path is lib/MQMonitor.jar.

import mypackage.MyClass
buildscript {
   repositories {
      flatDir name: 'localRepository', dirs: 'lib'
   }
    dependencies {
        classpath name: 'MQMonitor'
    }
}

task myTaskCallJava << {
   MyClass.foo()
}
like image 65
user3271098 Avatar answered Oct 15 '22 03:10

user3271098


Assuming the class is on the buildscript classpath (it should be, since you're calling main from the same class)

task runSimple {
  doLast {
    com.test.model.JavaTest.anotherMethod("foo", "bar")
  }
}

Tested on Gradle 4.6

like image 44
Abhijit Sarkar Avatar answered Oct 15 '22 03:10

Abhijit Sarkar