简体中文 繁體中文 English 日本語 Deutsch 한국 사람 بالعربية TÜRKÇE português คนไทย Français

站内搜索

搜索

活动公告

11-02 12:46
10-23 09:32
通知:本站资源由网友上传分享,如有违规等问题请到版务模块进行投诉,将及时处理!
10-23 09:31
10-23 09:28
通知:签到时间调整为每日4:00(东八区)
10-23 09:26

Maven项目打jar包完全指南 手把手教你解决依赖冲突打包失败等问题轻松实现一键部署

3万

主题

317

科技点

3万

积分

大区版主

木柜子打湿

积分
31893

财Doro三倍冰淇淋无人之境【一阶】立华奏小樱(小丑装)⑨的冰沙以外的星空【二阶】

发表于 2025-8-24 20:50:01 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
引言

Maven作为Java项目中最流行的构建工具之一,其强大的依赖管理和项目构建能力深受开发者喜爱。然而,在实际开发过程中,许多开发者常常遇到Maven打包的各种问题,如依赖冲突、打包失败、无法正确包含依赖等。本文将全面介绍Maven项目打包为jar的各种方法,手把手教你解决常见问题,最终实现一键部署的完整流程。

Maven基础知识回顾

在深入探讨Maven打包之前,我们先简要回顾一些Maven的基础知识。

Maven项目结构

标准的Maven项目结构如下:
  1. my-app
  2. ├── pom.xml          # Maven项目配置文件
  3. ├── src
  4. │   ├── main
  5. │   │   ├── java    # Java源代码
  6. │   │   └── resources # 资源文件
  7. │   └── test
  8. │       ├── java    # 测试Java源代码
  9. │       └── resources # 测试资源文件
  10. └── target          # 构建输出目录
复制代码

Maven生命周期

Maven有三套相互独立的生命周期:clean、default和site。其中default生命周期是我们最关心的,它包含以下阶段:

1. validate:验证项目是否正确
2. compile:编译项目的源代码
3. test:使用合适的单元测试框架测试编译后的源代码
4. package:将编译后的代码打包成可分发的格式,如JAR
5. verify:运行任何检查以验证包是否有效且符合质量标准
6. install:将包安装到本地仓库,以作为本地其他项目的依赖
7. deploy:将最终包复制到远程仓库以共享给其他开发者和项目

本文主要关注package阶段及其相关内容。

Maven打包的基本方法

使用maven-jar-plugin打包

Maven默认使用maven-jar-plugin来打包项目。这个插件将项目的主要代码(src/main/java目录下的代码)编译后打包成一个JAR文件。

在pom.xml中,你可以这样配置maven-jar-plugin:
  1. <project>
  2.   ...
  3.   <build>
  4.     <plugins>
  5.       <plugin>
  6.         <groupId>org.apache.maven.plugins</groupId>
  7.         <artifactId>maven-jar-plugin</artifactId>
  8.         <version>3.2.0</version>
  9.         <configuration>
  10.           <archive>
  11.             <manifest>
  12.               <addClasspath>true</addClasspath>
  13.               <mainClass>com.example.MainClass</mainClass>
  14.             </manifest>
  15.           </archive>
  16.         </configuration>
  17.       </plugin>
  18.     </plugins>
  19.   </build>
  20.   ...
  21. </project>
复制代码

执行打包命令:
  1. mvn package
复制代码

这将在target目录下生成一个JAR文件。但是,这个JAR文件默认不包含项目的依赖,所以如果运行这个JAR文件,可能会因为缺少依赖类而失败。

使用maven-assembly-plugin打包

maven-assembly-plugin是Maven中用于创建自定义分发包的插件,它可以将项目及其依赖打包成一个可执行的JAR文件。

首先,在pom.xml中添加maven-assembly-plugin的配置:
  1. <project>
  2.   ...
  3.   <build>
  4.     <plugins>
  5.       <plugin>
  6.         <groupId>org.apache.maven.plugins</groupId>
  7.         <artifactId>maven-assembly-plugin</artifactId>
  8.         <version>3.3.0</version>
  9.         <configuration>
  10.           <archive>
  11.             <manifest>
  12.               <mainClass>com.example.MainClass</mainClass>
  13.             </manifest>
  14.           </archive>
  15.           <descriptorRefs>
  16.             <descriptorRef>jar-with-dependencies</descriptorRef>
  17.           </descriptorRefs>
  18.         </configuration>
  19.         <executions>
  20.           <execution>
  21.             <id>make-assembly</id>
  22.             <phase>package</phase>
  23.             <goals>
  24.               <goal>single</goal>
  25.             </goals>
  26.           </execution>
  27.         </executions>
  28.       </plugin>
  29.     </plugins>
  30.   </build>
  31.   ...
  32. </project>
