• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1950
  • Last Modified:

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

0
TERACytEX
Asked:
TERACytEX
  • 3
  • 2
1 Solution
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now