maven打jar包时如何把libs中的jar包一并打入
时间: 2023-09-09 16:08:33 浏览: 180
可以通过使用Maven Shade插件来实现将依赖的jar包打包到一个可执行jar包中。
首先,需要在pom.xml文件中添加以下插件配置:
```xml
<build>
<plugins>
<!--其他插件-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
<!--其他插件-->
</plugins>
</build>
```
然后,执行`mvn clean package`命令即可将依赖的jar包打包到一个可执行jar包中。
需要注意的是,如果有多个main方法,需要在插件的配置中指定正确的main class:
```xml
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.example.MainClass</mainClass>
</transformer>
</transformers>
</configuration>
```
阅读全文