【sprinb-boot】分离打包
使用 maven 命令 mvn package 打包 spring boot 项目时,将 lib 分离出来。
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>
<build>
...
<plugins>
<!-- lib分离打包步骤 : 1,copy-dependencies -> 2,repackage -->
<!-- lib分离打包1/2 copy-dependencies : 拷贝依赖文件 -->
<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}/final-package/lib/
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<!-- lib分离打包2/2 repackage : spring boot 打包设置,不打包lib -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<includes>
<!--不将依赖的jar打包到spring boot的jar/war中 -->
<include>
<groupId>null</groupId>
<artifactId>null</artifactId>
</include>
</includes>
<layout>ZIP</layout>
<!--设置spring boot的jar/war的存放路径 -->
<outputDirectory>${project.build.directory}/final-package</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
...
</project>
打包命令
mvn clean package
启动命令
java -Dloader.path=lib -jar xxx-springboot-app.jar
【sprinb-boot】配置文件分离打包_boot 配置文件分离打包 - CSDN 博客
使用 maven 命令 mvn package 打包 spring boot 项目时,将配置文件分离出来。
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>
<build>
...
<plugins>
<!-- 配置文件分离打包步骤 : 1,jar -> 2,copy-resources -> 3,repackage -->
<!-- 配置文件分离打包1/3 jar : 配置文件文件不打包到jar中 -->
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<excludes>
<exclude>application*.properties</exclude>
<exclude>messages*.properties</exclude>
<exclude>*.xml</exclude>
</excludes>
</configuration>
</plugin>
<!-- 配置文件分离打包2/3 copy-resources : 拷贝配置文件 -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
</configuration>
<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>application.properties</include>
<include>logback-spring.xml</include>
<include>messages*.properties</include>
</includes>
</resource>
</resources>
<outputDirectory>${project.build.directory}/final-package</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<!-- 配置文件分离打包3/3 repackage : spring boot 打包设置 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<layout>ZIP</layout>
<!--设置spring boot的jar/war的存放路径 -->
<outputDirectory>${project.build.directory}/final-package</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
...
</project>
打包命令
mvn clean package
启动命令
java -jar xxx-springboot-app.jar
【sprinb-boot】配置和 lib 分离打包_springboot 打包 war 分离 lib-CSDN 博客
使用 maven 命令 mvn package 打包 spring boot 项目时,将配置和 lib 分离出来。
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>
<build>
...
<plugins>
<!-- 配置和lib分离打包步骤 : 1,jar -> 2,copy-dependencies -> 3,copy-resources -> 4,repackage -->
<!-- 配置和lib分离打包1/4 jar : 配置不打包到jar中 -->
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<excludes>
<exclude>application*.properties</exclude>
<exclude>messages*.properties</exclude>
<exclude>*.xml</exclude>
</excludes>
</configuration>
</plugin>
<!-- 配置和lib分离打包2/4 copy-dependencies : 拷贝依赖文件 -->
<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}/final-package/lib/
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<!-- 配置和lib分离打包3/4 copy-resources : 拷贝配置文件 -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
</configuration>
<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>application.properties</include>
<include>logback-spring.xml</include>
<include>messages*.properties</include>
</includes>
</resource>
</resources>
<outputDirectory>${project.build.directory}/final-package</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<!-- 配置和lib分离打包4/4 repackage : spring boot 打包设置,不打包lib -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<includes>
<!--不将依赖的jar打包到spring boot的jar/war中 -->
<include>
<groupId>null</groupId>
<artifactId>null</artifactId>
</include>
</includes>
<layout>ZIP</layout>
<!--设置spring boot的jar/war的存放路径 -->
<outputDirectory>${project.build.directory}/final-package</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
...
</project>
打包命令
mvn clean package
启动命令
java -Dloader.path=lib -jar xxx-springboot-app.jar
【sprinb-boot】资源、配置、lib 分离打包_springboot 把 lib、config、static、templates 打包分离 - CSDN 博客
使用 maven 命令 mvn package 打包 spring boot 项目时,将资源、配置、lib 分离出来。
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>
<build>
...
<plugins>
<!-- 资源、配置、lib分离打包步骤 : 1,jar -> 2,copy-dependencies -> 3,copy-resources -> 4,repackage -->
<!-- 资源、配置、lib分离打包1/4 jar : 资源和配置不打包到jar中 -->
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<excludes>
<exclude>application*.properties</exclude>
<exclude>messages*.properties</exclude>
<exclude>*.xml</exclude>
<exclude>config/**</exclude>
<exclude>templates/**</exclude>
<exclude>static/**</exclude>
</excludes>
</configuration>
</plugin>
<!-- 资源、配置、lib分离打包2/4 copy-dependencies : 拷贝依赖文件 -->
<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}/final-package/lib/
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<!-- 资源、配置、lib分离打包3/4 copy-resources : 拷贝资源和配置文件 -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
</configuration>
<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>application.properties</include>
<include>logback-spring.xml</include>
<include>messages*.properties</include>
<include>templates/**</include>
<include>config/**</include>
</includes>
</resource>
</resources>
<outputDirectory>${project.build.directory}/final-package</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<!-- 资源、配置、lib分离打包3/4 repackage : spring boot 打包设置,不打包lib -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<includes>
<!--不将依赖的jar打包到spring boot的jar/war中 -->
<include>
<groupId>null</groupId>
<artifactId>null</artifactId>
</include>
</includes>
<layout>ZIP</layout>
<!--设置spring boot的jar/war的存放路径 -->
<outputDirectory>${project.build.directory}/final-package</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
...
</project>
打包命令
mvn clean package
启动命令
java -Dloader.path=config,static,templates,lib -jar xxx-springboot-app.jar
前言
- spring boot 2.0.0.RELEASE
操作
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
此处要特别提到 fork 参数。该参数的说明:
Flag to indicate if the run processes should be forked. Disabling forking will disable some features such as an agent, custom JVM arguments, devtools or specifying the working directory to use.
Default value is: true.
User property is: spring-boot.run.fork.
Since: 1.2.0
虽然不明觉厉,但是还真碰到了需要该参数的情况。
在一个项目中发现():mvn spring-boot:run 启动项目屡次失败。最后发现,将 pom.xml 中的 spring-boot-devtools 去掉就可以启动了。来回添加、去掉 spring-boot-devtools 还是很麻烦的。将 fork 这个参数设置成 false,就不用来回添加、去掉 spring-boot-devtools 了。
【springboot】模板路径、静态资源路径、WebRoot 的本地路径_springboot webroot-CSDN 博客
模板路径
- 分离系统模版,将模板文件夹从打包后的 jar 文件中分离。项目发布后,方便对模板文件的修改。
- 建议,将模板文件夹放在 “jar 所在文件夹” 下,起名为 “templates”。
- 修改 application.properties 实现模板路径分离。
- 下面是针对 thymeleaf 模板进行的设置。
custom.app.home=C:/Users/Administrator.SKY-20171114LED/Desktop/myproject
spring.thymeleaf.prefix=file:///${custom.app.home}/templates/
静态资源路径
- 分离静态资源,将静态资源从打包后的 jar 文件中分离。项目发布后,方便对静态资源修改。
- 建议,将模板文件夹放在 “jar 所在文件夹” 下,起名为 “static”。
- 修改 application.properties 实现模板路径分离。
custom.app.home=C:/Users/Administrator.SKY-20171114LED/Desktop/myproject
spring.mvc.static-path-pattern=/**
spring.resources.static-locations=file:///${custom.app.home}/static
WebRoot 的本地路径
- 项目发布成 jar 后(内嵌 tomcat),代码 “request.getSession ().getServletContext ().getRealPath (“/”)” 获得的本地路径是哪里呢?
- “jar 所在文件夹”/“public”。
- “jar 所在文件夹”/“static”。当上门的目录不存在时,本目录起效。
- 如果前面两个目录都不存在,在 window 上发现为,在系统的 temp 目录中创建一个临时目录作为 WebRoot 的本地起始目录。
- 关于 WebRoot 的本地路径的结论,是测试得出的。未查看相关源码进行分析。

浙公网安备 33010602011771号