Skip to content, Skip to search

Changes

Uber-JAR

183 bytes added, 14:27, 12 December 2018
Improve formatting of approaches nested list and example shade plugin code snippet
There are three common methods for constructing an uber-JAR:
<ul>* <li>'''Unshaded.''' Unpack all JAR files, then repack them into a single JAR.** <ul> <li>'''Pro:''' Works with Java's default class loader.</li>** <li>'''Con:''' Files present in multiple JAR files with the same path (e.g., <code>META-INF/services/javax.script.ScriptEngineFactory</code>) will overwrite one another, resulting in faulty behavior.</li>** <li>'''Tools:''' [http://maven.apache.org/plugins/maven-assembly-plugin/ Maven Assembly Plugin], [http://classworlds.codehaus.org/uberjar.html Classworlds Uberjar]</li> </ul>* </li><li>'''Shaded.''' Same as unshaded, but rename (i.e., "shade") all packages of all dependencies.** <ul> <li>'''Pro:''' Works with Java's default class loader. Avoids some (not all) dependency version clashes.</li>** <li>'''Con:''' Files present in multiple JAR files with the same path (e.g., <code>META-INF/services/javax.script.ScriptEngineFactory</code>) will overwrite one another, resulting in faulty behavior. As a work aroundworkaround, adding if you use [[Maven]], you can add the following lines to the shade plugin section of the pom will POM to apply the appending tranform to multiple resources with the same name (in this case <code>org.scijava.plugin.Plugin</code> files). :<codesource lang=xml>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
 
<resource>META-INF/json/org.scijava.plugin.Plugin</resource>
 
</transformer>
</codesource>** </li> <li>'''Tools:''' [http://maven.apache.org/plugins/maven-shade-plugin/ Maven Shade Plugin]</li>* </ul></li><li>'''JAR of JARs'''. The final JAR file contains the other JAR files embedded within.** <ul> <li>'''Pro:''' Avoids dependency version clashes. All resource files are preserved.</li>** <li>'''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.</li>** <li>'''Tools:''' [http://help.eclipse.org/luna/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Freference%2Fref-export-jar.htm Eclipse JAR File Exporter], [http://one-jar.sourceforge.net/ One-JAR].</li> </ul></li>
== Discussion ==
Bureaucrat, emailconfirmed, incoming, administrator, uploaders
11,759
edits