3
##### My POM file pom.xml

http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 company.myproj myproj 0.0.1 jar

<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");
    }
}
#

precon, Maven 3.5.4 and Java 1.8 installed

#
directory structure:
-testproj
   pom.xml
   TestTest.xml
   -src
      -myproj
         TestTest.java

Jars get output to:
-testproj
  -target

On command line I can run:

mvn package -DskipTests=false

to build, run tests and generate jars

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\erstest-0.0.1-jar-with-dependencies.jar TestTest.xml 

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

I have been able to copy dependencies and run the following:

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. I want to just use TestNG as is and package it with my tests in a single jar.

This should work independent of any IDE.

1 Answer 1

2

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>
1
  • i am not able to get data.csv file in side the jar, i ran jar tf sel.jar , but doesn't contain it. How you add files ?
    – PDHide
    Apr 11, 2020 at 9:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.