1

Is there a way to bundle up multiple jars within the same jar file and make sure only one main jar inside it becomes part of classpath of the application which adds this full jar into their classpath. Code inside my main jar will use a custom class loader to load classes present in other Jars in the full jar. I also want to create directory structure for other jars so that I can segregate jars based on use case and load only jars from a within directory inside the full jar.

Any help with any of the requirement above is appreciated. Thanks.

pranjaljain
  • 170
  • 1
  • 3
  • 18
  • 1
    Why? In any case http://one-jar.sourceforge.net/ might be useful. – Thorbjørn Ravn Andersen Jun 17 '21 at 12:44
  • Maybe this helps https://stackoverflow.com/questions/11947037/what-is-an-uber-jar – akortex Jun 17 '21 at 14:19
  • @ThorbjørnRavnAndersen if I am not wrong one-jar.sourceforge.net allows you to bundle main jar with dependencies in lib folder and when main class of main jar is called it magically uses custom class loader to load classes from lib folder. Please correct if if I am wrong. In my case I am writing my own custom class loader. – pranjaljain Jun 18 '21 at 09:32
  • @pranjal sounds about right. You may want to explain in detail why you need something different. Writing a custom class loader in it self is not that difficult so what is the underlying problem you try to solve? – Thorbjørn Ravn Andersen Jun 18 '21 at 09:36
  • Are you trying to change the behavior of an application based on a certain configuration setting and it's not a runtime change? If that's the case you can write your own custom `URLClassLoader`. It gets complicated if it's a run time change. – Indra Basak Jun 23 '21 at 02:32
  • So you want the extra jars **not in the class path at compiletime** but want to load them yourself via methods at runtime? Is this correct? – Nate T Jun 27 '21 at 09:22
  • If so, I would Imagine that you'd need to put the "extra" jars in a resource folder and write custom code to include them. – Nate T Jun 27 '21 at 09:25
  • I think here indeed rather than giving us the solution that you are looking for but the "problem" would be better to help you out. Having this kind of solution is possible but also poses risks when you are adding classes to the classpath that should actually not be there. – BuzZin' Jun 27 '21 at 17:31

1 Answers1

0

you can use Maven Shade plugin because it is a better option to create jar within jar based on custom business logic, if compare with the maven assembly plugin, it provides a class relocating feature, to avoid the issues in the complex structure of the classpath.

sample shade plugin configuration

you can also use many transformations in Shade plugin from the below list as per your requirement.

Manoj2.0S
  • 31
  • 3
  • Doesn't maven need to rebuild when dependencies are completely removed from classpath? If so, that wouldn't be an option at runtime – Nate T Jun 27 '21 at 09:29