一、把依赖打进jar包
1.pom修改
使用 maven-assembly-plugin
插件,指定在 package
阶段(运行 mvn package
),会执行 maven-assembly-plugin:single
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>package-test</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-csv</artifactId>
<version>1.9.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.example.Main</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
2.结果
会在 target
目录下生成文件名包含 jar-with-dependencies
的 jar
文件
解压生成的jar文件中:包含依赖解压出class文件
zxm@zxm-pc:~/IdeaProjects/package-test$ jar ftv target/package-test-1.0-SNAPSHOT-jar-with-dependencies.jar
0 Sat Mar 02 13:01:46 CST 2024 META-INF/
153 Sat Mar 02 13:01:44 CST 2024 META-INF/MANIFEST.MF
0 Sat Mar 02 13:01:44 CST 2024 com/
0 Sat Mar 02 13:01:44 CST 2024 com/example/
0 Sat Mar 02 13:01:44 CST 2024 resources.txt
261 Sat Mar 02 13:01:44 CST 2024 com/example/Main.class
0 Wed Jan 22 15:10:16 CST 2020 org/
0 Wed Jan 22 15:10:16 CST 2020 org/apache/
0 Wed Jan 22 15:10:16 CST 2020 org/apache/commons/
0 Wed Jan 22 15:10:16 CST 2020 org/apache/commons/csv/
0 Wed Jan 22 15:10:16 CST 2020 META-INF/maven/
0 Wed Jan 22 15:10:16 CST 2020 META-INF/maven/org.apache.commons/
0 Wed Jan 22 15:10:16 CST 2020 META-INF/maven/org.apache.commons/commons-csv/
11358 Wed Jan 22 15:10:16 CST 2020 META-INF/LICENSE.txt
173 Wed Jan 22 15:10:16 CST 2020 META-INF/NOTICE.txt
870 Wed Jan 22 15:10:16 CST 2020 org/apache/commons/csv/CSVFormat$1.class
9817 Wed Jan 22 15:10:16 CST 2020 org/apache/commons/csv/CSVFormat$Builder.class
2338 Wed Jan 22 15:10:16 CST 2020 org/apache/commons/csv/CSVFormat$Predefined.class
29587 Wed Jan 22 15:10:16 CST 2020 org/apache/commons/csv/CSVFormat.class
916 Wed Jan 22 15:10:16 CST 2020 org/apache/commons/csv/CSVParser$1.class
2128 Wed Jan 22 15:10:16 CST 2020 org/apache/commons/csv/CSVParser$CSVRecordIterator.class
852 Wed Jan 22 15:10:16 CST 2020 org/apache/commons/csv/CSVParser$Headers.class
11519 Wed Jan 22 15:10:16 CST 2020 org/apache/commons/csv/CSVParser.class
5228 Wed Jan 22 15:10:16 CST 2020 org/apache/commons/csv/CSVPrinter.class
6926 Wed Jan 22 15:10:16 CST 2020 org/apache/commons/csv/CSVRecord.class
1224 Wed Jan 22 15:10:16 CST 2020 org/apache/commons/csv/Constants.class
2699 Wed Jan 22 15:10:16 CST 2020 org/apache/commons/csv/ExtendedBufferedReader.class
1665 Wed Jan 22 15:10:16 CST 2020 org/apache/commons/csv/IOUtils.class
7483 Wed Jan 22 15:10:16 CST 2020 org/apache/commons/csv/Lexer.class
1177 Wed Jan 22 15:10:16 CST 2020 org/apache/commons/csv/QuoteMode.class
1244 Wed Jan 22 15:10:16 CST 2020 org/apache/commons/csv/Token$Type.class
1074 Wed Jan 22 15:10:16 CST 2020 org/apache/commons/csv/Token.class
20575 Wed Jan 22 15:10:16 CST 2020 META-INF/maven/org.apache.commons/commons-csv/pom.xml
64 Wed Jan 22 15:10:16 CST 2020 META-INF/maven/org.apache.commons/commons-csv/pom.properties
MANIFEST.MF的内容
zxm@zxm-pc:~/IdeaProjects/package-test$ ls
pom.xml src target
zxm@zxm-pc:~/IdeaProjects/package-test$ jar vxf target/package-test-1.0-SNAPSHOT-jar-with-dependencies.jar META-INF/MANIFEST.MF
已解压: META-INF/MANIFEST.MF
zxm@zxm-pc:~/IdeaProjects/package-test$ ls
META-INF pom.xml src target
zxm@zxm-pc:~/IdeaProjects/package-test$ cat META-INF/MANIFEST.MF
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: zxm
Build-Jdk: 1.8.0_341
Main-Class: com.example.Main
二、依赖包和项目分离
1.pom修改
使用 maven-dependency-plugin
插件,指定在package阶段(运行 mvn package
),会执行 maven-dependency-plugin:copy-dependencies
maven-dependency-plugin:copy-dependencies
拷贝依赖包到指定目录
将jar包和lib文件夹放同一个目录,jar包就可以使用lib下的依赖包
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>package-test</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-csv</artifactId>
<version>1.9.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<!-- 主方法所在类名 -->
<mainClass>com.example.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<!-- 将依赖包放到lib文件夹中 -->
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>
${project.build.directory}/lib
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
2.结果
target目录下生成文件名包含的jar文件,和lib文件夹(依赖文件)
zxm@zxm-pc:~/IdeaProjects/package-test$ jar ftv target/package-test-1.0-SNAPSHOT.jar
0 Sat Mar 02 13:18:50 CST 2024 META-INF/
198 Sat Mar 02 13:18:48 CST 2024 META-INF/MANIFEST.MF
0 Sat Mar 02 13:18:48 CST 2024 com/
0 Sat Mar 02 13:18:48 CST 2024 com/example/
0 Sat Mar 02 13:18:48 CST 2024 resources.txt
261 Sat Mar 02 13:18:48 CST 2024 com/example/Main.class
0 Sat Mar 02 13:18:50 CST 2024 META-INF/maven/
0 Sat Mar 02 13:18:50 CST 2024 META-INF/maven/com.example/
0 Sat Mar 02 13:18:50 CST 2024 META-INF/maven/com.example/package-test/
2223 Sat Mar 02 13:18:28 CST 2024 META-INF/maven/com.example/package-test/pom.xml
115 Sat Mar 02 13:18:50 CST 2024 META-INF/maven/com.example/package-test/pom.properties
MANIFEST.MF的内容
zxm@zxm-pc:~/IdeaProjects/package-test$ ls
pom.xml src target
zxm@zxm-pc:~/IdeaProjects/package-test$ jar vxf target/package-test-1.0-SNAPSHOT.jar META-INF/MANIFEST.MF
已解压: META-INF/MANIFEST.MF
zxm@zxm-pc:~/IdeaProjects/package-test$ ls
META-INF pom.xml src target
zxm@zxm-pc:~/IdeaProjects/package-test$ cat META-INF/MANIFEST.MF
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: zxm
Class-Path: lib/commons-csv-1.9.0.jar
Created-By: Apache Maven 3.8.6
Build-Jdk: 1.8.0_341
Main-Class: com.example.Main