- maven-compiler-plugin:负责编译源码
- spring-boot-maven-plugin :负责编译springbootjar
- maven-jar-plugin:负责把项目依赖的jar写到MANIFEST.MF文件
- maven-dependency-plugin:负责导出项目依赖的jar
- maven-resources-plugin:负责导出项目配置文件
1:springboot全量打包项目为jar包的pom配置
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!--设置为true,以便把本地的system的jar也包括进来-->
<includeSystemScope>true</includeSystemScope>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
2:springboot打包项目为jar包和lib包分离的pom配置(其他的配置文件resources还是在jar包中)
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!--项目的启动类-->
<mainClass>org.jeecg.JeecgApplication</mainClass>
<!--layout 是为了 springboot jar简化后能够加载第三方jar包目录,如果没加入这句话,在待会儿启动时,会报错-->
<layout>ZIP</layout>
<!--解决windows命令行窗口中文乱码-->
<jvmArguments>-Dfile.encoding=UTF-8</jvmArguments>
<!--不包含任何jar包-->
<includes>
<include>
<groupId>nothing</groupId>
<artifactId>nothing</artifactId>
</include>
</includes>
</configuration>
<!--idea打jar包,提示 jar包中没有主清单属性 - 解决办法
1.是 <goal>repackage</goal>起了作用,内容如下:-->
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
3:配置项目启动类,并且关联lib包下面的jar包,启动项目后才能关联找到
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<!-- 是否要把第三方jar加入到类构建路径 -->
<addClasspath>true</addClasspath>
<!-- 外部依赖jar包的最终位置 -->
<!-- MANIFEST.MF 中 Class-Path 各个依赖加入前缀 -->
<classpathPrefix>lib/</classpathPrefix>
<!--指定jar程序入口-->
<mainClass>org.jeecg.JeecgApplication</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
4:Maven打包jar与lib依赖包分离
addClasspath:表示需要加入到类构建路径
classpathPrefix:指定生成的Manifest文件中Class-Path依赖lib前面都加上该前缀路径,构建出lib/xx.jar
mainClass:表示项目的启动类。
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<!-- 是否要把第三方jar加入到类构建路径 -->
<addClasspath>true</addClasspath>
<!-- 外部依赖jar包的最终位置 -->
<!-- MANIFEST.MF 中 Class-Path 各个依赖加入前缀 -->
<classpathPrefix>lib/</classpathPrefix>
<!--指定jar程序入口-->
<mainClass>com.baidu.test.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<!--拷贝依赖到jar外面的lib目录-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<!-- 拷贝项目依赖包到lib/目录下 -->
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
5:拷贝项目依赖的jar包到target目录下lib中
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-lib</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>target/lib</outputDirectory>
<excludeTransitive>false</excludeTransitive>
<stripVersion>false</stripVersion>
<includeScope>runtime</includeScope>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
在和jar包同级的目录下新建一个config目录,放入application.yml文件
这里可能有小伙伴有疑问了,打包的jar里面不是应该有application.yml文件吗,这里为什么再放一份?
这是因为springboot读取配置有一个优先级,放在jar包外面config目录优先级最高,主要是便于从外部修改配置,而不是改jar包中的application.yml文件。优先级如下:
(1)当前目录的config目录下
(2)当前目录
(3)classpath的config目录下
(4)classpath的根目录
6:拷贝项目resource包下的内容到target目录下resource中
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<!-- 复制配置文件 -->
<execution>
<id>copy-resources</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>src/main/resources</directory>
<!--<includes>-->
<!--<include>*.properties</include>-->
<!--<include>*.yml</include>-->
<!--<include>*/*.properties</include>-->
<!--<include>*/*.properties</include>-->
<!--</includes>-->
</resource>
</resources>
<outputDirectory>${project.build.directory}/resource/</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
7:pom.xml文件完整的瘦身配置
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<!-- 剔除配置文件 -->
<excludes>
<exclude>*.properties</exclude>
<exclude>*.yml</exclude>
<exclude>*/*.properties</exclude>
<exclude>*/*.yml</exclude>
<exclude>*/*.ftl</exclude>
</excludes>
<archive>
<manifest>
<!-- 是否要把第三方jar加入到类构建路径 -->
<addClasspath>true</addClasspath>
<!-- 外部依赖jar包的最终位置 -->
<classpathPrefix>lib/</classpathPrefix>
<!--指定jar程序入口-->
<mainClass>org.jeecg.JeecgApplication</mainClass>
</manifest>
<manifestEntries>
<!--MANIFEST.MF 中 Class-Path 加入自定义路径,多个路径用空格隔开,
此处是加入resource资源访问路径-->
<!--此处resources文件夹的内容,需要maven-resources-plugin插件补充上-->
<Class-Path>./resources/</Class-Path>
<!--类似引入第三方包,加入 lib/example-1.0.jar lib/jna-1.0.jar 注意需要有空格,
如果不想这样加入第三方包的引用,就直接将第三方包打包到本地仓库,然后dependency依赖进来-->
<!--<Class-Path>./resources/ lib/example-1.0.jar lib/jna-1.0.jar</Class-Path>-->
</manifestEntries>
</archive>
<outputDirectory>${project.build.directory}</outputDirectory>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<!-- 拷贝项目依赖包到lib/目录下 -->
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<!-- 复制配置文件 -->
<execution>
<id>copy-resources</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>src/main/resources</directory>
<!--<includes>-->
<!--<include>*.properties</include>-->
<!--<include>*.yml</include>-->
<!--<include>*/*.properties</include>-->
<!--<include>*/*.properties</include>-->
<!--</includes>-->
</resource>
</resources>
<outputDirectory>${project.build.directory}/resources/</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
8:怎么将当前项目java目录下除了java文件打进jar包呢,在build和plugins之间先设置策略如下
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.yml</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
<finalName>poi</finalName>
<plugins>
注意:**表示多级目录,*表示一级目录