创建一个可执行的 JAR 及其所有依赖项,并使用 maven assembly 放入 ZIP 文件

Create an executable JAR with all its dependencies and put in the ZIP file using maven assembly

如问题标题中所述,是否有任何方法可以使用 Maven 程序集创建一个 zip 文件,其中包含 assembly.xml 文件中提到的所有文件,还包含项目的可运行 jar。

目前我要做的是生成项目的 shaded jar 和一个包含普通 jar 和 bin 文件夹中所有依赖项的 zip 文件。

这是我的 assembly.xml :

    <assembly
    xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
    <id>dist</id>
    <formats>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <files>
        <file>
            <source>target/${project.artifactId}-${project.version}.jar</source>
        </file>
    </files>
    <dependencySets>
        <dependencySet>
            <outputDirectory>/lib</outputDirectory>
            <useProjectArtifact>true</useProjectArtifact>
            <scope>compile</scope>
        </dependencySet>
    </dependencySets>
    <fileSets>
        <fileSet>
            <directory>${project.basedir}</directory>
            <includes>
                <include>README*</include>
                <include>LICENSE*</include>
                <include>NOTICE*</include>
                <include>pom.xml</include>
            </includes>
            <useDefaultExcludes>true</useDefaultExcludes>
        </fileSet>
        <fileSet>
            <directory>${project.build.sourceDirectory}/src</directory>
            <useDefaultExcludes>true</useDefaultExcludes>
        </fileSet>
        <fileSet>
            <directory>lib</directory>
            <outputDirectory>lib</outputDirectory>
            <includes>
                <include>*</include>
                <include>*/*</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>images</directory>
            <outputDirectory>images</outputDirectory>
            <includes>
                <include>*</include>
                <include>*/*</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>${project.basedir}</directory>
            <includes>
                <include>*.dll</include>
            </includes>
        </fileSet>
    </fileSets>
</assembly>

他是我现在使用的 插件:

<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>
                                <descriptor>assembly.xml</descriptor>
                            </configuration>
                            <transformers>
                                <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.sgs.terminal.Run</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>

我试过这个解决方案,但它不起作用:Maven: Build runnable jar and then add to zip with maven-assembly-plugin

EDIT: I have edited my pom.xml so there is 2 different execution: the first creates a runnable jar and the second creates the zip but this did not resolve my problem too.

所以问题是我 运行ning maven 命令:mvn package 这就是问题所在。 最后的pom.xml汇编插件:

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>assembly</goal>
                        </goals>
                        <configuration>
                            <descriptors>
                                <descriptor>assembly.xml</descriptor>
                            </descriptors>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

和最终的 assembly.xml 文件:

<assembly
    xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
    <id>dist</id>
    <formats>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <files>
        <file>
            <source>target/${project.artifactId}-${project.version}-jar-with-dependencies.jar</source>
        </file>
    </files>
    <!-- IF YOU NEED TO ADD ALL THE DEPENDENCIES IN ONE FOLDER (lib) -->
    <dependencySets>
        <dependencySet>
            <outputDirectory>/lib</outputDirectory>
            <useProjectArtifact>true</useProjectArtifact>
            <scope>compile</scope>
        </dependencySet>
    </dependencySets>
    <fileSets>
        <fileSet>
            <directory>${project.basedir}</directory>
            <includes>
                <include>README*</include>
                <include>LICENSE*</include>
                <include>NOTICE*</include>
                <include>pom.xml</include>
            </includes>
            <useDefaultExcludes>true</useDefaultExcludes>
        </fileSet>
        <fileSet>
            <directory>${project.build.sourceDirectory}/src</directory>
            <useDefaultExcludes>true</useDefaultExcludes>
        </fileSet>
        ....
       </fileSets>
</assembly>

永远不要忘记 运行 命令 : mvn assembly:assembly -DdescriptorId=jar-with-dependencies package