[JAVA] Wie man ein schattiertes Glas macht

Was ist ein schattiertes Glas?

Eine der Möglichkeiten, um die Abhängigkeitshölle in Java zu vermeiden.

Angenommen, die ursprüngliche Codebasis hängt von Version X der Bibliothek A ab. Ich möchte dieser Codebasis eine neue Bibliothek B hinzufügen. Bibliothek B hängt jedoch von Version Y von Bibliothek A ab. Betrachten Sie die Situation, in der die Versionen X und Y der Bibliothek A nicht kompatibel sind.

CodeBase
 |- Library A, Version X
 |- Library B
    |- Library A, Version Y # Conflict!!!

Unabhängig davon, ob Sie Version X oder Y von Bibliothek A auswählen, neue Funktionen und vorhandene Funktionen Sie können nicht beide gleichzeitig bewegen.

Hier kommt das schattierte Glas ins Spiel. Die Methode besteht darin, alle Klassen der Klasse Y von Bibliothek A mit Bibliothek B zu bündeln und Bibliothek B ohne Abhängigkeiten zu erstellen.

CodeBase
 |- Library A, Version X
 |- Library B' (include Library A, Version Y)

relocation Die in Bibliothek A enthaltene Klasse und die in Bibliothek B enthaltene Klasse von Bibliothek A befinden sich jedoch im selben Klassenpfad, sodass der Konflikt nicht gelöst werden kann.

libraryA-versionX.jar


com.foo.libraryA.Bar.class
...

libraryB'.jar


com.bar.libraryB.Example.class
...
com.foo.libraryA.Bar.class # Conflict!!
...

Hier kommt der Umzug ins Spiel. Durch Ändern des Paketnamens der in Bibliothek B enthaltenen Bibliothek A werden Klassenkonflikte gelöst.

libraryB'.jar


com.bar.libraryB.Example.class
...
shaded.com.foo.libraryA.Bar.class # No Conflict :)
...

In diesem Beispiel wird "com.foo.libraryA" in "shaded.com.foo.libraryA" geändert. Es geht nicht nur darum, es umzubenennen, sondern auch alle Verweise auf die in Bibliothek B enthaltenen Klassen der Bibliothek A neu zu schreiben.

Wie man ein schattiertes Glas macht

In diesem Artikel werde ich erklären, wie man Maven benutzt.

Fügen Sie die Bibliothek hinzu, die Sie als Beispiel hinzufügen möchten: "com.google.firebase: firebase-admin", Angenommen, die Bibliothek, mit der Sie Konflikte vermeiden möchten, lautet "com.google.guava: guava".

Bereiten Sie zunächst die folgende POM-Datei vor.

pom.xml


<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>shadeex</artifactId>
    <packaging>jar</packaging>
    <version>1.0.0</version>
    <dependencies>
        <!-- https://mvnrepository.com/artifact/com.google.firebase/firebase-admin -->
        <dependency>
            <groupId>com.google.firebase</groupId>
            <artifactId>firebase-admin</artifactId>
            <version>6.13.0</version>
        </dependency>
    </dependencies>
        
</project>

Lassen Sie uns die Abhängigkeit von firebase-admin überprüfen.

