Including dependencies in a jar with Maven

后端 未结 13 1971
盖世英雄少女心
盖世英雄少女心 2020-11-22 00:39

Is there a way to force maven(2.0.9) to include all the dependencies in a single jar file?

I have a project the builds into a single jar file. I want the classes fro

13条回答
  •  渐次进展
    2020-11-22 01:06

    http://fiji.sc/Uber-JAR provides an excellent explanation of the alternatives:

    There are three common methods for constructing an uber-JAR:

    1. Unshaded. Unpack all JAR files, then repack them into a single JAR.
      • Pro: Works with Java's default class loader.
      • Con: Files present in multiple JAR files with the same path (e.g., META-INF/services/javax.script.ScriptEngineFactory) will overwrite one another, resulting in faulty behavior.
      • Tools: Maven Assembly Plugin, Classworlds Uberjar
    2. Shaded. Same as unshaded, but rename (i.e., "shade") all packages of all dependencies.
      • Pro: Works with Java's default class loader. Avoids some (not all) dependency version clashes.
      • Con: Files present in multiple JAR files with the same path (e.g., META-INF/services/javax.script.ScriptEngineFactory) will overwrite one another, resulting in faulty behavior.
      • Tools: Maven Shade Plugin
    3. JAR of JARs. The final JAR file contains the other JAR files embedded within.
      • Pro: Avoids dependency version clashes. All resource files are preserved.
      • Con: Needs to bundle a special "bootstrap" classloader to enable Java to load classes from the wrapped JAR files. Debugging class loader issues becomes more complex.
      • Tools: Eclipse JAR File Exporter, One-JAR.

提交回复
热议问题