16

We have a small project with some heavy-weight dependencies (e.g. Spring) of which we only use small parts. Therefore, the JAR we get when packing with all dependencies weighs several megabytes, even for out two-class-server. This seems unnecessary.

Is there a way to restrict JAR assembly to actually used (class) files?

Raphael
  • 8,276
  • 4
  • 58
  • 91

2 Answers2

11

You can use the maven-shade-plugin to create a Jar-with-dependencies (your project and the dependencies merged into one big jar) while limiting the classes or packages that are added to that jar. See the includes / excludes page for reference.

If you don't want to manually specify what needs to be included, perhaps there is a way to integrate ProGuard with your build.

Sean Patrick Floyd
  • 284,665
  • 62
  • 456
  • 576
  • I found lots of useful references on the ProGuard site, thank you! Even if Maven is not able to do this automatically (shame), there seems to be hope with other tools. – Raphael Aug 06 '11 at 16:07
  • 5
    maven shade plugin now allows automatic filtering via `true` configuration – harschware Mar 02 '15 at 17:24
  • I just tried it on a project and it cut my jar size 36%, no additional config needed. – harschware Mar 02 '15 at 17:35
  • `true` reduced my JAR's size by more than 50% but at the same time it also "reduced" the JAR's functionality to 0 by throwing out required classes. Guess I'll have to go with manual exclusion of packages... BTW, I am not really disappointed though because, having worked as a developer for close to a decade now, you just anticipate these things ;-) – zepp133 Apr 17 '18 at 07:26
2

It's not possible to include only classes which are used. But you can exclude dependencies from your depencies to reduce the JAR size. Only drawback: you need to know what you can exclude and what not.

Joachim Rohde
  • 5,791
  • 2
  • 28
  • 45