How to create executable JAR for TestNG class using intellij and gradle? – User friendly Tech help

Problem:- How to create a executable JAR for testng class using intellij + gradle.
n
nTool stack :– Intellij (IDE) + Gradle (Build tool) + TestNg (Testing framework)
n
n

Share more learn more  FbG+,Twitter 

n

GITHUB for complete code

n


Step1:– Create a basic java class to display “HelloBuild!”, execution using TestNG.
n

n

public class TestBuild {
@Test
public void test() {
System.out.println("HelloBuild!");
}
}

n

Step2:-Browse to the parent directory of the java class using Terminal/Command prompt > gradle clean 
n
nThis is used to clean any existing build directory (cleaning everything including leftovers from previous builds which are no longer relevant. )
n
n
n
Step3:- gradle build, build the java projects and create the JAR package inside build > libs > test-****.jar
n
n
n
nStep4:- Now our jar package is ready lets browse to this package and try to run the java class
njava – jar test-****.jar
n

n
nThis failed with “no main manifest…”
n
Step5:-add the below line to your build.gradle
n

n

jar {
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
manifest {
attributes 'Main-Class': 'TestBuild'
}
}

n

nMain-Class : ‘Name of the class to be build
n
nStep6.:- if we repeat step2 > step 4 again, this time, you will get the “no main method error” as below. As our code doesn’t contain any main method to start the execution.
n
n
n
nStep7:- Here comes the final solution…Adding the below code inside our main method in the “TestBuild” class and add the testng.xml file containing the TestBuild class name as below.
n
nNow repeat the build process above, this time it will execute the code and print the “HelloBuild!”
n
WOOW WE MADE IT 🙂
n

n
Main Method:-

n

 public static void main(String[] args) {
TestNG testng = new TestNG();
List suites = Lists.newArrayList();
suites.add("/home/ms/Documents/Code/testbuild/src/main/resources/testng.xml");
testng.setTestSuites(suites);
testng.run();
}

n

testng.xml file:-

n









n

nLearn More JAVA 
nLearn Automation

Was this article helpful?
YesNo

Similar Posts