I use the shade maven plugin to build my project so that all of its dependencies are included in one jar (this makes it easier to run it on Hadoop). Shade seems to exclude my test code by default, which is understandable. Since I would like to run integration tests against my cluster, I am hoping to setup another profile to build a separate jar for this purpose. Is there any way to configure this plugin to also include test code?
-
This is more of a workaround than a direct answer, but I ended up adding a test-jar goal to the project that contains the tests and adding another dependency on that project (with the 'tests' classifier) to my shale-built project. Not ideal, but now my shale built jar includes tests as well. – Patrick Marchwiak Mar 01 '11 at 18:54
-
Can you provide an example of what you did? I'm new to maven and don't understand how you'd add goals to the project or classifiers to the dependencies. – divegeek Oct 28 '11 at 13:02
-
1Add this to the build section of the pom whose tests you want to reuse: `
maven-jar-plugin test-jar package test-jar com.abc blah 1.0 tests test -
@PatrickMarchwiak Could you please show what you did to solve this problem? – Eidan Spiegel Jun 23 '14 at 09:19
-
I got a working solution for this topic using the Assembly plugin: http://stackoverflow.com/a/36058365/5606016 – A_Di-Matteo Mar 17 '16 at 10:51
-
Possible duplicate of [How to include test classes in Jar created by maven-shade-plugin?](https://stackoverflow.com/questions/10307652/how-to-include-test-classes-in-jar-created-by-maven-shade-plugin) – gcandal Aug 17 '18 at 13:58
5 Answers
These last couple of answers are messy workarounds for a broken feature at best. The fact of the matter remains that there is a bug in maven-shade-plugin
. In the meantime I've investigated and root-caused the bug, and created a patch. Now I hope someone at Apache includes it soon and then finally the shadeTestJar
feature can work like it's supposed to.

- 301
- 2
- 3
With version 2.2 of the maven-shade-plugin, they added a "shadeTestJar" option (see MSHADE-158): http://maven.apache.org/plugins/maven-shade-plugin/shade-mojo.html#shadeTestJar
However, I tried using this and couldn't get it to work. Here's my plugin config:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<shadeTestJar>true</shadeTestJar>
</configuration>
</execution>
</executions>
</plugin>
The "...-tests.jar" file has no entries, but the main shaded jar looks fine (although it doesn't contain any test classes).
Also, this question duplicates this other question, although the accepted answer isn't real satisfying: How to include test classes in Jar created by maven-shade-plugin?
-
I also tried for a long while this path but couldn't get it working, I ended up using the Assembly Plugin instead and it worked fine, check my solution here http://stackoverflow.com/a/36058365/5606016 – A_Di-Matteo Mar 17 '16 at 10:52
I've managed to make it work by adding :
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.9.1</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${project.basedir}/src/test/java/</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>

- 51
- 1
- 1
-
For this to work, any test dependencies need to be made non-test dependencies. – Adam Cataldo Jan 10 '16 at 06:02
-
1This solution works for me. Regarding comment "any test dependencies need to be made non-test dependencies" this could be solved by introducing separate shade profile with added all dependencies need for test-classes. Otherwise your shaded JAR will be not workable. – dmaidaniuk Apr 20 '18 at 14:22
Using the maven-shade-plugin
as explained by ~steve-k above is correct, unfortunately due to a bug shadeTestJar
doesn't work and the resulting test JAR is empty.

- 301
- 2
- 3
Try include
ing your test packages like this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.2.2</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<includes>
<include>org.apache.maven:*</include>
</includes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>

- 3,869
- 4
- 30
- 44
-
I don't think this will work unless that include encompasses an artifact that includes test code. Even then I would have to add an include for each of my dependencies which is redundant. – Patrick Marchwiak Mar 01 '11 at 18:58