3

I've got a Java/Selenium/TestNG/Maven project. I can successfully create the myproj-0.0.1-SNAPSHOT.jar file. When I try to run it I'm getting an error:

$ java -jar myproj-0.0.1-SNAPSHOT.jar
Error: Could not find or load main class utilities.MavenTestInvoker

My main goal is to be able to run this SNAPSHOT.jar file to run my test cases on a remote machine.

I'm using a build plugin in my pom.xml:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.6.0</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19.1</version>
            <configuration>
                <suiteXmlFiles>
                    <suiteXmlFile>testng.xml</suiteXmlFile>
                </suiteXmlFiles>
                <skipTests>true</skipTests>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>utilities.MavenTestInvoker</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>

When I build the project I'm using the maven command: "mvn clean package shade:shade"

That creates my uber jar.

Here are the dependencies:

<dependencies>
    <dependency>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
        <version>3.0.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.maven.shared</groupId>
        <artifactId>maven-invoker</artifactId>
        <version>2.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>2.4.3</version>
    </dependency>
plus some others
</dependencies>

What am I doing incorrectly?

Anton
  • 761
  • 17
  • 40
  • what is the `mainClass` suppose to do here? also you have not shared the content where `shade` plugin is defined in your pom – Naman Dec 06 '16 at 16:04
  • Also, could you add a brief description about what is your final aim in doing all the above? – Naman Dec 06 '16 at 16:06
  • @nullpointer I'd like to be able to run the newly created jar file: java -jar myproj-0.0.1-SNAPSHOT.jar and have it run my selenium test cases. – Anton Dec 06 '16 at 16:08
  • @nullpointer the mainClass is supposed to be the entry point into my java program. It should get the main method from the utilities package and MavenTestInvoker class. – Anton Dec 06 '16 at 16:10
  • Using maven plugins as dependencies is simply wrong. You can use [exec-maven-plugin](http://www.mojohaus.org/exec-maven-plugin/) to start a jar – khmarbaise Dec 07 '16 at 07:34

2 Answers2

1

The correct usage of including plugin dependencies is as follows, you can merge the plugin dependencies at a single place -

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19.1</version>
                <configuration>
                    <suiteXmlFiles>
                        <suiteXmlFile>testng.xml</suiteXmlFile>
                    </suiteXmlFiles>
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.3.2</version>
            <!--Notice I have removed the configs here. Not required to generate the jar-->
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.0.1</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.shared</groupId>
                <artifactId>maven-invoker</artifactId>
                <version>2.2</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.4.3</version>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

Further you can edit the respective plugin configs within the same declaration.

Also for using maven-shade-plugin and maven-jar-plugin, you might want to look into the samples here and here respectively.

Naman
  • 27,789
  • 26
  • 218
  • 353
  • Those are dependencies. Does it work that way? Or should I use this around the plugins? – Anton Dec 06 '16 at 16:15
  • @WillByers These are plugin dependencies if you are not using any implementation from them in your code. I would suggest using them in ``. That depends on what you are trying to achieve finally. – Naman Dec 06 '16 at 16:20
  • Okay the weight of what you said finally sunk in. I have made the changes to my pom.xml file. I'm still trying to set up the entry point into my jar however. Do I not need the configs so I can state where the main method is? – Anton Dec 06 '16 at 16:49
  • How will the jar know to look for the main method in the utilities.MavenTestInvoker class? – Anton Dec 06 '16 at 16:50
  • @Will This answer is missing the critical point of setting the main class for the Shade Plugin, refer to http://stackoverflow.com/a/40829078/1743880 for that (see the `` configuration there). Also `maven-invoker` isn't a plugin dependency so it shouldn't be under ``. – Tunaki Dec 06 '16 at 17:09
  • @WillByers 1. Why do you want an entry point? 2. How do you want the maven-shade or maven-jar plugin being used in this? – Naman Dec 06 '16 at 17:11
  • @Tunaki Since the OP's aim is not clear in the question about using shade or jar plugin have not added config details in the answer yet. Have alredy shared the links to samples though. – Naman Dec 06 '16 at 17:13
  • 1
    Ah yes I see @nullpointer. I think they want a main class specified so that they can lanch their jar directly with `java -jar ...` instead of specifiying it on the command-line. I guess a clarification is needed yes. – Tunaki Dec 06 '16 at 17:15
  • @Tunaki Yes, I would like to launch my jar directly with java -jar SNAPSHOT.jar. The problem is no main method is specified for the entry point. I have a main method in one of the class files and keep trying to specify it "utilities.MavenTestInvoker" but I keep getting the error, "Error: Could not find or load main class utilities.MavenTestInvoker" – Anton Dec 06 '16 at 17:30
  • @WillByers in that case your mainClass is incorrect, please change it accordingly. – Naman Dec 08 '16 at 19:11
1

Remember that your project must have the right folder structure, for example your code must be in src/main/java

Alberto Perez
  • 1,019
  • 15
  • 17