$ mvn dependency:tree
...
INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ shadeex ---
[INFO] com.example:shadeex:jar:1.0.0
[INFO] \- com.google.firebase:firebase-admin:jar:6.13.0:compile
[INFO]    +- com.google.api-client:google-api-client:jar:1.30.1:compile
[INFO]    |  +- com.google.oauth-client:google-oauth-client:jar:1.30.1:compile
[INFO]    |  \- com.google.http-client:google-http-client-jackson2:jar:1.30.1:compile
[INFO]    |     \- com.fasterxml.jackson.core:jackson-core:jar:2.9.9:compile
[INFO]    +- com.google.api-client:google-api-client-gson:jar:1.30.1:compile
[INFO]    |  \- com.google.http-client:google-http-client-gson:jar:1.30.1:compile
[INFO]    |     \- com.google.code.gson:gson:jar:2.8.5:compile
[INFO]    +- com.google.http-client:google-http-client:jar:1.30.1:compile
[INFO]    |  +- org.apache.httpcomponents:httpclient:jar:4.5.8:compile
[INFO]    |  |  +- org.apache.httpcomponents:httpcore:jar:4.4.11:compile
[INFO]    |  |  +- commons-logging:commons-logging:jar:1.2:compile
[INFO]    |  |  \- commons-codec:commons-codec:jar:1.11:compile
[INFO]    |  +- com.google.code.findbugs:jsr305:jar:3.0.2:compile
[INFO]    |  +- com.google.j2objc:j2objc-annotations:jar:1.3:compile
[INFO]    |  +- io.opencensus:opencensus-api:jar:0.21.0:compile
[INFO]    |  |  \- io.grpc:grpc-context:jar:1.19.0:compile
[INFO]    |  \- io.opencensus:opencensus-contrib-http-util:jar:0.21.0:compile
[INFO]    +- com.google.api:api-common:jar:1.8.1:compile
[INFO]    |  \- javax.annotation:javax.annotation-api:jar:1.3.2:compile
[INFO]    +- com.google.auth:google-auth-library-oauth2-http:jar:0.17.1:compile
[INFO]    |  +- com.google.auto.value:auto-value-annotations:jar:1.6.6:compile
[INFO]    |  \- com.google.auth:google-auth-library-credentials:jar:0.17.1:compile
[INFO]    +- com.google.cloud:google-cloud-storage:jar:1.91.0:compile
[INFO]    |  +- com.google.cloud:google-cloud-core-http:jar:1.90.0:compile
[INFO]    |  |  +- com.google.cloud:google-cloud-core:jar:1.90.0:compile
[INFO]    |  |  |  +- com.google.protobuf:protobuf-java-util:jar:3.9.1:compile
[INFO]    |  |  |  |  \- com.google.errorprone:error_prone_annotations:jar:2.3.2:compile
[INFO]    |  |  |  \- com.google.api.grpc:proto-google-iam-v1:jar:0.12.0:compile
[INFO]    |  |  +- com.google.http-client:google-http-client-appengine:jar:1.31.0:compile
[INFO]    |  |  \- com.google.api:gax-httpjson:jar:0.65.1:compile
[INFO]    |  |     \- org.threeten:threetenbp:jar:1.3.3:compile
[INFO]    |  \- com.google.apis:google-api-services-storage:jar:v1-rev20190624-1.30.1:compile
[INFO]    +- com.google.cloud:google-cloud-firestore:jar:1.31.0:compile
[INFO]    |  +- com.google.cloud:google-cloud-core-grpc:jar:1.91.3:compile
[INFO]    |  |  +- com.google.api:gax:jar:1.49.1:compile
[INFO]    |  |  \- com.google.api:gax-grpc:jar:1.49.1:compile
[INFO]    |  |     +- io.grpc:grpc-stub:jar:1.23.0:compile
[INFO]    |  |     |  \- io.grpc:grpc-api:jar:1.23.0:compile
[INFO]    |  |     +- io.grpc:grpc-auth:jar:1.23.0:compile
[INFO]    |  |     +- io.grpc:grpc-protobuf:jar:1.23.0:compile
[INFO]    |  |     |  \- io.grpc:grpc-protobuf-lite:jar:1.23.0:compile
[INFO]    |  |     +- io.grpc:grpc-netty-shaded:jar:1.23.0:compile
[INFO]    |  |     |  \- io.grpc:grpc-core:jar:1.23.0:compile (version selected from constraint [1.23.0,1.23.0])
[INFO]    |  |     |     +- com.google.android:annotations:jar:4.1.1.4:compile
[INFO]    |  |     |     +- io.perfmark:perfmark-api:jar:0.17.0:compile
[INFO]    |  |     |     \- io.opencensus:opencensus-contrib-grpc-metrics:jar:0.21.0:compile
[INFO]    |  |     \- io.grpc:grpc-alts:jar:1.23.0:compile
[INFO]    |  |        +- io.grpc:grpc-grpclb:jar:1.23.0:compile
[INFO]    |  |        \- org.apache.commons:commons-lang3:jar:3.5:compile
[INFO]    |  +- com.google.api.grpc:proto-google-cloud-firestore-admin-v1:jar:1.31.0:compile
[INFO]    |  |  +- com.google.protobuf:protobuf-java:jar:3.10.0:compile
[INFO]    |  |  \- com.google.api.grpc:proto-google-common-protos:jar:1.17.0:compile
[INFO]    |  +- com.google.api.grpc:proto-google-cloud-firestore-v1:jar:1.31.0:compile
[INFO]    |  +- com.google.api.grpc:proto-google-cloud-firestore-v1beta1:jar:0.84.0:compile
[INFO]    |  \- io.opencensus:opencensus-contrib-grpc-util:jar:0.24.0:compile
[INFO]    +- com.google.guava:guava:jar:26.0-android:compile
[INFO]    |  +- org.checkerframework:checker-compat-qual:jar:2.5.2:compile
[INFO]    |  \- org.codehaus.mojo:animal-sniffer-annotations:jar:1.14:compile
[INFO]    +- org.slf4j:slf4j-api:jar:1.7.25:compile
[INFO]    +- io.netty:netty-codec-http:jar:4.1.45.Final:compile
[INFO]    |  +- io.netty:netty-common:jar:4.1.45.Final:compile
[INFO]    |  +- io.netty:netty-buffer:jar:4.1.45.Final:compile
[INFO]    |  \- io.netty:netty-codec:jar:4.1.45.Final:compile
[INFO]    +- io.netty:netty-handler:jar:4.1.45.Final:compile
[INFO]    \- io.netty:netty-transport:jar:4.1.45.Final:compile
[INFO]       \- io.netty:netty-resolver:jar:4.1.45.Final:compile
...

