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

TERACytEXAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

for_yanCommented:

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
0
TERACytEXAuthor Commented:
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.
0
TERACytEXAuthor Commented:
I managed to resolve this question using the following plugins in my pom.  The solution copies the test resources to the output directory before packaging.  There may be a more elegant solution to achieve the same result.  If one is found, I would appreciate to be notified of it.

The following pom entries were leveraged and tested using Maven 3:

<plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
                        <id>${project.artifactId}-prepare-test-resources</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <tasks>
                                <echo message="Copying compiled test classes for packaging: from='${project.build.testOutputDirectory}', to='${project.build.outputDirectory}'"/>
                                <copy todir="${project.build.outputDirectory}">
                                    <fileset dir="${project.build.testOutputDirectory}"/>
                                </copy>
                                <echo message="Copying compiled test resources for packaging: from='src/test/resources/', to='${project.build.outputDirectory}/resources'"/>
                                <copy todir="${project.build.outputDirectory}/resources">
                                    <fileset dir="${project.basedir}/src/test/resources/"/>
                                </copy>
                            </tasks>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <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>com.myaddress.ClassWithMainMethod</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>exec-${artifactId}-${version}</finalName>
                            <minimizeJar>true</minimizeJar>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TERACytEXAuthor Commented:
The solution is complete in what is required to achieve the result.  To replicate it, all one has to do is copy and paste.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.