Skip to content

Commit

Permalink
Exclude java.security.Provider SPI configuration from shaded jar (#…
Browse files Browse the repository at this point in the history
…5825)

Motivation:

Recently we noticed that java modules don't work well with Armeria. The
reason was that `META-INF/services/java.security.Provider` is included
in our SPI, but `org.bouncycastle.jce.provider.BouncyCastleProvider`
doesn't exist in the JAR causing an exception in the following
[location](https://github.com/openjdk/jdk/blob/476d2ae69d6f67fdf9e2a9353f224141318690f2/src/java.base/share/classes/jdk/internal/module/ModulePath.java#L554).

Since we shade our custom `BouncyCastleProvider` and use it sparingly,
we probably don't need to also export the SPI implementations.

https://github.com/line/armeria/blob/6dec8498457d7032cbbc192e4070ca2bc5658535/core/src/main/java/com/linecorp/armeria/internal/common/util/MinifiedBouncyCastleProvider.java#L93-L98

Note that this problem can happen if a shaded module provides SPI
implementations and is not necessarily unique to `BouncyCastle`. From a
quick check I don't think (`guava`, `jctools`, `reflections`,
`fastutil`, `futures`) have a similar issue though.

A working version with java 9 modules with the built snapshot version
can be found in the following repository:
https://github.com/jrhee17/my-java-module. Note that this change also
requires line/gradle-scripts#184.

Also, did a local test to ensure there are no unintentional changes
```
user@AL02437565 shaded-diff % tar -zxf /Users/user/Projects/armeria/core/build/libs/armeria-shaded-1.30.0-SNAPSHOT.jar -C new               
user@AL02437565 shaded-diff % tar -zxf /Users/user/Projects/upstream-armeria/core/build/libs/armeria-shaded-1.30.0-SNAPSHOT.jar -C base     
user@AL02437565 shaded-diff % diff -rq new base                                                                                                      
Files new/META-INF/com.linecorp.armeria.versions.properties and base/META-INF/com.linecorp.armeria.versions.properties differ
Only in base/META-INF/services: java.security.Provider
user@AL02437565 shaded-diff %
```

Modifications:

- Added a `shadowExclusions` property which excludes files from the
shadowed JAR

Result:

- Armeria has more compatibility with Java 9 modules.

<!--
Visit this URL to learn more about how to write a pull request
description:

https://armeria.dev/community/developer-guide#how-to-write-pull-request-description
-->
  • Loading branch information
jrhee17 authored Jul 26, 2024
1 parent e2a30e7 commit cbe744e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ org.gradle.jvmargs=-Xmx2048m -XX:+HeapDumpOnOutOfMemoryError
systemProp.https.protocols=TLSv1,TLSv1.1,TLSv1.2

jacocoExclusions=com/linecorp/armeria/internal/common/CurrentJavaVersionSpecific,com/linecorp/armeria/*/scalapb/**,META-INF/versions/**
shadowExclusions=META-INF/services/java.security.Provider
org.gradle.caching = true
3 changes: 3 additions & 0 deletions gradle/scripts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,9 @@ relocations [ { from: "com.google.common", to: "com.doe.john.myproject.shaded.gu
Unshaded tests are disabled by default when a shading task is configured. If you want to run unshaded tests,
you can specify `-PpreferShadedTests=false` option.

If you would like to remove specific files when shading the JAR, you may specify the
`-PshadowExclusions=<comma delimited files>` option.

### Trimming a shaded JAR with `trim` flag

If you shade many dependencies, your JAR will grow huge, even if you only use
Expand Down
8 changes: 8 additions & 0 deletions gradle/scripts/lib/java-shade.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ configure(relocatedProjects) {
exclude '/META-INF/*.RSA'
// Exclude the files generated by Maven
exclude '/META-INF/maven/**'

def shadowExclusions = []
if (rootProject.hasProperty('shadowExclusions')) {
shadowExclusions = rootProject.findProperty('shadowExclusions').split(",")
}
shadowExclusions.each {
exclude it
}
}
tasks.assemble.dependsOn tasks.shadedJar

Expand Down

0 comments on commit cbe744e

Please sign in to comment.