2

I am totally new to Maven. I Have a maven project(selenium project) that uses TestNG annotations. That means I do not have main method in entire project. I want to create a single fat JAR file with mvn package. I have gone through few articles for mvn package but could not find any relevant stuff. How can we achieve this for the projects having no main methods.

EDIT

ON checking some more articles, I added below Class with main method as follow

public class MainTest
{
    public static void main(String[] args)
      {
        TestListenerAdapter tla = new TestListenerAdapter();
        TestNG testng = new TestNG();
        testng.setTestClasses(new Class[] { AnnotationTest.class });
        testng.addListener(tla);
        testng.run();
     }
}

Where AnnotationTest is a class where all the annotations are used. When I run generated *one-jar.jar file from command line, I get ClassNotFoundException:AnnotationTest . Since it is maven project my test.class files are in /target/test-classes. How do I make it available in main method.

MKay
  • 818
  • 9
  • 32
  • what exactly is your problem? what have you already tried? see http://stackoverflow.com/help/how-to-ask – drkthng Sep 03 '15 at 11:13
  • @drkthng: Thank you for above link. I may not have framed my query correctly. Please check Edit. – MKay Sep 03 '15 at 11:25

1 Answers1

2

I was trying to figure this same thing out so in case anybody else winds up here, this is how I got around that problem:

I created a main class that ran all the classes in my testng.xml file like so:

public class MainClass {

    public static void main(String[] args){
        // Get the path to testng.xml
        String xmlPath = System.getProperty("user.dir") + "/testng.xml";

        // Run all the tests in testng.xml
        TestNG testng = new TestNG();
        List<String> suites = Lists.newArrayList();
        suites.add(xmlPath);
        testng.setTestSuites(suites);
        testng.run();
    }
}

Then you can use Maven to build the fat jar for you as explained here: How can I create an executable JAR with dependencies using Maven?

This only helps if you already have a testng.xml file though, not that it has to be named that, you just need all the classes or tests already defined in an xml, something like this:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite name="Your Suite Name">

    <test name="TestName">
        <classes>
            <class name="com.fullyQualified.ClassName"></class>
            <class name="com.fullyQualified.ClassName2"></class>
            <class name="com.fullyQualified.ClassName3"></class>
        </classes>
    </test>

</suite>