Es scheint von "com.google.guava: guava: jar: 26.0-android: compile" abzuhängen. Es ist unangenehm, sich auf die Version für "android" zu verlassen, also schreiben Sie sie in die Version für "jdk" um Fügen wir es der Abhängigkeit hinzu.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>shadeex</artifactId>
    <packaging>jar</packaging>
    <version>1.0.0</version>
    <dependencies>
        <!-- https://mvnrepository.com/artifact/com.google.firebase/firebase-admin -->
        <dependency>
            <groupId>com.google.firebase</groupId>
            <artifactId>firebase-admin</artifactId>
            <version>6.13.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>26.0-jre</version>
        </dependency>
    </dependencies>
        
</project>

Wenn Sie die Abhängigkeit erneut überprüfen, ist dies wie folgt.

$ mvn dependency:tree
...
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ shadeex ---
[INFO] com.example:shadeex:jar:1.0.0
[INFO] +- com.google.firebase:firebase-admin:jar:6.13.0:compile
[INFO] |  +- com.google.api-client:google-api-client:jar:1.30.1:compile
[INFO] |  |  +- com.google.oauth-client:google-oauth-client:jar:1.30.1:compile
[INFO] |  |  \- com.google.http-client:google-http-client-jackson2:jar:1.30.1:compile
[INFO] |  |     \- com.fasterxml.jackson.core:jackson-core:jar:2.9.9:compile
[INFO] |  +- com.google.api-client:google-api-client-gson:jar:1.30.1:compile
[INFO] |  |  \- com.google.http-client:google-http-client-gson:jar:1.30.1:compile
[INFO] |  |     \- com.google.code.gson:gson:jar:2.8.5:compile
[INFO] |  +- com.google.http-client:google-http-client:jar:1.30.1:compile
[INFO] |  |  +- org.apache.httpcomponents:httpclient:jar:4.5.8:compile
[INFO] |  |  |  +- org.apache.httpcomponents:httpcore:jar:4.4.11:compile
[INFO] |  |  |  +- commons-logging:commons-logging:jar:1.2:compile
[INFO] |  |  |  \- commons-codec:commons-codec:jar:1.11:compile
[INFO] |  |  +- io.opencensus:opencensus-api:jar:0.21.0:compile
[INFO] |  |  |  \- io.grpc:grpc-context:jar:1.19.0:compile
[INFO] |  |  \- io.opencensus:opencensus-contrib-http-util:jar:0.21.0:compile
[INFO] |  +- com.google.api:api-common:jar:1.8.1:compile
[INFO] |  |  \- javax.annotation:javax.annotation-api:jar:1.3.2:compile
[INFO] |  +- com.google.auth:google-auth-library-oauth2-http:jar:0.17.1:compile
[INFO] |  |  +- com.google.auto.value:auto-value-annotations:jar:1.6.6:compile
[INFO] |  |  \- com.google.auth:google-auth-library-credentials:jar:0.17.1:compile
[INFO] |  +- com.google.cloud:google-cloud-storage:jar:1.91.0:compile
[INFO] |  |  +- com.google.cloud:google-cloud-core-http:jar:1.90.0:compile
[INFO] |  |  |  +- com.google.cloud:google-cloud-core:jar:1.90.0:compile
[INFO] |  |  |  |  +- com.google.protobuf:protobuf-java-util:jar:3.9.1:compile
[INFO] |  |  |  |  |  \- com.google.errorprone:error_prone_annotations:jar:2.3.2:compile
[INFO] |  |  |  |  \- com.google.api.grpc:proto-google-iam-v1:jar:0.12.0:compile
[INFO] |  |  |  +- com.google.http-client:google-http-client-appengine:jar:1.31.0:compile
[INFO] |  |  |  \- com.google.api:gax-httpjson:jar:0.65.1:compile
[INFO] |  |  |     \- org.threeten:threetenbp:jar:1.3.3:compile
[INFO] |  |  \- com.google.apis:google-api-services-storage:jar:v1-rev20190624-1.30.1:compile
[INFO] |  +- com.google.cloud:google-cloud-firestore:jar:1.31.0:compile
[INFO] |  |  +- com.google.cloud:google-cloud-core-grpc:jar:1.91.3:compile
[INFO] |  |  |  +- com.google.api:gax:jar:1.49.1:compile
[INFO] |  |  |  \- com.google.api:gax-grpc:jar:1.49.1:compile
[INFO] |  |  |     +- io.grpc:grpc-stub:jar:1.23.0:compile
[INFO] |  |  |     |  \- io.grpc:grpc-api:jar:1.23.0:compile
[INFO] |  |  |     +- io.grpc:grpc-auth:jar:1.23.0:compile
[INFO] |  |  |     +- io.grpc:grpc-protobuf:jar:1.23.0:compile
[INFO] |  |  |     |  \- io.grpc:grpc-protobuf-lite:jar:1.23.0:compile
[INFO] |  |  |     +- io.grpc:grpc-netty-shaded:jar:1.23.0:compile
[INFO] |  |  |     |  \- io.grpc:grpc-core:jar:1.23.0:compile (version selected from constraint [1.23.0,1.23.0])
[INFO] |  |  |     |     +- com.google.android:annotations:jar:4.1.1.4:compile
[INFO] |  |  |     |     +- io.perfmark:perfmark-api:jar:0.17.0:compile
[INFO] |  |  |     |     \- io.opencensus:opencensus-contrib-grpc-metrics:jar:0.21.0:compile
[INFO] |  |  |     \- io.grpc:grpc-alts:jar:1.23.0:compile
[INFO] |  |  |        +- io.grpc:grpc-grpclb:jar:1.23.0:compile
[INFO] |  |  |        \- org.apache.commons:commons-lang3:jar:3.5:compile
[INFO] |  |  +- com.google.api.grpc:proto-google-cloud-firestore-admin-v1:jar:1.31.0:compile
[INFO] |  |  |  +- com.google.protobuf:protobuf-java:jar:3.10.0:compile
[INFO] |  |  |  \- com.google.api.grpc:proto-google-common-protos:jar:1.17.0:compile
[INFO] |  |  +- com.google.api.grpc:proto-google-cloud-firestore-v1:jar:1.31.0:compile
[INFO] |  |  +- com.google.api.grpc:proto-google-cloud-firestore-v1beta1:jar:0.84.0:compile
[INFO] |  |  \- io.opencensus:opencensus-contrib-grpc-util:jar:0.24.0:compile
[INFO] |  +- org.slf4j:slf4j-api:jar:1.7.25:compile
[INFO] |  +- io.netty:netty-codec-http:jar:4.1.45.Final:compile
[INFO] |  |  +- io.netty:netty-common:jar:4.1.45.Final:compile
[INFO] |  |  +- io.netty:netty-buffer:jar:4.1.45.Final:compile
[INFO] |  |  \- io.netty:netty-codec:jar:4.1.45.Final:compile
[INFO] |  +- io.netty:netty-handler:jar:4.1.45.Final:compile
[INFO] |  \- io.netty:netty-transport:jar:4.1.45.Final:compile
[INFO] |     \- io.netty:netty-resolver:jar:4.1.45.Final:compile
[INFO] \- com.google.guava:guava:jar:26.0-jre:compile
[INFO]    +- com.google.code.findbugs:jsr305:jar:3.0.2:compile
[INFO]    +- org.checkerframework:checker-qual:jar:2.5.2:compile
[INFO]    +- com.google.j2objc:j2objc-annotations:jar:1.1:compile
[INFO]    \- org.codehaus.mojo:animal-sniffer-annotations:jar:1.14:compile
...

Lassen Sie uns jetzt von hier aus ein schattiertes Glas aufstellen.

pom.xml


<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>shadeex</artifactId>
    <packaging>jar</packaging>
    <version>1.0.0</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.4</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                          ...
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    ... 
</project>

Verwenden Sie das Maven-Shade-Plugin, um ein Shade-Jar zu erstellen.

Das Folgende ist wichtig, lassen Sie uns dies im Detail erklären.

<configuration>
    <artifactSet>
        <includes>
            <include>com.google.guava:guava</include>
            <include>com.google.firebase:firebase-admin</include>
        </includes>
    </artifactSet>
    <relocations>
        <relocation>
            <pattern>com.google</pattern>
            <shadedPattern>com.example.shaded.com.google</shadedPattern>
            <excludes>
                <exclude>com.google.firebase.**</exclude>
            </excludes>
        </relocation>
    </relocations>
</configuration>

Verwenden Sie , um die Liste der im Shaded-Jar enthaltenen Artefakte festzulegen. Dieses Mal werden der Haupt-Firebase-Administrator und die abhängige Bibliothek-Guave angegeben, die Sie bündeln möchten.

Stellen Sie den Umzug mit "" ein. Fügen Sie das Präfix des Java-Pakets hinzu, das Sie in "" verschieben möchten. guava.jar enthält com.google.common. ** undcom.google.thirdparty. ** Also habe ich "com.google" angegeben. Auf der anderen Seite enthält firebase-admin`` com.google.firebase. **, Da es nicht erforderlich ist, firebase-admin selbst zu verschieben, wird es durch <excludes> ausgeschlossen.

