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
I have been working on this too. You know I like the function of eclipse and intellij with the run with Junit options, and I want to do this using command line and gradle.
If you could accept putting your test method in the directory of 'test' directory of gradle. I actually have a fair solution.
This my test class which is in src/test/java/com/udacity/gradle/TestClass.java
Then the under below is the file of my build.gradle
Simple idea you know this is a test class so I use the test task of gradle. And to specify which method to use, I added a Test filter which could specify down to method.
Then you could just run
Then you could find in console that you have what you want in there. However, remember to add
if you don't do this, gradle would swallow your console output. But even if you don't add this line. You could read a test log in the directory of
...../build/test-results/test/TEST-com.udacity.gradle.TestClass.xml
There are well organized test reports output in it.
Assuming the class is on the buildscript classpath (it should be, since you're calling
main
from the same class)Tested on Gradle 4.6
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 islib/MQMonitor.jar
.If you want to execute a static method you will need to add the class to the Gradle build script's classpath.
To add the code to the build scripts classpath if your code is in a repository:
To add the code to the build scripts classpath if you code is built locally (I didn't test this one):
Then you should be able to call that method just like any other: