目录

Apache Maven Shade Plugin

目录

这个插件提供了将 artifact 打包在 uber-jar 中的能力,包括它的依赖项和 shade - 即重命名 - 一些依赖项的包。

“uber-jar"这个术语,许多人看到后感到困惑。其实在很多编程语言中会把super叫做uber (因为suber可能是关键字), 这是上世纪80年代开始流行的,比如管superman叫uberman。所以uber-jar从字面上理解就是super-jar,这样的jar不但包含自己代码中的class ,也会包含一些第三方依赖的jar,也就是把自身的代码和其依赖的jar全打包在一个jar里面了,所以就很形象的称其为super-jar

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.3.0</version>
        <configuration>
          <!-- put your configurations here -->
        </configuration>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  ...
</project>
1
mvn package
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.1.1</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <artifactSet>
                            <excludes>
                                <exclude>com.google.code.findbugs:jsr305</exclude>
                            </excludes>
                        </artifactSet>
                        <filters>
                            <filter>
                                <!-- Do not copy the signatures in the META-INF folder.
                                Otherwise, this might cause SecurityExceptions when using the JAR. -->
                                <artifact>*:*</artifact>
                                <excludes>
                                    <exclude>META-INF/*.SF</exclude>
                                    <exclude>META-INF/*.DSA</exclude>
                                    <exclude>META-INF/*.RSA</exclude>
                                </excludes>
                            </filter>
                        </filters>
                        <transformers>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <!-- Replace this with the main class of your job -->
                                <mainClass>my.programs.main.clazz</mainClass>
                            </transformer>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
buildscript {
    repositories {
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }
    dependencies {
        classpath "gradle.plugin.com.github.johnrengelman:shadow:7.1.2"
    }
}
plugins {
    id 'java'
    id 'application'
    // shadow plugin to produce fat JARs
    id "com.github.johnrengelman.shadow" version "7.1.2"
}
apply plugin: "com.github.johnrengelman.shadow"

group 'com.gsm'
version '1.0-SNAPSHOT'
mainClassName = 'com.gsm.DSJdbcDeal'

ext {
    javaVersion = '1.8'
    flinkVersion = '1.13.6'
    scalaBinaryVersion = '2.11'
    slf4jVersion = '2.17.2'
    log4jVersion = '2.17.2'
}

sourceCompatibility = javaVersion
targetCompatibility = javaVersion
tasks.withType(JavaCompile) {
    options.encoding = 'UTF-8'
}

applicationDefaultJvmArgs = ["-Dlog4j.configuration=log4j.xml"]

repositories {
    maven { url "https://maven.aliyun.com/repository/central" }
    mavenCentral()
}


// NOTE: We cannot use "compileOnly" or "shadow" configurations since then we could not run code
// in the IDE or with "gradle run". We also cannot exclude transitive dependencies from the
// shadowJar yet (see https://github.com/johnrengelman/shadow/issues/159).
// -> Explicitly define the // libraries we want to be included in the "flinkShadowJar" configuration!
configurations {
    flinkShadowJar // dependencies which go into the shadowJar

    // always exclude these (also from transitive dependencies) since they are provided by Flink
    flinkShadowJar.exclude group: 'org.apache.flink', module: 'force-shading'
    flinkShadowJar.exclude group: 'com.google.code.findbugs', module: 'jsr305'
    flinkShadowJar.exclude group: 'org.slf4j'
    flinkShadowJar.exclude group: 'log4j'
}


dependencies {
    // --------------------------------------------------------------
    // Compile-time dependencies that should NOT be part of the
    // shadow jar and are provided in the lib folder of Flink
    // --------------------------------------------------------------

    // https://mvnrepository.com/artifact/org.redisson/redisson
    implementation 'org.redisson:redisson:3.16.8'

    // https://mvnrepository.com/artifact/org.apache.flink/flink-clients
    implementation "org.apache.flink:flink-clients_${scalaBinaryVersion}:${flinkVersion}"
    // implementation "org.apache.flink:flink-java:${flinkVersion}"
    // implementation "org.apache.flink:flink-streaming-java_${scalaBinaryVersion}:${flinkVersion}"
    // https://mvnrepository.com/artifact/org.apache.flink/flink-table-api-java-bridge
    implementation "org.apache.flink:flink-table-api-java-bridge_${scalaBinaryVersion}:${flinkVersion}"
    // https://mvnrepository.com/artifact/org.apache.flink/flink-table-planner
    implementation "org.apache.flink:flink-table-planner_${scalaBinaryVersion}:${flinkVersion}"

    // https://mvnrepository.com/artifact/org.apache.flink/flink-connector-jdbc
    implementation "org.apache.flink:flink-connector-jdbc_${scalaBinaryVersion}:${flinkVersion}"

    // https://mvnrepository.com/artifact/org.apache.flink/flink-connector-base
    implementation "org.apache.flink:flink-connector-base:${flinkVersion}"

    // https://mvnrepository.com/artifact/com.ververica/flink-connector-postgres-cdc
    implementation group: 'com.ververica', name: 'flink-connector-postgres-cdc', version: '2.1.1'
    // https://mvnrepository.com/artifact/com.ververica/flink-connector-mysql-cdc
    implementation group: 'com.ververica', name: 'flink-connector-mysql-cdc', version: '2.1.1'

    // https://mvnrepository.com/artifact/mysql/mysql-connector-java
    implementation 'mysql:mysql-connector-java:8.0.21'
    // https://mvnrepository.com/artifact/org.postgresql/postgresql
    implementation group: 'org.postgresql', name: 'postgresql', version: '42.3.3'
    // --------------------------------------------------------------
    // Dependencies that should be part of the shadow jar, e.g.
    // connectors. These must be in the flinkShadowJar configuration!
    // --------------------------------------------------------------
    //flinkShadowJar "org.apache.flink:flink-connector-kafka-0.11_${scalaBinaryVersion}:${flinkVersion}"

    // https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core
    implementation "org.apache.logging.log4j:log4j-core:${log4jVersion}"
// https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-slf4j18-impl
    implementation "org.apache.logging.log4j:log4j-slf4j18-impl:${slf4jVersion}"

//    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
//    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
}

//test {
//    useJUnitPlatform()
//}

// make compileOnly dependencies available for tests:
sourceSets {
    main.compileClasspath += configurations.flinkShadowJar
    main.runtimeClasspath += configurations.flinkShadowJar

    test.compileClasspath += configurations.flinkShadowJar
    test.runtimeClasspath += configurations.flinkShadowJar

    javadoc.classpath += configurations.flinkShadowJar
}

run.classpath = sourceSets.main.runtimeClasspath

jar {
    manifest {
        attributes 'Built-By': System.getProperty('user.name'),
                'Build-Jdk': System.getProperty('java.version')
    }
}

shadowJar {
    configurations = [project.configurations.flinkShadowJar]
    zip64 true
}

tasks.named('wrapper') {
    gradleVersion = '7.4.1'
    distributionType = Wrapper.DistributionType.ALL
}

附录