如何使用 Maven 将一个 jar 和所有依赖项打包到一个新的 jar 中

How to package a jar and all dependencies within a new jar with maven

我有一个包含十个依赖项的 Maven 项目。以前,由于 maven-assembly-plugin:

,我曾经把所有这些都打包在一个罐子里
<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.4</version>
    <executions>
        <execution>
            <id>create-executable-jar</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
            <configuration>
                <descriptors>
                    <descriptor>assembly.xml</descriptor>
                </descriptors>
                <archive>
                    <manifest>
                        <mainClass>myApp.Main</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </execution>
    </executions>
</plugin>

但是现在,我在之前添加了一个步骤。我有一个插件可以生成我的应用程序的 jar。所以我只想让程序集插件将依赖项添加到这个 jar 中。不幸的是,插件不使用这个 jar,而是似乎使用了编译器的结果。

有没有办法指定我希望插件使用之前生成的 jar 而不是编译器的结果?

尝试使用 maven-shade-plugin。你需要这样的东西:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.3</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>