复制代码

执行打包命令:
  1. mvn package
复制代码

这将生成两个JAR文件:一个是普通的JAR文件,另一个是带有所有依赖的JAR文件(通常命名为your-project-name-jar-with-dependencies.jar)。

使用maven-shade-plugin打包

maven-shade-plugin提供了将项目依赖打包到单个JAR文件中的能力,同时还可以解决依赖冲突、重命名包等问题。

配置maven-shade-plugin:
  1. <project>
  2.   ...
  3.   <build>
  4.     <plugins>
  5.       <plugin>
  6.         <groupId>org.apache.maven.plugins</groupId>
  7.         <artifactId>maven-shade-plugin</artifactId>
  8.         <version>3.2.4</version>
  9.         <executions>
  10.           <execution>
  11.             <phase>package</phase>
  12.             <goals>
  13.               <goal>shade</goal>
  14.             </goals>
  15.             <configuration>
  16.               <transformers>
  17.                 <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
  18.                   <mainClass>com.example.MainClass</mainClass>
  19.                 </transformer>
  20.               </transformers>
  21.             </configuration>
  22.           </execution>
  23.         </executions>
  24.       </plugin>
  25.     </plugins>
  26.   </build>
  27.   ...
  28. </project>
复制代码

执行打包命令:
  1. mvn package
复制代码

这将生成一个包含所有依赖的单一JAR文件。

处理依赖问题的策略

在Maven项目中,依赖管理是一个复杂但关键的任务。以下是处理依赖问题的几种策略:

依赖范围(Scope)

Maven中的依赖范围决定了依赖在哪些类路径中可用。常见的依赖范围有:

• compile:默认范围,在编译、测试和运行时都可用
• provided:在编译和测试时可用,但在运行时由JDK或容器提供
• runtime:在测试和运行时可用,但在编译时不需要
• test:只在测试时可用
• system:类似于provided,但需要显式提供JAR文件

正确设置依赖范围可以避免不必要的依赖被打包进最终的JAR文件。
  1. <dependency>
  2.   <groupId>javax.servlet</groupId>
  3.   <artifactId>javax.servlet-api</artifactId>
  4.   <version>4.0.1</version>
  5.   <scope>provided</scope>
  6. </dependency>
复制代码

可选依赖(Optional Dependencies)

可选依赖用于标记那些不是项目运行所必需的依赖。当其他项目依赖你的项目时,这些可选依赖不会被传递。
  1. <dependency>
  2.   <groupId>com.example</groupId>
  3.   <artifactId>optional-library</artifactId>
  4.   <version>1.0.0</version>
  5.   <optional>true</optional>
  6. </dependency>
复制代码

依赖排除(Exclusions)

当你想排除某个依赖传递过来的其他依赖时,可以使用exclusions元素。
  1. <dependency>
  2.   <groupId>com.example</groupId>
  3.   <artifactId>example-library</artifactId>
  4.   <version>1.0.0</version>
  5.   <exclusions>
  6.     <exclusion>
  7.       <groupId>unwanted.group</groupId>
  8.       <artifactId>unwanted-artifact</artifactId>
  9.     </exclusion>
  10.   </exclusions>
  11. </dependency>
复制代码

创建可执行JAR包

创建可执行JAR包是Maven打包的常见需求,以下是几种实现方式:

使用maven-jar-plugin设置主类

如前所述,可以在maven-jar-plugin中设置主类:
  1. <plugin>
  2.   <groupId>org.apache.maven.plugins</groupId>
  3.   <artifactId>maven-jar-plugin</artifactId>
  4.   <version>3.2.0</version>
  5.   <configuration>
  6.     <archive>
  7.       <manifest>
  8.         <mainClass>com.example.MainClass</mainClass>
  9.         <addClasspath>true</addClasspath>
  10.       </manifest>
  11.     </archive>
  12.   </configuration>
  13. </plugin>
