Jartable for the Selenium testng maven project that does not have a main class

advertisements

I have a Selenium TestNG project created with maven. ``

  • There is no main class for this prject
  • Using TestNg.xml file and the same is configured in pom.xml file.
  • Ran 'maven test' from eclispe and the test runs successfully based on the classes defined in testng.xml. no issues here
  • Tried the same from command prompt using mvn test from the project folder and it ran succesfully. no issues here too.

My requirement : Now i want to package this project either to executable jar or are there are any option to make a executable file so that i can schedule the run using a batch file.

To do this I ran the 'mvn package' command and it generated the jar file in the target folder. Now when i try to run this as java -jar myproj-0.0.1-SNAPSHOT.jar i get the message as "no main manifest attribute, in myproj-0.0.1-SNAPSHOT.jar". This is expected behavior since there is no main class in my case.
So i created a main class with just one line to print "text" and added the mainClass below entry in pom.xml

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-jar-plugin</artifactId>
<version>2.3.2</version>
<configuration>
    <archive>
    <manifest>
        <mainClass>mainPackage.MainOne</mainClass>
    </manifest>
</archive>
</configuration>
 </plugin>

Then i ran mvn package and it generated the new jar. Then went to target folder of my project and ran the command as java -jar myProj.jar mainPackage.MainOne, it ran and just printed "text". MY TestNG tests did not run. It just ran main class :(.. What should i do?


Just to let you know that i have found a solution as provided in this How to programmatically call a Maven-task

as i mentioned I did use the main class. So i did the below

-Added the maven-invoker dependency in pom.xml

<dependency>
    <groupId>org.apache.maven.shared</groupId>
    <artifactId>maven-invoker</artifactId>
  <version>2.1.1</version>
</dependency>

  • Updated my main class class as below using this link- http://maven.apache.org/shared/maven-invoker/usage.html

    public class MainOne {
        public static void main(String[] args) {
            InvocationRequest request = new DefaultInvocationRequest();
            request.setPomFile( new File( "pom.xml" ));
            request.setGoals( Collections.singletonList( "install" ));
            Invoker invoker = new DefaultInvoker();
            invoker.setMavenHome(new File(System.getenv("MAVEN_HOME")));
            try {
                invoker.execute( request );
            } catch (MavenInvocationException e) {
                e.printStackTrace();
            }
        }
    }
    
    
  • Then from command prompt ran the command mvn clean package shade:shade

  • This created the jar in the target folder.
  • Copied the jar into Project folder and ran the command java -jar myproj-0.0.1-SNAPSHOT.jar, my tests starting running perfectly!!.