maven-shade-plugin:如何将所有依赖项和自定义本地jar添加到可执行jar?

时间:2019-03-15 12:46:05

标签: java maven maven-shade-plugin

Maven 3 Java 1.8

在我的pom.xml中

    <dependencies>
        <dependency>
            <groupId>com.myproject/groupId>
            <artifactId>mixed-pojo</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/libs/external-pojo-1.0-SNAPSHOT.jar</systemPath>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-stdlib-jdk8</artifactId>
            <version>${kotlin.version}</version>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-test</artifactId>
            <version>${kotlin.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.zaxxer</groupId>
            <artifactId>HikariCP</artifactId>
            <version>3.3.1</version>
    </dependency>
...

我需要创建一个可执行jar 。所以我使用插件maven-shade-plugin 这是pom的代码段:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.2.1</version>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>shade</goal>
                </goals>
                <configuration>
                    <transformers>
                        <transformer
                                implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                            <mainClass>com.myproject.AppStarterKt</mainClass>
                        </transformer>
                    </transformers>
                    <artifactSet>
                        <includes>
                            <include>${project.basedir}/libs/external-pojo-1.0-SNAPSHOT.jar</include>
                            <include>com.zaxxer:HikariCP</include>
                        </includes>
                    </artifactSet>
                </configuration>
            </execution>
        </executions>
    </plugin>

其结果是生成executable jar

myproject-1.0-SNAPSHOT-shaded.jar

好。 但是问题是我需要在插件中手动添加所有依赖项,如下所示:

<artifactSet>
                                <includes>
                                    <include>${project.basedir}/libs/external-pojo-1.0-SNAPSHOT.jar</include>
                                    <include>com.zaxxer:HikariCP</include>
                                </includes>
                            </artifactSet>

但是我有 50 个依赖项。如何将所有依赖项添加到可执行jar?

P.S。

external-pojo-1.0-SNAPSHOT.jar

是位于项目文件夹/libs

上的外部自定义jar

1 个答案:

答案 0 :(得分:1)

默认情况下,所有<artifactSet>范围内的依赖项都将包含在带阴影的uber JAR中。从<configuration>中删除<artifactSet>,以获取默认行为。

<includes>配置选项用于覆盖默认值。指定*选项时,要包括的工件白名单。

自插件版本1.3起,您可以使用通配符?<includes> <include>**</include> </includes> ,但是在您的示例中这不是必需的:

def fill_missing_colnames(colnames):

    valid_colnames = ['Z', 'K', 'C', 'T', 'A', 'E', 'F', 'G']

    missing = list(set(valid_colnames) - set(colnames))
    if len(missing) > 0:  
        for i, col in enumerate(colnames):
            if col not in valid_colnames and len(missing) > 0:
                colnames[i] = missing.pop(0)
    return colnames