如何使用maven打包fatjar?

如何使用Maven打包FatJar?

FatJar是什么?

我们在写Java程序的时候可能会依赖其他的库,如果我们打包jar的时候能够将我们所依赖的jar包一起打包进来,那么用户就可以直接启动该jar包而不用考虑其他依赖问题,包含依赖的jar包就是fatjar。

如何打包FatJar?

将以下代码复制到pom.xml文件中

<build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.2.0</version>
        <executions>
          <execution>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <transformers>
                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                  <manifestEntries>
                    <Main-Class>${app.main.class}</Main-Class>
                    <X-Compile-Source-JDK>${maven.compile.source}</X-Compile-Source-JDK>
                    <X-Compile-Target-JDK>${maven.compile.target}</X-Compile-Target-JDK>
                  </manifestEntries>
                </transformer>
              </transformers>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
     

替换${app.main.class}为你的main函数所在的类
${maven.compile.source}是你的项目源码的JDK版本如1.8
${maven.compile.target}是你的项目编译的Java版本如1.8

猜你喜欢

转载自blog.csdn.net/alisonyu/article/details/82822051