Maven通过maven-jar-plugin , maven-dependency-plugin , maven-resources-plugin 打包主Jar,分离依赖JAR,配置文件等(主JAR + lib目录 + 配置目录)
<build>
<plugins>
<!-- 主JAR配置 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix> <!-- 依赖路径前缀 -->
<mainClass>com.example.Main</mainClass> <!-- 替换主类 -->
</manifest>
</archive>
</configuration>
</plugin>
<!-- 复制依赖到lib目录 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.2</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<!-- 复制配置文件到lib目录 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.3.1</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<encoding>UTF-8</encoding>
<!--打成jar包后复制到的路径-->
<outputDirectory>
${project.build.directory}/config
</outputDirectory>
<resources>
<resource>
<!--项目中的路径-->
<directory>src/main/resources/</directory>
<includes>
<include>xxxxxx.xml</include>
<include>xxxxxx.bat</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
<!--可配置多个提取复制路径只需要 “<id>”名字不一样即可-->
</executions>
</plugin>
</plugins>
</build>
<!--Fat Jar模式 可把前面maven-jar-plugin替换-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.6.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<!-- 指定主类 -->
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.fast.Main</mainClass>
</transformer>
<!-- 合并同名资源(如 Spring 配置) -->
<!-- <transformer-->
<!-- implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">-->
<!-- <resource>META-INF/spring.handlers</resource>-->
<!-- </transformer>-->
</transformers>
<minimizeJar>true</minimizeJar>
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>s</shadedClassifierName> <!-- 生成 myapp-1.0-shaded.jar -->
</configuration>
</execution>
</executions>
</plugin>