您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
# 如何使用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>
validate
→ compile
→ test
→ package
→ verify
→ install
→ deploy
package
阶段完成插件名称 | 产出类型 | 依赖处理方式 | 适用场景 |
---|---|---|---|
maven-jar-plugin | 普通JAR | 外部lib目录 | 简单应用 |
maven-assembly-plugin | 胖JAR | 全部打包进JAR | 需要独立分发 |
maven-shade-plugin | 胖JAR(重命名) | 合并+重命名 | 解决依赖冲突 |
public class App {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
<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>
target/
├── my-app-1.0.jar
└── lib/
├── dependency1.jar
└── dependency2.jar
java -jar target/my-app-1.0.jar
<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>
<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>
<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>
<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>
<dependency>
<scope>provided</scope> <!-- 不会打包进可执行JAR -->
</dependency>
NoClassDefFoundError
mvn dependency:tree
分析依赖No main manifest attribute
<mainClass>
配置正确版本冲突
mvn dependency:analyze
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
<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>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
并行构建:
mvn -T 1C package
跳过测试:
mvn package -DskipTests
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.example.DemoApplication</mainClass>
<executable>true</executable>
</configuration>
</plugin>
BOOT-INF/
├── classes/
├── lib/
META-INF/
org/
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 “`
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。