创建单个可执行jar文件Maven

时间:2019-12-18 11:34:21

标签: java maven jar pom.xml executable-jar

下午好,我想使用Maven Shade plugin来打包jar文件。但是每个版本都会创建2个jar文件。

为什么?以及如何打包1个jar文件?

我的pom.xml

<groupId>test</groupId>
<artifactId>task2_maven</artifactId>
<version>1.0</version>

<build>
    <plugins>
        <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>
                        <createDependencyReducedPom>false</createDependencyReducedPom>
                        <transformers>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <manifestEntries>
                                    <Main-Class>test.run.Runner</Main-Class>
                                    <Build-Number>1</Build-Number>
                                </manifestEntries>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>

</build>

我的输出:

enter image description here

请帮助,让我知道发生了什么事。

1 个答案:

答案 0 :(得分:2)

这为什么打扰您?

  1. 在打包阶段,Maven将创建target / artifact-version.jar
  2. Shade插件将运行,并将该jar重命名为target / original-artifact-version.jar并为阴影JAR命名为target / artifact-version.jar

因此,只需忽略仅包含(1.)结果的target / original-artifact-version.jar

编辑:添加此插件并运行mvn clean install,但是正如我试图说的那样,删除文件没有意义

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.7</version>
            <executions>
                <execution>
                    <phase>install</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                        <target>
                            <delete>
                                <fileset dir="${project.build.directory}" includes="original-*.jar" />
                            </delete>
                        </target>
                    </configuration>
                </execution>
            </executions>
        </plugin>