1

I have a multi-module Maven build where one module shades a library, and another module uses the shared library.

If I run mvn test, this results in package com.example.shaded.javax.el7 does not exist.

If I run mvn package test, the compilation and tests pass.

So what I would like is for the shaded module to not just compile but be shaded (packaged) running maven-shade-plugin:3.2.1:shade before the dependent module is compiled.

Is it possible to introduce such a target dependency in maven in the pom.xml?

Binding the shade plugin to other phases than package yields this error message:

[ERROR] The project main artifact does not exist. This could have the following
[ERROR] reasons:
[ERROR] - You have invoked the goal directly from the command line. This is not
[ERROR]   supported. Please add the goal to the default lifecycle via an
[ERROR]   <execution> element in your POM and use "mvn package" to have it run.
[ERROR] - You have bound the goal to a lifecycle phase before "package". Please
[ERROR]   remove this binding from your POM such that the goal will be run in
[ERROR]   the proper phase.

tkruse
  • 10,222
  • 7
  • 53
  • 80

1 Answers1

1

Update There doesn't seem to be a clean way to do this.

Insert a new Maven module between the two which does the shading. Attach shade to the phase test in this module. Don't forget to depend on the new module.

The code fails for mvn test because this doesn't create any JARs and therefore doesn't invoke the Shade plugin.

You may be tempted to change the phase in which Shade is run to generate-test-resources or test in the existing module but that could cause other problems if something happens in later phases of the Maven lifecycle. That's why I suggest to create a new module where you have no other side effects.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • I edited the question, the solution you suggest seems to be prevented by the shade plugin. – tkruse Apr 03 '19 at 04:10
  • I think there is something weird going on with "mvn test" because that doesn't create JARs. So I'm wondering what's on the classpath for the next module. Maven probably adds the `target/classes/` folder in this case. – Aaron Digulla Apr 03 '19 at 10:54
  • Can you try to convert the test into an integration test? See https://stackoverflow.com/questions/1399240/how-do-i-get-my-maven-integration-tests-to-run – Aaron Digulla Apr 03 '19 at 13:46
  • Hm... This feels very wrong. It might be possible, but I think the correct answer to my question would be: It is not cleanly possible. So I'd rather do shading differently. – tkruse Apr 05 '19 at 04:09