默认打包方式如下:
<build><finalName>${project.artifactId}</finalName><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><executions><execution><goals><goal>repackage</goal></goals></execution></executions></plugin></plugins></build>
新打包方式
1、添加资源引用
包含如下保和文件格式
<resources><resource><directory>src/main/java</directory><filtering>true</filtering></resource><resource><directory>src/main/resources</directory><includes><include>**.*</include><include>**/*.properties</include><include>**/*.xml</include><include>**/*.*</include></includes><filtering>true</filtering></resource></resources>
2、jar打包插件
一定需要添加启动类<mainClass>com.xxx.xxx.xxx</mainClass>自己修改
<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-jar-plugin</artifactId><version>3.1.0</version><configuration><!-- 包含文件配置,现在只打包 com 文件夹 --><includes><include>**/com/**</include></includes><archive><manifest><!-- 配置加入依赖包 --><addClasspath>true</addClasspath><classpathPrefix>lib/</classpathPrefix><useUniqueVersions>false</useUniqueVersions><!-- Spring Boot 启动类(自行修改) --><mainClass>com.xxx.xxx.xxx</mainClass></manifest><manifestEntries><!-- 外部资源路径加入 manifest.mf 的 Class-Path --><Class-Path>resources/</Class-Path></manifestEntries></archive><!-- jar 输出目录 --><outputDirectory>${project.build.directory}/build/</outputDirectory></configuration></plugin>
3、依赖库打包
把依赖文件打包到lib目录
<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-dependency-plugin</artifactId><!-- 复制依赖 --><executions><execution><id>copy-dependencies</id><phase>package</phase><goals><goal>copy-dependencies</goal></goals><configuration><!-- 依赖包 输出目录 --><outputDirectory>${project.build.directory}/build/lib</outputDirectory></configuration></execution></executions></plugin>
4、资源打包
导报到resources目录下面
<plugin><groupId>org.apache.maven.plugins</groupId><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></resource></resources><!-- 资源文件 输出目录 --><outputDirectory>${project.build.directory}/build/resources</outputDirectory></configuration></execution></executions></plugin>
最终打包格式
<build><!-- 为jar包取名 --><finalName>${project.artifactId}</finalName><resources><resource><directory>src/main/java</directory><filtering>true</filtering></resource><resource><directory>src/main/resources</directory><includes><include>**.*</include><include>**/*.properties</include><include>**/*.xml</include><include>**/*.*</include></includes><filtering>true</filtering></resource></resources><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-jar-plugin</artifactId><version>3.1.0</version><configuration><!-- 包含文件配置,现在只打包 com 文件夹 --><includes><include>**/com/**</include></includes><archive><manifest><!-- 配置加入依赖包 --><addClasspath>true</addClasspath><classpathPrefix>lib/</classpathPrefix><useUniqueVersions>false</useUniqueVersions><!-- Spring Boot 启动类(自行修改) --><mainClass>com.xx.xxx.xxx</mainClass></manifest><manifestEntries><!-- 外部资源路径加入 manifest.mf 的 Class-Path --><Class-Path>resources/</Class-Path></manifestEntries></archive><!-- jar 输出目录 --><outputDirectory>${project.build.directory}/build/</outputDirectory></configuration></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-dependency-plugin</artifactId><!-- 复制依赖 --><executions><execution><id>copy-dependencies</id><phase>package</phase><goals><goal>copy-dependencies</goal></goals><configuration><!-- 依赖包 输出目录 --><outputDirectory>${project.build.directory}/build/lib</outputDirectory></configuration></execution></executions></plugin><plugin><groupId>org.apache.maven.plugins</groupId><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></resource></resources><!-- 资源文件 输出目录 --><outputDirectory>${project.build.directory}/build/resources</outputDirectory></configuration></execution></executions></plugin></plugins></build>
然后再命令窗口进行打包操作
mvn clean package -Dmaven.test.skip=true
打包目录如下,在target下面,build 目录下面包含两个文件夹和一个jar包,lib存储依赖的包,resources包含着配置资源,具体如下展示
执行:
java -Dfile.encoding=utf-8 -jar xxxxx.jar