复制代码

使用maven-assembly-plugin创建fat jar

maven-assembly-plugin可以将所有依赖打包到一个JAR文件中:
  1. <plugin>
  2.   <groupId>org.apache.maven.plugins</groupId>
  3.   <artifactId>maven-assembly-plugin</artifactId>
  4.   <version>3.3.0</version>
  5.   <configuration>
  6.     <archive>
  7.       <manifest>
  8.         <mainClass>com.example.MainClass</mainClass>
  9.       </manifest>
  10.     </archive>
  11.     <descriptorRefs>
  12.       <descriptorRef>jar-with-dependencies</descriptorRef>
  13.     </descriptorRefs>
  14.   </configuration>
  15.   <executions>
  16.     <execution>
  17.       <id>make-assembly</id>
  18.       <phase>package</phase>
  19.       <goals>
  20.         <goal>single</goal>
  21.       </goals>
  22.     </execution>
  23.   </executions>
  24. </plugin>
复制代码

使用maven-shade-plugin创建uber jar

maven-shade-plugin不仅可以创建包含所有依赖的JAR文件,还可以处理依赖冲突:
  1. <plugin>
  2.   <groupId>org.apache.maven.plugins</groupId>
  3.   <artifactId>maven-shade-plugin</artifactId>
  4.   <version>3.2.4</version>
  5.   <executions>
  6.     <execution>
  7.       <phase>package</phase>
  8.       <goals>
  9.         <goal>shade</goal>
  10.       </goals>
  11.       <configuration>
  12.         <transformers>
  13.           <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
  14.             <mainClass>com.example.MainClass</mainClass>
  15.           </transformer>
  16.         </transformers>
  17.         <filters>
  18.           <filter>
  19.             <artifact>*:*</artifact>
  20.             <excludes>
  21.               <exclude>META-INF/*.SF</exclude>
  22.               <exclude>META-INF/*.DSA</exclude>
  23.               <exclude>META-INF/*.RSA</exclude>
  24.             </excludes>
  25.           </filter>
  26.         </filters>
  27.       </configuration>
  28.     </execution>
  29.   </executions>
  30. </plugin>
复制代码

使用spring-boot-maven-plugin(适用于Spring Boot项目)

如果你使用的是Spring Boot项目,可以使用spring-boot-maven-plugin创建可执行的JAR文件:
  1. <plugin>
  2.   <groupId>org.springframework.boot</groupId>
  3.   <artifactId>spring-boot-maven-plugin</artifactId>
  4.   <version>2.5.5</version>
  5.   <executions>
  6.     <execution>
  7.       <goals>
  8.         <goal>repackage</goal>
  9.       </goals>
  10.     </execution>
  11.   </executions>
  12. </plugin>
复制代码

解决依赖冲突

依赖冲突是Maven项目中的常见问题,当不同的依赖引入了同一库的不同版本时,就会发生冲突。以下是几种解决依赖冲突的方法:

使用dependency:tree分析依赖树

首先,使用Maven的dependency:tree命令查看项目的依赖树:
  1. mvn dependency:tree
复制代码

这将显示项目的所有依赖及其传递依赖,帮助你识别冲突。

使用dependencyManagement管理依赖版本

在pom.xml中使用dependencyManagement元素可以统一管理依赖的版本:
  1. <dependencyManagement>
  2.   <dependencies>
  3.     <dependency>
  4.       <groupId>com.example</groupId>
  5.       <artifactId>example-library</artifactId>
  6.       <version>1.0.0</version>
  7.     </dependency>
  8.   </dependencies>
  9. </dependencyManagement>
复制代码

这样,当项目中的任何模块引入example-library时,如果没有显式指定版本,将使用dependencyManagement中指定的版本。

使用maven-shade-plugin重定位包

maven-shade-plugin可以重定位(重命名)包,从而解决冲突:
  1. <plugin>
  2.   <groupId>org.apache.maven.plugins</groupId>
  3.   <artifactId>maven-shade-plugin</artifactId>
  4.   <version>3.2.4</version>
  5.   <executions>
  6.     <execution>
  7.       <phase>package</phase>
  8.       <goals>
  9.         <goal>shade</goal>
  10.       </goals>
  11.       <configuration>
  12.         <relocations>
  13.           <relocation>
  14.             <pattern>org.conflicting.package</pattern>
  15.             <shadedPattern>org.shaded.package</shadedPattern>
  16.           </relocation>
  17.         </relocations>
  18.       </configuration>
  19.     </execution>
  20.   </executions>
  21. </plugin>
复制代码

排除冲突依赖

如前所述,可以使用exclusions排除特定的依赖:
  1. <dependency>
  2.   <groupId>com.example</groupId>
  3.   <artifactId>example-library</artifactId>
  4.   <version>1.0.0</version>
  5.   <exclusions>
  6.     <exclusion>
  7.       <groupId>conflicting.group</groupId>
  8.       <artifactId>conflicting-artifact</artifactId>
  9.     </exclusion>
  10.   </exclusions>
  11. </dependency>
复制代码

高级打包技巧

多模块项目打包

对于多模块项目,通常在父POM中配置打包插件,然后在子模块中继承这些配置。

父POM示例:
  1. <project>
  2.   <groupId>com.example</groupId>
  3.   <artifactId>parent-project</artifactId>
  4.   <version>1.0.0</version>
  5.   <packaging>pom</packaging>
  6.   
  7.   <modules>
  8.     <module>module1</module>
  9.     <module>module2</module>
  10.     <module>application</module>
  11.   </modules>
  12.   
  13.   <build>
  14.     <pluginManagement>
  15.       <plugins>
  16.         <plugin>
  17.           <groupId>org.apache.maven.plugins</groupId>
  18.           <artifactId>maven-shade-plugin</artifactId>
  19.           <version>3.2.4</version>
  20.           <executions>
  21.             <execution>
  22.               <phase>package</phase>
  23.               <goals>
  24.                 <goal>shade</goal>
  25.               </goals>
  26.               <configuration>
  27.                 <transformers>
  28.                   <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
  29.                     <mainClass>com.example.MainClass</mainClass>
  30.                   </transformer>
  31.                 </transformers>
  32.               </configuration>
  33.             </execution>
  34.           </executions>
  35.         </plugin>
  36.       </plugins>
  37.     </pluginManagement>
  38.   </build>
  39. </project>
复制代码

自定义Assembly Descriptor

maven-assembly-plugin允许你自定义打包描述符,创建更复杂的分发包。

首先,创建一个assembly描述符文件(如src/main/assembly/assembly.xml):
  1. <assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.0"
  2.           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.           xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.0 http://maven.apache.org/xsd/assembly-2.1.0.xsd">
  4.   <id>bin</id>
  5.   <formats>
  6.     <format>zip</format>
  7.   </formats>
  8.   <includeBaseDirectory>false</includeBaseDirectory>
  9.   <fileSets>
  10.     <fileSet>
  11.       <directory>${project.build.directory}</directory>
  12.       <outputDirectory>/</outputDirectory>
  13.       <includes>
  14.         <include>*.jar</include>
  15.       </includes>
  16.     </fileSet>
  17.   </fileSets>
  18.   <dependencySets>
  19.     <dependencySet>
  20.       <outputDirectory>/lib</outputDirectory>
  21.       <useProjectArtifact>false</useProjectArtifact>
  22.       <scope>runtime</scope>
  23.     </dependencySet>
  24.   </dependencySets>
  25. </assembly>
复制代码

然后,在pom.xml中引用这个描述符:
  1. <plugin>
  2.   <groupId>org.apache.maven.plugins</groupId>
  3.   <artifactId>maven-assembly-plugin</artifactId>
  4.   <version>3.3.0</version>
  5.   <configuration>
  6.     <descriptors>
  7.       <descriptor>src/main/assembly/assembly.xml</descriptor>
  8.     </descriptors>
  9.   </configuration>
  10.   <executions>
  11.     <execution>
  12.       <id>make-assembly</id>
  13.       <phase>package</phase>
  14.       <goals>
  15.         <goal>single</goal>
  16.       </goals>
  17.     </execution>
  18.   </executions>
  19. </plugin>
复制代码

使用Maven Profile

Maven Profile允许你根据不同的环境或需求创建不同的构建配置。
  1. <profiles>
  2.   <profile>
  3.     <id>development</id>
  4.     <build>
  5.       <plugins>
  6.         <plugin>
  7.           <groupId>org.apache.maven.plugins</groupId>
  8.           <artifactId>maven-jar-plugin</artifactId>
  9.           <version>3.2.0</version>
  10.           <configuration>
  11.             <archive>
  12.               <manifest>
  13.                 <mainClass>com.example.dev.MainClass</mainClass>
  14.               </manifest>
  15.             </archive>
  16.           </configuration>
  17.         </plugin>
  18.       </plugins>
  19.     </build>
  20.   </profile>
  21.   <profile>
  22.     <id>production</id>
  23.     <build>
  24.       <plugins>
  25.         <plugin>
  26.           <groupId>org.apache.maven.plugins</groupId>
  27.           <artifactId>maven-shade-plugin</artifactId>
  28.           <version>3.2.4</version>
  29.           <executions>
  30.             <execution>
  31.               <phase>package</phase>
  32.               <goals>
  33.                 <goal>shade</goal>
  34.               </goals>
  35.               <configuration>
  36.                 <transformers>
  37.                   <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
  38.                     <mainClass>com.example.prod.MainClass</mainClass>
  39.                   </transformer>
  40.                 </transformers>
  41.               </configuration>
  42.             </execution>
  43.           </executions>
  44.         </plugin>
  45.       </plugins>
  46.     </build>
  47.   </profile>
  48. </profiles>
复制代码

激活特定Profile:
  1. mvn package -P development
复制代码


  1. mvn package -P production
复制代码

一键部署实现

实现一键部署通常需要结合Maven和其他工具或脚本。以下是几种实现一键部署的方法:

使用Maven Deploy插件

Maven的deploy插件可以将构建的产物部署到远程仓库:
  1. <plugin>
  2.   <groupId>org.apache.maven.plugins</groupId>
  3.   <artifactId>maven-deploy-plugin</artifactId>
  4.   <version>3.0.0-M1</version>
  5.   <configuration>
  6.     <skip>false</skip>
  7.   </configuration>
  8. </plugin>
复制代码

在settings.xml中配置远程仓库:
  1. <settings>
  2.   ...
  3.   <servers>
  4.     <server>
  5.       <id>deployment-repo</id>
  6.       <username>deployment-user</username>
  7.       <password>deployment-password</password>
  8.     </server>
  9.   </servers>
  10.   ...
  11. </settings>
复制代码

在pom.xml中配置远程仓库:
  1. <distributionManagement>
  2.   <repository>
  3.     <id>deployment-repo</id>
  4.     <url>https://repository.example.com/releases</url>
  5.   </repository>
  6.   <snapshotRepository>
  7.     <id>deployment-repo</id>
  8.     <url>https://repository.example.com/snapshots</url>
  9.   </snapshotRepository>
  10. </distributionManagement>
复制代码

执行部署:
  1. mvn deploy
复制代码

结合Shell/Batch脚本实现一键部署

创建一个Shell脚本(deploy.sh):
  1. #!/bin/bash
  2. # 1. 构建项目
  3. mvn clean package
  4. # 2. 上传到服务器
  5. scp target/*.jar user@server:/path/to/deployment/directory/
  6. # 3. 重启服务
  7. ssh user@server "systemctl restart my-service"
  8. echo "Deployment completed!"
复制代码

给脚本添加执行权限:
  1. chmod +x deploy.sh
复制代码

执行一键部署:
  1. ./deploy.sh
复制代码

使用Maven Antrun插件

Maven Antrun插件允许你在Maven构建过程中运行Ant任务,可以用于实现一键部署:
  1. <plugin>
  2.   <groupId>org.apache.maven.plugins</groupId>
  3.   <artifactId>maven-antrun-plugin</artifactId>
  4.   <version>3.0.0</version>
  5.   <executions>
  6.     <execution>
  7.       <id>deploy</id>
  8.       <phase>install</phase>
  9.       <configuration>
  10.         <target>
  11.           <scp file="${project.build.directory}/${project.build.finalName}.jar"
  12.                todir="user:password@server:/path/to/deployment/directory/"
  13.                trust="true"/>
  14.           <sshexec host="server"
  15.                    username="user"
  16.                    password="password"
  17.                    command="systemctl restart my-service"
  18.                    trust="true"/>
  19.         </target>
  20.       </configuration>
  21.       <goals>
  22.         <goal>run</goal>
  23.       </goals>
  24.     </execution>
  25.   </executions>
  26.   <dependencies>
  27.     <dependency>
  28.       <groupId>org.apache.ant</groupId>
  29.       <artifactId>ant-jsch</artifactId>
  30.       <version>1.10.9</version>
  31.     </dependency>
  32.   </dependencies>
  33. </plugin>
复制代码

执行一键部署:
  1. mvn install
复制代码

使用Docker实现一键部署

结合Docker,可以实现更标准化的一键部署流程。

创建Dockerfile:
  1. FROM openjdk:11-jre
  2. COPY target/*.jar /app.jar
  3. EXPOSE 8080
  4. ENTRYPOINT ["java", "-jar", "/app.jar"]
复制代码

在pom.xml中添加dockerfile-maven-plugin:
  1. <plugin>
  2.   <groupId>com.spotify</groupId>
  3.   <artifactId>dockerfile-maven-plugin</artifactId>
  4.   <version>1.4.13</version>
  5.   <executions>
  6.     <execution>
  7.       <id>default</id>
  8.       <goals>
  9.         <goal>build</goal>
  10.         <goal>push</goal>
  11.       </goals>
  12.     </execution>
  13.   </executions>
  14.   <configuration>
  15.     <repository>my-docker-repo/my-app</repository>
  16.     <tag>${project.version}</tag>
  17.     <buildArgs>
  18.       <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
  19.     </buildArgs>
  20.   </configuration>
  21. </plugin>
复制代码

执行一键部署:
  1. mvn package dockerfile:build dockerfile:push
复制代码

常见问题及解决方案

问题1:打包时出现”NoClassDefFoundError”

原因:运行时找不到依赖类,可能是因为依赖没有被正确打包到JAR文件中。

解决方案:

1. 使用maven-assembly-plugin或maven-shade-plugin创建包含所有依赖的fat JAR:
  1. <plugin>
  2.   <groupId>org.apache.maven.plugins</groupId>
  3.   <artifactId>maven-shade-plugin</artifactId>
  4.   <version>3.2.4</version>
  5.   <executions>
  6.     <execution>
  7.       <phase>package</phase>
  8.       <goals>
  9.         <goal>shade</goal>
  10.       </goals>
  11.     </execution>
  12.   </executions>
  13. </plugin>
复制代码

1. 或者,创建一个包含所有依赖的目录,并在Manifest文件中设置Class-Path:
  1. <plugin>
  2.   <groupId>org.apache.maven.plugins</groupId>
  3.   <artifactId>maven-jar-plugin</artifactId>
  4.   <version>3.2.0</version>
  5.   <configuration>
  6.     <archive>
  7.       <manifest>
  8.         <addClasspath>true</addClasspath>
  9.         <classpathPrefix>lib/</classpathPrefix>
  10.         <mainClass>com.example.MainClass</mainClass>
  11.       </manifest>
  12.     </archive>
  13.   </configuration>
  14. </plugin>
  15. <plugin>
  16.   <groupId>org.apache.maven.plugins</groupId>
  17.   <artifactId>maven-dependency-plugin</artifactId>
  18.   <version>3.1.2</version>
  19.   <executions>
  20.     <execution>
  21.       <id>copy-dependencies</id>
  22.       <phase>package</phase>
  23.       <goals>
  24.         <goal>copy-dependencies</goal>
  25.       </goals>
  26.       <configuration>
  27.         <outputDirectory>${project.build.directory}/lib</outputDirectory>
  28.       </configuration>
  29.     </execution>
  30.   </executions>
  31. </plugin>
复制代码

问题2:依赖冲突导致运行时错误

原因:不同的依赖引入了同一库的不同版本。

解决方案:

1. 使用mvn dependency:tree查看依赖树,识别冲突:
  1. mvn dependency:tree
复制代码

1. 使用dependencyManagement统一管理依赖版本:
  1. <dependencyManagement>
  2.   <dependencies>
  3.     <dependency>
  4.       <groupId>com.example</groupId>
  5.       <artifactId>conflicting-library</artifactId>
  6.       <version>1.0.0</version>
  7.     </dependency>
  8.   </dependencies>
  9. </dependencyManagement>
复制代码

1. 排除冲突的依赖:
  1. <dependency>
  2.   <groupId>com.example</groupId>
  3.   <artifactId>some-library</artifactId>
  4.   <version>1.0.0</version>
  5.   <exclusions>
  6.     <exclusion>
  7.       <groupId>com.example</groupId>
  8.       <artifactId>conflicting-library</artifactId>
  9.     </exclusion>
  10.   </exclusions>
  11. </dependency>
复制代码

1. 使用maven-shade-plugin重定位包:
  1. <plugin>
  2.   <groupId>org.apache.maven.plugins</groupId>
  3.   <artifactId>maven-shade-plugin</artifactId>
  4.   <version>3.2.4</version>
  5.   <executions>
  6.     <execution>
  7.       <phase>package</phase>
  8.       <goals>
  9.         <goal>shade</goal>
  10.       </goals>
  11.       <configuration>
  12.         <relocations>
  13.           <relocation>
  14.             <pattern>com.example.conflicting</pattern>
  15.             <shadedPattern>com.example.shaded</shadedPattern>
  16.           </relocation>
  17.         </relocations>
  18.       </configuration>
  19.     </execution>
  20.   </executions>
  21. </plugin>
复制代码

问题3:打包时出现”签名文件错误”

原因:依赖的JAR文件有签名,而在打包过程中这些签名文件被重复包含或修改。

解决方案:

在maven-shade-plugin中排除签名文件:
  1. <plugin>
  2.   <groupId>org.apache.maven.plugins</groupId>
  3.   <artifactId>maven-shade-plugin</artifactId>
  4.   <version>3.2.4</version>
  5.   <executions>
  6.     <execution>
  7.       <phase>package</phase>
  8.       <goals>
  9.         <goal>shade</goal>
  10.       </goals>
  11.       <configuration>
  12.         <filters>
  13.           <filter>
  14.             <artifact>*:*</artifact>
  15.             <excludes>
  16.               <exclude>META-INF/*.SF</exclude>
  17.               <exclude>META-INF/*.DSA</exclude>
  18.               <exclude>META-INF/*.RSA</exclude>
  19.             </excludes>
  20.           </filter>
  21.         </filters>
  22.       </configuration>
  23.     </execution>
  24.   </executions>
  25. </plugin>
复制代码

问题4:打包后资源文件丢失

原因:资源文件没有被正确包含在JAR文件中。

解决方案:

1. 确保资源文件位于src/main/resources目录下。
2. 在pom.xml中配置资源目录:

确保资源文件位于src/main/resources目录下。

在pom.xml中配置资源目录:
  1. <build>
  2.   <resources>
  3.     <resource>
  4.       <directory>src/main/resources</directory>
  5.       <filtering>true</filtering>
  6.     </resource>
  7.   </resources>
  8. </build>
复制代码

1. 如果需要包含额外的资源文件,可以添加额外的资源目录:
  1. <build>
  2.   <resources>
  3.     <resource>
  4.       <directory>src/main/resources</directory>
  5.     </resource>
  6.     <resource>
  7.       <directory>src/main/config</directory>
  8.     </resource>
  9.   </resources>
  10. </build>
复制代码

问题5:打包后Spring Boot应用无法启动

原因:Spring Boot应用需要特殊的打包方式。

解决方案:

使用spring-boot-maven-plugin:
  1. <plugin>
  2.   <groupId>org.springframework.boot</groupId>
  3.   <artifactId>spring-boot-maven-plugin</artifactId>
  4.   <version>2.5.5</version>
  5.   <executions>
  6.     <execution>
  7.       <goals>
  8.         <goal>repackage</goal>
  9.       </goals>
  10.     </execution>
  11.   </executions>
  12. </plugin>
复制代码

总结

本文详细介绍了Maven项目打包为JAR的各种方法,从基本的maven-jar-plugin到更高级的maven-assembly-plugin和maven-shade-plugin。我们讨论了如何处理依赖问题、解决依赖冲突、创建可执行JAR包,以及实现一键部署的多种方法。

通过合理选择和配置Maven插件,你可以轻松解决打包过程中的各种问题,实现项目的快速构建和部署。无论是简单的单模块项目还是复杂的多模块项目,Maven都提供了灵活而强大的打包解决方案。

希望本文能帮助你更好地理解和掌握Maven打包技术,提高项目构建和部署的效率。如果你有任何问题或建议,欢迎在评论区留言讨论。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

频道订阅

频道订阅

加入社群

加入社群

联系我们|TG频道|RSS

Powered by Pixtech

© 2025 Pixtech Team.