apache / apache/maven-shade-plugin
[MSHADE-402] Shading produces empty test jar
- Dominant language
- Java
- Stars
- 188
- Forks
- 103
- Avg merge
- 16h 39m
- Merged PRs (30d)
- 4
Description
**[Cédric Champeau](https://issues.apache.org/jira/secure/ViewProfile.jspa?name=melix)** opened **[MSHADE-402](https://issues.apache.org/jira/browse/MSHADE-402?redirect=false)** and commented
Dear maintainers,
I'm working with the shade plugin in the context of a workaround for a Windows bug: when invoking a CLI in Windows, there's a maximum length that you can use. The GraalVM `native-image` is invoked on CLI with a "classpath" argument which easily exceeds that limit. There are several options to workaround this but in this context, the most promising, which worked for the Gradle plugin, was to use _shading_.
For the main artifact, this technique works properly: we can use the "shade" plugin to generate a shaded jar which is then passed to `native-image`. However, we also have support for _unit test_ execution "as a native image", which means that we need to build a native image which includes test classes and the test runtime (e.g JUnit Platform).
Unfortunately, this technique doesn't work for Maven. First of all, let's see how the build is configured:
```xml
org.apache.maven.plugins
maven-shade-plugin
3.2.4
package
shade
true
true
```
As you can see, I'm using `shadeTestJar`, but if you run the package phase, you will see that the resulting jar is _empty_. After trying to figure out why, it turns out there are 2 different reasons:
* the first one seems to be that you _must_ generate a test jar before hand. This isn't mentioned in the docs, but I could get a _non empty_ jar by adding this:
```xml
org.apache.maven.plugins
maven-jar-plugin
3.2.0
test-jar
```
* the second problem is that the shaded generated test jar doesn't include the test dependencies
After debugging, I noticed that the `project.getDependencies()` method did **not** return the test dependencies. The javadocs for this method state that they _should_ include the test dependencies, since when this code is executed, the "test" phase has been executed. Debugging showed me that the "resolved dependencies" cache was correct, but that a filter only kept the "runtime" dependencies. The mojo descriptor mentions:
`@Mojo`( name = "shade", defaultPhase = LifecyclePhase.PACKAGE, threadSafe = true, requiresDependencyResolution = ResolutionScope.RUNTIME )
So I guess the problem comes from the "requiresDependencyResolution" value, which should be set to "TEST" for the test jar to include test dependencies.
However, I suspect that by changing this parameter to TEST, then the _main_ jar would suddenly start including test dependencies, which isn't expected.
Let me know if my analysis is correct. I'm open to suggestions on how to workaround this problem.
Thanks!
---
**Affects:** 3.2.4
Contributor guide
No contributing guide indexed for this repository
Research direction
Start by reproducing the package phase with shadeTestJar enabled, first without and then with the maven-jar-plugin test-jar goal. Inspect the shade mojo's requiresDependencyResolution setting and project.getDependencies() behavior. Done means the shaded test jar is non-empty and includes test dependencies without adding them to the main shaded jar.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- build-system
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100