springboot打包与依赖包分离
前言:
springboot项目部署时,需要本地打包成一个jar放到服务器进行部署(使用jenkins自动打包部署同理),部署包里包含了其它所有依赖包,整个包会比较大,小则几M,大则几十上百。
正文:
1、pom文件plugins标签中添加以下内容
<!-- 去除配置文件,启动时默认指定外置config目录下的配置文件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <excludes> <exclude>*.properties</exclude> <exclude>*.xml</exclude> <exclude>*.yml</exclude> </excludes> </configuration> </plugin> <!-- 指定启动类,打包去除依赖包 --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.bangdao.parking.ordercenter.Application</mainClass> <layout>ZIP</layout> <includes> <include> <groupId>nothing</groupId> <artifactId>nothing</artifactId> </include> </includes> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> <!-- 指定jdk版本 --> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </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}/lib</outputDirectory> <overWriteReleases>false</overWriteReleases> <overWriteSnapshots>false</overWriteSnapshots> <overWriteIfNewer>true</overWriteIfNewer> </configuration> </execution> </executions> </plugin>
2、本地打包
3、在服务器上部署文件夹内新建lib目录,并将本地locallib(见上图)copy到新建的lib目录【注:只有在第一次部署时才需要复制】
4、指定依赖包目录,执行启动命令
nohup java -Dloader.path='lib/' -jar applets-api.jar &
从此可以开心的部署了,网络传输慢也可以轻松搞定。
未来的事,谁也说不准,活在当下!