Damit sind die Einstellungen abgeschlossen. Lassen Sie es uns ausführen und sehen, welche Art von Glas erstellt wird.

$ mvn package

Shaded-jar wird an target / shadowex-1.0.0.jar ausgegeben. Wenn Sie den Inhalt überprüfen, können Sie sehen, dass die in guava.jar enthaltene Klasse sicherlich verschoben wird.

$ unzip -l target/shadeex-1.0.0.jar  | grep 'com/example/shaded' | head
        0  04-13-2020 17:23   com/example/shaded/
        0  04-13-2020 17:23   com/example/shaded/com/
        0  04-13-2020 17:23   com/example/shaded/com/google/
        0  04-13-2020 17:23   com/example/shaded/com/google/common/
        0  04-13-2020 17:23   com/example/shaded/com/google/common/annotations/
      624  04-13-2020 17:23   com/example/shaded/com/google/common/annotations/Beta.class
      678  04-13-2020 17:23   com/example/shaded/com/google/common/annotations/GwtCompatible.class
      686  04-13-2020 17:23   com/example/shaded/com/google/common/annotations/GwtIncompatible.class
      312  04-13-2020 17:23   com/example/shaded/com/google/common/annotations/VisibleForTesting.class
        0  04-13-2020 17:23   com/example/shaded/com/google/common/base/

Andererseits wird die Klasse von firebase-admin.jar nicht verschoben.

$ unzip -l target/shadeex-1.0.0.jar  | grep -v 'shaded' | grep 'com/google' | head
        0  05-14-2020 19:36   com/google/
        0  05-14-2020 19:36   com/google/firebase/
        0  05-14-2020 19:36   com/google/firebase/messaging/
     1647  05-14-2020 19:36   com/google/firebase/messaging/Notification$Builder.class
     4528  05-14-2020 19:36   com/google/firebase/messaging/AndroidConfig$Builder.class
     1340  05-14-2020 19:36   com/google/firebase/messaging/AndroidFcmOptions$Builder.class
      277  05-14-2020 19:36   com/google/firebase/messaging/TopicManagementResponse$1.class
     1492  05-14-2020 19:36   com/google/firebase/messaging/TopicManagementResponse$Error.class
      409  05-14-2020 19:36   com/google/firebase/messaging/BatchResponse.class
     2910  05-14-2020 19:36   com/google/firebase/messaging/CriticalSound.class

Andere abhängige Bibliotheksklassen sind ebenfalls nicht enthalten.

$ unzip -l target/shadeex-1.0.0.jar  | grep -v 'shaded' | grep -v 'com/google' 
Archive:  target/shadeex-1.0.0.jar
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  06-11-2020 00:24   META-INF/
      135  06-11-2020 00:24   META-INF/MANIFEST.MF
        0  06-11-2020 00:24   META-INF/maven/
        0  06-11-2020 00:24   META-INF/maven/com.example/
        0  06-11-2020 00:24   META-INF/maven/com.example/shadeex/
     2603  06-11-2020 00:24   META-INF/maven/com.example/shadeex/pom.xml
      103  06-10-2020 23:05   META-INF/maven/com.example/shadeex/pom.properties
      599  05-14-2020 19:36   admin_sdk.properties
        0  05-14-2020 19:36   com/
        0  05-14-2020 19:35   META-INF/maven/com.google.firebase/
        0  05-14-2020 19:35   META-INF/maven/com.google.firebase/firebase-admin/
    19833  05-14-2020 19:35   META-INF/maven/com.google.firebase/firebase-admin/pom.xml
      119  05-14-2020 19:36   META-INF/maven/com.google.firebase/firebase-admin/pom.properties
        0  04-13-2020 17:23   META-INF/maven/com.google.guava/
        0  04-13-2020 17:23   META-INF/maven/com.google.guava/guava/
      133  04-13-2020 17:23   META-INF/maven/com.google.guava/guava/pom.properties
    10920  04-13-2020 17:12   META-INF/maven/com.google.guava/guava/pom.xml
        0  04-13-2020 17:23   com/example/
---------                     -------
  8638864                     2699 files

maven-shadow-plugin gibt pom.xml für shaded-jar an dependency-reduced-pom.xml aus.

dependency-reduced-pom.xml


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <artifactId>shadeex</artifactId>
  <version>1.0.0</version>
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.2.4</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <artifactSet>
                <includes>
                  <include>com.google.guava:guava</include>
                  <include>com.google.firebase:firebase-admin</include>
                </includes>
              </artifactSet>
              <relocations>
                <relocation>
                  <pattern>com.google</pattern>
                  <shadedPattern>com.example.shaded.com.google</shadedPattern>
                  <excludes>
                    <exclude>com.google.firebase.**</exclude>
                  </excludes>
                </relocation>
              </relocations>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

Sie können sehen, dass die enthaltenen Abhängigkeiten "guava" und "firebase-admin" verschwunden sind.

Es gibt jedoch ein Problem mit diesem Pom. Das heißt, es gab neben "Guave" noch andere abhängige Bibliotheken für "firebase-admin".

$ mvn dependency:tree
...
INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ shadeex ---
[INFO] com.example:shadeex:jar:1.0.0
[INFO] \- com.google.firebase:firebase-admin:jar:6.13.0:compile
[INFO]    +- com.google.api-client:google-api-client:jar:1.30.1:compile
[INFO]    |  +- com.google.oauth-client:google-oauth-client:jar:1.30.1:compile
[INFO]    |  \- com.google.http-client:google-http-client-jackson2:jar:1.30.1:compile
[INFO]    |     \- com.fasterxml.jackson.core:jackson-core:jar:2.9.9:compile
[INFO]    +- com.google.api-client:google-api-client-gson:jar:1.30.1:compile
[INFO]    |  \- com.google.http-client:google-http-client-gson:jar:1.30.1:compile
[INFO]    |     \- com.google.code.gson:gson:jar:2.8.5:compile
[INFO]    +- com.google.http-client:google-http-client:jar:1.30.1:compile
[INFO]    |  +- org.apache.httpcomponents:httpclient:jar:4.5.8:compile
[INFO]    |  |  +- org.apache.httpcomponents:httpcore:jar:4.4.11:compile
[INFO]    |  |  +- commons-logging:commons-logging:jar:1.2:compile
[INFO]    |  |  \- commons-codec:commons-codec:jar:1.11:compile
[INFO]    |  +- com.google.code.findbugs:jsr305:jar:3.0.2:compile
[INFO]    |  +- com.google.j2objc:j2objc-annotations:jar:1.3:compile
[INFO]    |  +- io.opencensus:opencensus-api:jar:0.21.0:compile
[INFO]    |  |  \- io.grpc:grpc-context:jar:1.19.0:compile
[INFO]    |  \- io.opencensus:opencensus-contrib-http-util:jar:0.21.0:compile
[INFO]    +- com.google.api:api-common:jar:1.8.1:compile
[INFO]    |  \- javax.annotation:javax.annotation-api:jar:1.3.2:compile
[INFO]    +- com.google.auth:google-auth-library-oauth2-http:jar:0.17.1:compile
[INFO]    |  +- com.google.auto.value:auto-value-annotations:jar:1.6.6:compile
[INFO]    |  \- com.google.auth:google-auth-library-credentials:jar:0.17.1:compile
[INFO]    +- com.google.cloud:google-cloud-storage:jar:1.91.0:compile
[INFO]    |  +- com.google.cloud:google-cloud-core-http:jar:1.90.0:compile
[INFO]    |  |  +- com.google.cloud:google-cloud-core:jar:1.90.0:compile
[INFO]    |  |  |  +- com.google.protobuf:protobuf-java-util:jar:3.9.1:compile
[INFO]    |  |  |  |  \- com.google.errorprone:error_prone_annotations:jar:2.3.2:compile
[INFO]    |  |  |  \- com.google.api.grpc:proto-google-iam-v1:jar:0.12.0:compile
[INFO]    |  |  +- com.google.http-client:google-http-client-appengine:jar:1.31.0:compile
[INFO]    |  |  \- com.google.api:gax-httpjson:jar:0.65.1:compile
[INFO]    |  |     \- org.threeten:threetenbp:jar:1.3.3:compile
[INFO]    |  \- com.google.apis:google-api-services-storage:jar:v1-rev20190624-1.30.1:compile
[INFO]    +- com.google.cloud:google-cloud-firestore:jar:1.31.0:compile
[INFO]    |  +- com.google.cloud:google-cloud-core-grpc:jar:1.91.3:compile
[INFO]    |  |  +- com.google.api:gax:jar:1.49.1:compile
[INFO]    |  |  \- com.google.api:gax-grpc:jar:1.49.1:compile
[INFO]    |  |     +- io.grpc:grpc-stub:jar:1.23.0:compile
[INFO]    |  |     |  \- io.grpc:grpc-api:jar:1.23.0:compile
[INFO]    |  |     +- io.grpc:grpc-auth:jar:1.23.0:compile
[INFO]    |  |     +- io.grpc:grpc-protobuf:jar:1.23.0:compile
[INFO]    |  |     |  \- io.grpc:grpc-protobuf-lite:jar:1.23.0:compile
[INFO]    |  |     +- io.grpc:grpc-netty-shaded:jar:1.23.0:compile
[INFO]    |  |     |  \- io.grpc:grpc-core:jar:1.23.0:compile (version selected from constraint [1.23.0,1.23.0])
[INFO]    |  |     |     +- com.google.android:annotations:jar:4.1.1.4:compile
[INFO]    |  |     |     +- io.perfmark:perfmark-api:jar:0.17.0:compile
[INFO]    |  |     |     \- io.opencensus:opencensus-contrib-grpc-metrics:jar:0.21.0:compile
[INFO]    |  |     \- io.grpc:grpc-alts:jar:1.23.0:compile
[INFO]    |  |        +- io.grpc:grpc-grpclb:jar:1.23.0:compile
[INFO]    |  |        \- org.apache.commons:commons-lang3:jar:3.5:compile
[INFO]    |  +- com.google.api.grpc:proto-google-cloud-firestore-admin-v1:jar:1.31.0:compile
[INFO]    |  |  +- com.google.protobuf:protobuf-java:jar:3.10.0:compile
[INFO]    |  |  \- com.google.api.grpc:proto-google-common-protos:jar:1.17.0:compile
[INFO]    |  +- com.google.api.grpc:proto-google-cloud-firestore-v1:jar:1.31.0:compile
[INFO]    |  +- com.google.api.grpc:proto-google-cloud-firestore-v1beta1:jar:0.84.0:compile
[INFO]    |  \- io.opencensus:opencensus-contrib-grpc-util:jar:0.24.0:compile
[INFO]    +- com.google.guava:guava:jar:26.0-android:compile
[INFO]    |  +- org.checkerframework:checker-compat-qual:jar:2.5.2:compile
[INFO]    |  \- org.codehaus.mojo:animal-sniffer-annotations:jar:1.14:compile
[INFO]    +- org.slf4j:slf4j-api:jar:1.7.25:compile
[INFO]    +- io.netty:netty-codec-http:jar:4.1.45.Final:compile
[INFO]    |  +- io.netty:netty-common:jar:4.1.45.Final:compile
[INFO]    |  +- io.netty:netty-buffer:jar:4.1.45.Final:compile
[INFO]    |  \- io.netty:netty-codec:jar:4.1.45.Final:compile
[INFO]    +- io.netty:netty-handler:jar:4.1.45.Final:compile
[INFO]    \- io.netty:netty-transport:jar:4.1.45.Final:compile
[INFO]       \- io.netty:netty-resolver:jar:4.1.45.Final:compile

Dependency-reduced-pom.xml entfernt jedoch die Abhängigkeiten dieser Bibliotheken.

Die Option zur Lösung dieses Problems lautet "". Durch Angabe dieser Option werden alle abhängigen Bibliotheken von "firebase-admin" als direkte Abhängigkeiten von "dependency-reduced-pom.xml" aufgelistet und anschließend "firebase-admin" und "guava" entfernt. ..

