4

I have packaged myjar using maven-shade plugin and am interested to know how it will behave when referenced by a client maven project

Does a maven shaded jar download transitive dependencies when referenced in a maven environment?

Am I able to exclude the dependencies from being packaged by the shade plugin and assume that they will be downloaded by maven when the client references myjar and builds?

Scenarios required: 1. Execute myjar from command line to get an AWT Forms dialog to appear (which will write out a licence file) 2. Referenced in a standard maven by a client project. Maven should download all dependencies transitively.

Therefore to satisfy scenario 1 I want to include the dependencies for forms-1.2.1 but exclude all others to be downloaded by the client during scenario 2.

Raedwald
  • 46,613
  • 43
  • 151
  • 237
Andrew Gray
  • 3,593
  • 3
  • 35
  • 62

3 Answers3

3

When referenced in a maven environment, all dependencies are downloaded using the pom file. However if you have created a shaded jar (uber jar) with all dependencies built in, it can also be used in non-maven (say directly referenced as a jar in your project) environments since all the dependencies are already there.

codelion
  • 1,056
  • 1
  • 11
  • 17
1

your question seems have nothing to do with Shade plugin.

  1. Yes, it will download the transitive dependencies, as long as those dependencies is in the final POM after shading.

  2. Yes I believe you can selectively package some dependencies and exclude them in the final POM. However I will not recommend you doing so. I will only recommend using shade plugin to

    1. create UBER jar for deployment or
    2. Shade dependencies that you really want to be kept internal to your project.

    Selectively packaging some dependencies in your JAR is going to ruin dependency management mechanism of Maven.


Update on why selectively packaging dependencies in JAR will ruin dependency management:

For example, you are developing foo-1.0 which depends on bar-2.0 . Now you decided you want to include class of bar-2.0 in your foo-1.0.jar.

If someone is depending on your foo-1.0, and he want to use bar-2.1, he is going to fall into a trouble: his application class path will contains classes from bar-2.0 (which is part of foo-1.0) and bar-2.1(which the project is declaring by itself), and you can hardly predict which bar the code is using.

Hence one of the use case of shade plugin is to shade the dependencies, for which involves package renaming (i.e. shading as mentioned in my original answer), instead of simply including dependencies in JAR directly.

Adrian Shum
  • 38,812
  • 10
  • 83
  • 131
  • I don't understand why will it "ruin dependency management of Maven", could you elaborate? – Andrew Gray Dec 22 '14 at 03:33
  • Thanks @AdrianShum, I do need the forms-1.2.1 to be in the jar to execute from the command line and don't mind if I get all of form-1.2.1 transitive dependencies. I'm looking for a clear answer on whether scenario 2 will download all the OTHER dependencies at client build time. – Andrew Gray Dec 22 '14 at 04:27
  • that's what I said, you should make sure that's an uber jar just for execution, but not for other to depends on. – Adrian Shum Dec 22 '14 at 06:04
  • So how do I package to serve both scenarios? It must be possible – Andrew Gray Dec 22 '14 at 07:24
  • have a normal jar artifact, which serves for normal dependency. Uber jar can be created with separate classifier, so not to mix up with the main artifact. http://maven.apache.org/plugins/maven-shade-plugin/examples/attached-artifact.html – Adrian Shum Dec 22 '14 at 07:28
-1

Maven works solely off the contents of pom files - both for direct and transitive dependencies. As long as your project's pom states the correct dependencies, a client build will work as expected. The fact that you created your jar using the shade plugin is irrelevant.

JamesB
  • 7,774
  • 2
  • 22
  • 21