-1

I can use Maven to generate a directory containing my tests and dependencies and run them from the command line using "java -cp" . But I cannot get this to work in a single jar, without recreating a testng main and needing to pass testng xmls in and such. Thinking this is a simple thing being overlooked.

fundamentally I believe this may be a question of, can you create a jar that runs a main in an included package dependency? I may not be asking or stating correctly, if so, some help with that would also be appreciated.

precon, Maven 3.5.4 and Java 1.8 installed

I HAVE been able to "copy dependencies" and run the following usual TestNG command line:

java -cp %CD%\target\*; org.testng.TestNG TestTest.xml

but this is not as nice as just getting everything into one jar.

If possible, I don't want to have to recreate the TestNG main and further pass xmls etc. which I have done. I want to just use TestNG as is and package it with my tests in a single jar, and run like this: java -jar %CD%\target\myproj-0.0.1-jar-with-dependencies.jar TestTest.xml

from My POM file pom.xml

<properties>
   <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   <skipTests>true</skipTests>
</properties>
<dependencies>
    <!-- https://mvnrepository.com/artifact/org.testng/testng -->
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>7.0.0-beta3</version>
        <scope>provided</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.141.59</version>
        <scope>provided</scope>
    </dependency>

</dependencies>


<build>

    <!-- Source directory configuration -->
    <sourceDirectory>src</sourceDirectory>

    <plugins>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>

        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <mainClass>org.testng.TestNG</mainClass>
                    </manifest>
                </archive>

            </configuration>
        </plugin> 

            <!-- // Following plugin executes the testng tests  -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.14.1</version>
            <configuration>
                <!-- // Suite testng xml file to consider for test execution  -->
                <suiteXmlFiles>
                    <suiteXmlFile>testtest.xml</suiteXmlFile>
                </suiteXmlFiles>
                  <skipTests>${skipTests}</skipTests>
            </configuration>
        </plugin>
    </plugins>
 </build>

my testNG xml TestTest.xml

<groups>
    <run>
      <include name="basic"/> 
    </run>      
</groups>

<classes>
  <class name="myproj.TestTest"/>
</classes>

my tests in TestTest.java

package myproj;

import org.testng.annotations.Test;

public class TestTest{
    @Test(groups = { "basic" })
    public void Test05BasicPASS() {
        System.out.println("This is test 5, Basic Pass");
    }
}

directory structure:

-testproj
   pom.xml
   TestTest.xml
   -src
      -myproj
         TestTest.java

Jars get output to:
-testproj
  -target

On command line I can successfully run: mvn package -DskipTests=false to build, run tests and generate jars with maven

After running and the jar exists, I believe I should be able to run the following and have the tests run as well: java -jar %CD%\target\myproj-0.0.1-jar-with-dependencies.jar TestTest.xml however I get: Error: Could not find or load main class org.testng.TestNG

Net goal, to be able to build a project using testng and run my tests with testNG xmls on the command line.

This should work independent of any IDE.

Eelke
  • 21
  • 6

1 Answers1

0

If you want to build so callled "fat jar" - a single executable .jar containing all dependencies and invoking TestNG main class I would recommend going for Maven Shade Plugin:

  1. Remove the following lines from your pom.xml

    <scope>provided</scope>
    
  2. Add the following Maven Shade plugin definition to create an executable jar:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.2.1</version>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>shade</goal>
                </goals>
                <configuration>
                    <transformers>
                        <transformer
                                implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                            <mainClass>org.testng.TestNG</mainClass>
                        </transformer>
                    </transformers>
                </configuration>
            </execution>
        </executions>
    </plugin>
    
  3. Package the jar
  4. Once done you should be able to invoke your test as jar -jar myproj-0.0.1.jar

Full pom.xml just in case:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>myproj</artifactId>
    <version>0.0.1</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <skipTests>true</skipTests>
    </properties>
    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.testng/testng -->
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>7.0.0-beta3</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.141.59</version>
        </dependency>

    </dependencies>


    <build>

        <!-- Source directory configuration -->
        <sourceDirectory>src</sourceDirectory>

        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>org.testng.TestNG</mainClass>
                        </manifest>
                    </archive>

                </configuration>
            </plugin>

            <!-- // Following plugin executes the testng tests  -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.14.1</version>
                <configuration>
                    <!-- // Suite testng xml file to consider for test execution  -->
                    <suiteXmlFiles>
                        <suiteXmlFile>testtest.xml</suiteXmlFile>
                    </suiteXmlFiles>
                    <skipTests>${skipTests}</skipTests>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>org.testng.TestNG</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>
Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • Nifty! Your solution works! Great. I'm not allowed to up-tick the solution though :/ Perhaps someone can on my behalf, or tell me how given my low status? – Eelke Jun 12 '19 at 21:26
  • Just want to confirm, looks like the maven-assembly-plugin would be redundant than? It can be removed? I tried it, and it looks like it works, but not sure if I am overlooking something. – Eelke Jun 12 '19 at 21:41