Link to home
Start Free TrialLog in
Avatar of TERACytEX
TERACytEX

asked on

How do I create an executable test jar using Maven 2?

I have a Maven 2 project which only holds TestNG tests:

./src/test/java/org/myAddress/<tests>

I am trying to create an executable JAR that includes and executes these tests.  The goal is to take this jar to any machine and run the unit tests.  I already have a class with a main method next too my tests, which loads and executes the testng tests.

I am having difficultly creating this JAR.

I've tried using the "maven-shade-plugin" where I can create an executable JAR that includes all of the dependencies, but I cannot get it to include the unit tests (or my main class) when I run "mvn clean package".  I have also tried creating an executable JAR using the "maven-jar-plugin" with the "test-jar" goal, but I cannot get the dependencies to be included in the JAR.

Can someone please help me with this?  Can I create an executable jar of tests using maven 2?  If so, how?  I am new to maven so any code you can provide would be very helpful.

Here is what I have currently:
<plugin> 
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>1.5</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>                             
                            <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>org.myAddress.test.MainTestClass</mainClass>
                            </transformer>
                        </transformers>
                        <filters>
                            <filter>
                                <artifact>*:*</artifact>
                                <excludes>
                                    <exclude>META-INF/*.SF</exclude>
                                    <exclude>META-INF/*.DSA</exclude>
                                    <exclude>META-INF/*.RSA</exclude>
                                </excludes>
                            </filter>
                        </filters>
                        <finalName>uber-${artifactId}-${version}</finalName>
                        <minimizeJar>true</minimizeJar>
                    </configuration>
                </execution>
            </executions>
        </plugin>

Open in new window

Avatar of for_yan
for_yan
Flag of United States of America image


also look at this (linke inside the stackoverflow solution of the previous link)
http://maven.apache.org/plugins/maven-jar-plugin/usage.html#How_to_create_a_jar_containing_test_classes
Avatar of TERACytEX
TERACytEX

ASKER

I posted that question on StackOverflow :)  It goes back to my point on getting it to work.  You have to add all the test-scoped dependencies by hand, which is why the shade plug-in is more appealing to me.  Additionally, I do not want to move my tests out of my src/test/java folder, in to the src/main/java folder, which is a step in Maven's 'preferred' way (referenced from the link at the bottom of that page).

Perhaps since I am new to Maven I am missing something.  Some mini-project demonstrating the solution that I could play with would clarify things immensely for me.
ASKER CERTIFIED SOLUTION
Avatar of TERACytEX
TERACytEX

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The solution is complete in what is required to achieve the result.  To replicate it, all one has to do is copy and paste.