Springboot 打jar包分离lib,配置文件正确方式


声明:本文转载自https://my.oschina.net/xiaozhutefannao/blog/1624092,转载目的在于传递更多信息,仅供学习交流之用。如有侵权行为,请联系我,我会及时删除。

Springboot 打jar包分离lib,配置文件正确方式

前言

Springboot 打jar包分离lib,配置文件的方式,网上可以搜到的我都没试通。跟刘大神(大神没有博客,很可惜)讨论后,给出了这么一个解决方案,供大家参考。

部署环境

  • window 10
  • redhat 6.4
  • 其他版本没有尝试,应该也是可以的

POM.xml

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">     <modelVersion>4.0.0</modelVersion>      <groupId>com.elvish</groupId>     <artifactId>test</artifactId>     <version>0.0.1-SNAPSHOT</version>     <packaging>jar</packaging>      <name>test</name>     <description>test</description>      <parent>         <groupId>org.springframework.boot</groupId>         <artifactId>spring-boot-starter-parent</artifactId>         <version>1.5.10.RELEASE</version>         <relativePath />     </parent>      <properties>         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>         <java.version>1.8</java.version>     </properties>      <dependencies>         <dependency>             <groupId>org.springframework.boot</groupId>             <artifactId>spring-boot-starter-web</artifactId>         </dependency>          <dependency>             <groupId>org.springframework.boot</groupId>             <artifactId>spring-boot-starter-thymeleaf</artifactId>         </dependency>     </dependencies>      <build>         <plugins>             <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>target/lib</outputDirectory>                             <excludeTransitive>false</excludeTransitive>                             <stripVersion>false</stripVersion>                             <includeScope>runtime</includeScope>                         </configuration>                     </execution>                 </executions>             </plugin>              <plugin>                 <groupId>org.apache.maven.plugins</groupId>                 <artifactId>maven-jar-plugin</artifactId>                 <configuration>                     <excludes>                         <exclude>**/*.properties</exclude>                         <exclude>**/*.xml</exclude>                         <exclude>**/*.yml</exclude>                         <exclude>static/**</exclude>                         <exclude>templates/**</exclude>                     </excludes>                 </configuration>             </plugin>              <plugin>                 <groupId>org.springframework.boot</groupId>                 <artifactId>spring-boot-maven-plugin</artifactId>                 <configuration>                     <layout>ZIP</layout>                     <includes>                         <include>                             <groupId>non-exists</groupId>                             <artifactId>non-exists</artifactId>                         </include>                     </includes>                 </configuration>                 <executions>                     <execution>                         <goals>                             <goal>repackage</goal>                         </goals>                         <configuration>                             <classifier>classes</classifier>                             <attach>false</attach>                         </configuration>                     </execution>                 </executions>             </plugin>              <plugin>                 <groupId>org.apache.maven.plugins</groupId>                 <artifactId>maven-antrun-plugin</artifactId>                 <executions>                     <execution>                         <phase>package</phase>                         <goals>                             <goal>run</goal>                         </goals>                         <configuration>                             <target>                                 <property name="dist">target/distribution</property>                                 <property name="dist-tmp">target/distribution/tmp</property>                                 <property name="app-name">${project.artifactId}-${project.version}</property>                                 <mkdir dir="${dist-tmp}" />                                 <copy file="target/${app-name}.jar" tofile="${dist-tmp}/${app-name}.jar" />                                 <unzip src="${dist-tmp}/${app-name}.jar" dest="${dist-tmp}" />                                 <delete file="${dist-tmp}/${app-name}.jar" />                                  <zip destfile="${dist}/${app-name}-pages.jar">                                     <zipfileset dir="${dist-tmp}/META-INF" prefix="META-INF" />                                     <zipfileset dir="target/classes/static" prefix="static" />                                     <zipfileset dir="target/classes/templates" prefix="templates" />                                 </zip>                                  <move file="target/${app-name}-classes.jar" todir="${dist}" />                                 <move todir="${dist}/3rd-lib">                                     <fileset dir="target/lib" />                                 </move>                                  <delete dir="${dist-tmp}" />                                  <copy todir="${dist}">                                     <fileset dir="target/classes">                                         <include name="**/*.properties" />                                         <include name="**/*.xml" />                                         <include name="**/*.yml" />                                     </fileset>                                 </copy>                             </target>                         </configuration>                     </execution>                 </executions>             </plugin>         </plugins>     </build> </project>  

打完包后目录结构

  • 3rd-lib
  • META-INF
  • *.yml
  • *.xml
  • *.properties
  • test-0.0.1-SNAPSHOT-classes.jar
  • test-0.0.1-SNAPSHOT-pages.jar

运行jar

java -jar -Dloader.path=.,3rd-lib test-0.0.1-SNAPSHOT-classes.jar  

总结

完美运行,觉得不错的小伙伴,收藏一波吧。(想个性化抽离的,自行修改)

本文发表于2018年02月26日 18:39
(c)注:本文转载自https://my.oschina.net/xiaozhutefannao/blog/1624092,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如有侵权行为,请联系我们,我们会及时删除.

阅读 3156 讨论 0 喜欢 0

抢先体验

扫码体验
趣味小程序
文字表情生成器

闪念胶囊

万稳万当,不如一默。任何一句话,你不说出来便是那句话的主人,你说了出来,便是那句话的奴隶。

你要过得好哇,这样我才能恨你啊,你要是过得不好,我都不知道该恨你还是拥抱你啊。

直抵黄龙府,与诸君痛饮尔。

那时陪伴我的人啊,你们如今在何方。

不出意外的话,我们再也不会见了,祝你前程似锦。

快捷链接
网站地图
提交友链
Copyright © 2016 - 2021 Cion.
All Rights Reserved.
京ICP备2021004668号-1