温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何使用Maven生成能直接运行的jar包

发布时间:2021-11-12 10:33:48 来源:亿速云 阅读:542 作者:小新 栏目:云计算
# 如何使用Maven生成能直接运行的jar包 ## 目录 1. [Maven基础概念](#maven基础概念) 2. [创建可执行Jar包的三种方式](#创建可执行jar包的三种方式) 3. [使用maven-jar-plugin基础配置](#使用maven-jar-plugin基础配置) 4. [使用maven-assembly-plugin详解](#使用maven-assembly-plugin详解) 5. [使用maven-shade-plugin高级方案](#使用maven-shade-plugin高级方案) 6. [依赖管理与类加载机制](#依赖管理与类加载机制) 7. [常见问题与解决方案](#常见问题与解决方案) 8. [生产环境最佳实践](#生产环境最佳实践) 9. [性能优化建议](#性能优化建议) 10. [扩展知识:Spring Boot的特殊处理](#扩展知识spring-boot的特殊处理) --- ## Maven基础概念 ### 1.1 Maven项目结构 标准的Maven项目遵循约定优于配置的原则: 

my-app/ ├── pom.xml ├── src/ │ ├── main/ │ │ ├── java/ │ │ │ └── com/ │ │ │ └── example/ │ │ │ └── App.java │ │ └── resources/ │ └── test/ │ ├── java/ │ └── resources/

 ### 1.2 POM文件核心元素 ```xml <project> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>my-app</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <!-- 插件配置区域 --> </plugins> </build> </project> 

1.3 Maven生命周期

  • validatecompiletestpackageverifyinstalldeploy
  • 生成可执行JAR主要在package阶段完成

创建可执行Jar包的三种方式

2.1 基础方案对比

插件名称 产出类型 依赖处理方式 适用场景
maven-jar-plugin 普通JAR 外部lib目录 简单应用
maven-assembly-plugin 胖JAR 全部打包进JAR 需要独立分发
maven-shade-plugin 胖JAR(重命名) 合并+重命名 解决依赖冲突

2.2 Main-Class要求

public class App { public static void main(String[] args) { System.out.println("Hello World!"); } } 

使用maven-jar-plugin基础配置

3.1 最小配置示例

<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.2.2</version> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <mainClass>com.example.App</mainClass> </manifest> </archive> </configuration> </plugin> </plugins> </build> 

3.2 执行流程

  1. 编译代码
  2. 生成MANIFEST.MF文件
  3. 创建包含以下结构的输出:
     target/ ├── my-app-1.0.jar └── lib/ ├── dependency1.jar └── dependency2.jar 

3.3 运行命令

java -jar target/my-app-1.0.jar 

使用maven-assembly-plugin详解

4.1 完整配置方案

<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>3.3.0</version> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <mainClass>com.example.App</mainClass> </manifest> </archive> </configuration> <executions> <execution> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> 

4.2 自定义Assembly描述符

<descriptors> <descriptor>src/main/assembly/custom.xml</descriptor> </descriptors> 

示例custom.xml:

<assembly> <id>bundle</id> <formats> <format>jar</format> </formats> <includeBaseDirectory>false</includeBaseDirectory> <dependencySets> <dependencySet> <outputDirectory>/</outputDirectory> <useProjectArtifact>true</useProjectArtifact> <unpack>true</unpack> <scope>runtime</scope> </dependencySet> </dependencySets> <fileSets> <fileSet> <directory>src/main/resources</directory> <outputDirectory>/</outputDirectory> </fileSet> </fileSets> </assembly> 

使用maven-shade-plugin高级方案

5.1 基础配置

<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.2.4</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>com.example.App</mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin> 

5.2 高级特性

解决依赖冲突

<configuration> <relocations> <relocation> <pattern>com.google.guava</pattern> <shadedPattern>com.shaded.guava</shadedPattern> </relocation> </relocations> </configuration> 

资源过滤

<transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>META-INF/spring.handlers</resource> </transformer> </transformers> 

依赖管理与类加载机制

6.1 类加载顺序

  1. Bootstrap ClassLoader
  2. Extension ClassLoader
  3. System ClassLoader
  4. 自定义ClassLoader

6.2 胖JAR的类加载特点

  • URLClassLoader处理嵌套JAR
  • 资源加载顺序依赖JAR文件中的物理顺序

6.3 依赖范围影响

<dependency> <scope>provided</scope> <!-- 不会打包进可执行JAR --> </dependency> 

常见问题与解决方案

7.1 典型问题清单

  1. NoClassDefFoundError

    • 检查依赖是否正确打包
    • 使用mvn dependency:tree分析依赖
  2. No main manifest attribute

    • 确认<mainClass>配置正确
    • 检查MANIFEST.MF生成位置
  3. 版本冲突

    • 使用mvn dependency:analyze
    • 排除特定依赖:
    <exclusions> <exclusion> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> </exclusion> </exclusions> 

生产环境最佳实践

8.1 版本管理策略

<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> <maven-shade-plugin.version>3.2.4</maven-shade-plugin.version> </properties> 

8.2 分层打包策略

<configuration> <filters> <filter> <artifact>*:*</artifact> <excludes> <exclude>META-INF/*.SF</exclude> <exclude>META-INF/*.DSA</exclude> </excludes> </filter> </filters> </configuration> 

性能优化建议

9.1 构建加速技巧

  1. 并行构建:

    mvn -T 1C package 
  2. 跳过测试:

    mvn package -DskipTests 

9.2 JAR优化方案

  • 使用ProGuard代码混淆
  • 压缩资源文件
  • 分模块打包

扩展知识:Spring Boot的特殊处理

10.1 spring-boot-maven-plugin

<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.example.DemoApplication</mainClass> <executable>true</executable> </configuration> </plugin> 

10.2 分层JAR结构

BOOT-INF/ ├── classes/ ├── lib/ META-INF/ org/ 

10.3 自定义Loader

public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } 

总结

本文详细介绍了三种生成可执行JAR的方案,建议根据项目需求选择: - 简单应用:maven-jar-plugin - 独立分发:maven-assembly-plugin - 企业级应用:maven-shade-plugin - Spring Boot项目:spring-boot-maven-plugin “`

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI