I'm trying to package my test classes in to an executable jar with dependencies using Maven, but I'm struggling to get this right.
This is my pom.xml so far:
<project> <modelVersion>4.0.0</modelVersion> <groupId>com.c0deattack</groupId> <artifactId>executable-tests</artifactId> <version>1.0</version> <packaging>jar</packaging> <name>executable-tests</name> <dependencies> <dependency> <groupId>info.cukes</groupId> <artifactId>cucumber-java</artifactId> <version>1.0.0</version> </dependency> <dependency> <groupId>info.cukes</groupId> <artifactId>cucumber-junit</artifactId> <version>1.0.0</version> </dependency> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>2.21.0</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.4</version> <executions> <execution> <goals> <goal>test-jar</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>1.6</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <finalName>cucumber-tests</finalName> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>cucumber.cli.Main</mainClass> </transformer> </transformers> <artifactSet> <includes> <include>info.cukes:*</include> </includes> </artifactSet> </configuration> </execution> </executions> <dependencies> <dependency> <groupId>com.c0deattack</groupId> <artifactId>executable-tests</artifactId> <version>1.0</version> <type>test-jar</type> </dependency> </dependencies> </plugin> </plugins> </build> </project>
When I execute mvn clean package
the build creates 3 jars:
Cucumber-tests.jar
contains the info.cuke
dependencies but it doesn't contain the executable-tests-1.0-tests.jar
.
I've done all sorts of things to try and have the test classes included but nothing has worked, what am I missing?
Edit: I've pushed my example project to GitHub if any one fancies playing around with it :) https://github.com/C0deAttack/ExecutableTests
You can produce a jar which will include your test classes and resources. To reuse this artifact in an other project, you must declare this dependency with type test-jar : <project> ...
This plugin provides the capability to package the artifact in an uber-jar, including its dependencies and to shade - i.e. rename - the packages of some of the dependencies.
Answering late, but got a working solution which may help future visitors of this question.
I succeed on having a fat jar using only one Maven plugin and including:
compile
scope)test
scope)Which basically means a fat jar with the addition of test classes (and their dependencies). The Maven Jar Plugin and its test-jar
goal would not suit this need. The Maven Shade Plugin and its shadeTestJar
option would not help neither.
So, how to create in Maven a fat jar with test classes and external dependencies?
The Maven Assembly Plugin is a perfect candidate in this case.
Here is a minimal POM sample:
<project> <modelVersion>4.0.0</modelVersion> <groupId>com.sample</groupId> <artifactId>sample-project</artifactId> <version>1.0-SNAPSHOT</version> <build> <plugins> <plugin> <artifactId>maven-assembly-plugin</artifactId> <version>2.3</version> <configuration> <descriptor>src/main/assembly/assembly.xml</descriptor> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> <configuration> <archive> <manifest> <mainClass>com.sample.TestMain</mainClass> </manifest> </archive> </configuration> </execution> </executions> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> </dependencies> </project>
The configuration above is also setting a main class defined in test classes (for a quick check whether it works or not, down on the answer). But that's not enough.
You also need to create a descriptor file, in the src\main\assembly
folder an assembly.xml
file with the following content:
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd"> <id>fat-tests</id> <formats> <format>jar</format> </formats> <includeBaseDirectory>false</includeBaseDirectory> <dependencySets> <dependencySet> <outputDirectory>/</outputDirectory> <useProjectArtifact>true</useProjectArtifact> <unpack>true</unpack> <scope>test</scope> </dependencySet> </dependencySets> <fileSets> <fileSet> <directory>${project.build.directory}/test-classes</directory> <outputDirectory>/</outputDirectory> <includes> <include>**/*.class</include> </includes> <useDefaultExcludes>true</useDefaultExcludes> </fileSet> </fileSets> </assembly>
The configuration above is:
test
scope (which will also take the compile
scope as well)fileset
to include compiled test classes as part of the packaged fat jarfat-tests
classifier (hence your final file will be something like sampleproject-1.0-SNAPSHOT-fat-tests.jar
).Build the jar:
mvn clean compile test-compile assembly:single
Running from the target
folder:
java -jar sampleproject-1.0-SNAPSHOT-fat-tests.jar
We would get the main (from tests classes) executed. The main may invoke others tests or application code (and hence require external dependencies, in both compile
or test
scope) and everything would work fine.
From such a main, you could also invoke all of your test cases as following:
Example of test suite:
import org.junit.runner.RunWith; import org.junit.runners.Suite; import org.junit.runners.Suite.SuiteClasses; @RunWith(Suite.class) @SuiteClasses({ AppTest.class }) public class AllTests { }
Note: in this case the test suite is only concerning the AppTest
sample test.
Then you could have a main class as following:
import org.junit.internal.TextListener; import org.junit.runner.JUnitCore; public class MainAppTest { public static void main(String[] args) { System.out.println("Running tests!"); JUnitCore engine = new JUnitCore(); engine.addListener(new TextListener(System.out)); // required to print reports engine.run(AllTests.class); } }
The main above would then execute the test suite which will in chain execute all of the configured tests.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With