Daher können die Abhängigkeiten von Bibliotheken, die nicht enthalten sind, korrekt belassen werden.

Die endgültige pom.xml sieht folgendermaßen aus:

pom.xml


<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>shadeex</artifactId>
    <packaging>jar</packaging>
    <version>1.0.0</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.4</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <promoteTransitiveDependencies>true</promoteTransitiveDependencies>
                            <artifactSet>
                                <includes>
                                    <include>com.google.guava:guava</include>
                                    <include>com.google.firebase:firebase-admin</include>
                                </includes>
                            </artifactSet>
                            <relocations>
                                <relocation>
                                    <pattern>com.google</pattern>
                                    <shadedPattern>com.example.shaded.com.google</shadedPattern>
                                    <excludes>
                                        <exclude>com.google.firebase.**</exclude>
                                    </excludes>
                                </relocation>
                            </relocations>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/com.google.firebase/firebase-admin -->
        <dependency>
            <groupId>com.google.firebase</groupId>
            <artifactId>firebase-admin</artifactId>
            <version>6.13.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>29.0-jre</version>
        </dependency>
    </dependencies>
        
</project>

Andererseits ist die Ausgabe von "dependency-reduced-pom.xml" durch Ausführen des "mvn-Pakets" wie folgt.

dependency-reduced-pom.xml


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <artifactId>shadeex</artifactId>
  <version>1.0.0</version>
  <build>
    ...
  </build>
  <dependencies>
    <dependency>
      <groupId>com.google.api-client</groupId>
      <artifactId>google-api-client</artifactId>
      <version>1.30.1</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>com.google.oauth-client</groupId>
      <artifactId>google-oauth-client</artifactId>
      <version>1.30.1</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>com.google.http-client</groupId>
      <artifactId>google-http-client-jackson2</artifactId>
      <version>1.30.1</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>2.9.9</version>
      <scope>compile</scope>
    </dependency>
    ...
  </dependencies>
</project>

Die abhängigen Bibliotheken von firebase-admin bleiben korrekt.

Jetzt müssen Sie nur noch dieses JAR und dependency-reduced-pom.xml in Ihr internes Maven-Repository hochladen.

Verweise

Apache Maven Shade Plugin

Recommended Posts

Wie man ein schattiertes Glas macht
Java - So erstellen Sie JTable
[Schienen] Wie man Samen macht
So erstellen Sie einen Java-Container
So erstellen Sie einen JDBC-Treiber
So erstellen Sie einen Begrüßungsbildschirm
So erstellen Sie ein Jenkins-Plug-In
Wie erstelle ich ein Maven-Projekt?
So erstellen Sie ein Java-Array
[Android] So erstellen Sie ein Dialogfragment
So erstellen Sie eine Java-Kalenderzusammenfassung
Wie erstelle ich einen Discord Bot (Java)
So entwickeln Sie OpenSPIFe
So rufen Sie AmazonSQSAsync auf
Verwendung von Map
Wie schreibe ich Rails
Wie benutzt man rbenv?
Verwendung mit_option
Verwendung von fields_for
Verwendung der Karte
So verkleinern Sie das Spring Boot Docker-Image
Verwendung von collection_select
So machen Sie doppelte Prüflogik lesbarer
Wie benutzt man Twitter4J
Wie benutzt man active_hash! !!
So installieren Sie Docker
Verwendung von MapStruct
Verwendung von TreeSet
So erstellen Sie eine leichte JRE für den Vertrieb
So deinstallieren Sie Rails
So installieren Sie Docker-Maschine
[Verwendung des Etiketts]
Wie schreibe ich Docker-Compose
Wie man Identität benutzt
Wie erstelle ich Unity Native Plugin (Android-Version)
Wie man Hash benutzt
Wie schreibe ich Mockito
So erstellen Sie Docker-Compose
So installieren Sie MySQL
So schreiben Sie eine Migrationsdatei
Wie man android-midi-lib baut
Verwendung von Dozer.mapper
Wie benutzt man Gradle?
Verwendung von org.immutables
Verwendung von java.util.stream.Collector
Verwendung von VisualVM
Wie man einen imposanten Android-Musikplayer macht
[Java] Wie man mehrere for-Schleifen einzeln macht
Verwendung von Map
Wie man einen Schrägstrich zurückschlägt \
So verketten Sie Zeichenfolgen