前提:自己建了个maven项目,里面写了一些公共的类,想打包给其他项目用
1、在pom.xml中加入打包插件maven-jar-plugin
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<!-- 略过测试代码的编译,不推荐 -->
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>${maven.jar.plugin.version}</version>
<configuration>
<archive>
<addMavenDescriptor>false</addMavenDescriptor>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>/</classpathPrefix>
<mainClass>com.wanting.want.WantApplication</mainClass>
</manifest>
<manifestEntries>
<Class-Path>./</Class-Path>
</manifestEntries>
</archive>
<!-- 过滤掉不希望包含在jar中的文件 -->
<excludes>
<exclude>*.xml</exclude>
</excludes>
<includes>
<include>/</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
我的pom文件详情(目前只依赖了springboot):
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.wanting</groupId>
<artifactId>want</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>want</name>
<properties>
<java.version>1.8</java.version>
<maven.jar.plugin.version>3.1.0</maven.jar.plugin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<!-- 略过测试代码的编译,不推荐 -->
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>${maven.jar.plugin.version}</version>
<configuration>
<archive>
<addMavenDescriptor>false</addMavenDescriptor>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>/</classpathPrefix>
<mainClass>com.wanting.want.WantApplication</mainClass>
</manifest>
<manifestEntries>
<Class-Path>./</Class-Path>
</manifestEntries>
</archive>
<!-- 过滤掉不希望包含在jar中的文件 -->
<excludes>
<exclude>*.xml</exclude>
</excludes>
<includes>
<include>/</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</project>
2、在项目目录下执行
##会打包两个包,其中一个是源码包,并上传到maven库
mvn source:jar install
3、在目标项目中加依赖:
<dependency>
<groupId>com.wanting</groupId>
<artifactId>want</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
4、jar包目录结构