maven之SpringBoot如何打包呢?
下文笔者讲述maven项目打包的方法及示例分享,如下所示
配置项目启动类
将当前项目java目录下除了java文件打进jar包呢
maven打包-我们需借助相应build插件,即可实现maven打包,如下所示
maven-compiler-plugin:负责编译源码 spring-boot-maven-plugin:负责编译springbootjar maven-jar-plugin:负责将项目依赖的jar写到MANIFEST.MF文件 maven-dependency-plugin:负责导出项目依赖的jar maven-resources-plugin:负责导出项目配置文件例:
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>
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>
配置项目启动类
关联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>
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>
拷贝项目依赖的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>
拷贝项目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>
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>
将当前项目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>
注意事项: **:表示多级目录 *:表示一级目录
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。