|
| 1 | +--- |
| 2 | +title: Manage Jar dependencies best practice for Azure HDInsight |
| 3 | +description: Best practice of how to manage Jar dependencies for HDInsight applications safely. |
| 4 | +author: hrasheed-msft |
| 5 | +ms.author: hrasheed |
| 6 | +ms.reviewer: jasonh |
| 7 | +ms.custom: hdinsightactive |
| 8 | +ms.service: hdinsight |
| 9 | +ms.topic: conceptual |
| 10 | +ms.date: 02/05/2020 |
| 11 | +--- |
| 12 | + |
| 13 | +# Jar dependency management best practice |
| 14 | + |
| 15 | +Components in HDInsight cluster have dependencies on third-party libraries. Usually, a specific version of common modules like Guava is referenced by these built-in components. When customers submit an application with its dependencies, they can bring a conflict version of the same module. If customer’s version is in the classpath first, built-in components may throw exceptions because of version incompatibility. While if built-in components inject the dependency to classpath first, customer’s application may throw errors like NoSuchMethod. |
| 16 | + |
| 17 | +To avoid version conflict, consider shading your application dependencies. |
| 18 | + |
| 19 | +## What does package shading mean? |
| 20 | +Shading provides a way to include and rename dependencies. It relocates the classes and rewrites affected bytecode and resources to create a private copy of your dependencies. |
| 21 | + |
| 22 | +## How to shade package? |
| 23 | + |
| 24 | +### Use uber-jar |
| 25 | +Uber-jar is a single jar file that contains both the application jar and its dependencies. The dependencies in Uber-jar are by-default not shaded. In some cases, this may introduce version conflict if other components or applications reference a different version of those libraries. To avoid this, customer can build Uber-Jar file with some (or all) of their dependencies shaded. |
| 26 | + |
| 27 | +### Shade package using Maven |
| 28 | +Maven can build applications written both in Java and Scala. Maven-shade-plugin can help you create a shaded uber-jar easily. |
| 29 | + |
| 30 | +Below example in `pom.xml` shows how to shade package using maven-shade-plugin. `<relocation>…</relocation>` moves classes from package `com.google.guava` into package `com.google.shaded.guava` by moving the corresponding JAR file entries and rewriting the affected bytecode. |
| 31 | + |
| 32 | +After changing `pom.xml`, you can execute `mvn package` to build the shaded uber-jar. |
| 33 | + |
| 34 | +```xml |
| 35 | + <build> |
| 36 | + <plugins> |
| 37 | + <plugin> |
| 38 | + <groupId>org.apache.maven.plugins</groupId> |
| 39 | + <artifactId>maven-shade-plugin</artifactId> |
| 40 | + <version>3.2.1</version> |
| 41 | + <executions> |
| 42 | + <execution> |
| 43 | + <phase>package</phase> |
| 44 | + <goals> |
| 45 | + <goal>shade</goal> |
| 46 | + </goals> |
| 47 | + <configuration> |
| 48 | + <relocations> |
| 49 | + <relocation> |
| 50 | + <pattern>com.google.guava</pattern> |
| 51 | + <shadedPattern>com.google.shaded.guava</shadedPattern> |
| 52 | + </relocation> |
| 53 | + </relocations> |
| 54 | + </configuration> |
| 55 | + </execution> |
| 56 | + </executions> |
| 57 | + </plugin> |
| 58 | + </plugins> |
| 59 | + </build> |
| 60 | +``` |
| 61 | + |
| 62 | +### Shade package using SBT |
| 63 | +SBT is also a build tool for Scala and Java. SBT doesn't have a shade plugin like maven-shade-plugin. You can modify `build.sbt` file to shade packages. |
| 64 | +For example, to shade `com.google.guava`, you can add below command to `build.sbt` file: |
| 65 | + |
| 66 | +```scala |
| 67 | +assemblyShadeRules in assembly := Seq( |
| 68 | + ShadeRule.rename("com.google.guava" -> "com.google.shaded.guava.@1").inAll |
| 69 | +) |
| 70 | +``` |
| 71 | + |
| 72 | +Then you can run `sbt clean` and `sbt assembly` to build the shaded jar file. |
0 commit comments