3
votes

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?

2
what is the mainClass suppose to do here? also you have not shared the content where shade plugin is defined in your pom - Naman
Also, could you add a brief description about what is your final aim in doing all the above? - Naman
@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. - James Franken
@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. - James Franken
Using maven plugins as dependencies is simply wrong. You can use exec-maven-plugin to start a jar - khmarbaise

2 Answers

1
votes

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.

1
votes

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