关于springboot:SpringBoot打包时分离ymlpropertiesxml配置文件以及依赖包

6次阅读

共计 3345 个字符,预计需要花费 9 分钟才能阅读完成。

1. 批改 pom 文件:

<output.dependence.file.path>lib/</output.dependence.file.path>
<output.resource.file.path>resource/</output.resource.file.path>
<build>
 <resources> <resource> <directory>src/main/resources</directory>
 <excludes> <exclude>*.properties</exclude>
 <exclude>*.yml</exclude>
 </excludes> </resource> </resources> <plugins> <!-- 打 JAR 包,不蕴含依赖文件;显式剔除配置文件 -->
 <plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-jar-plugin</artifactId>
 <configuration> <!-- 不打包资源文件,剔除配置文件 -->
 <excludes>
 <exclude>*.**</exclude>
 <exclude>*/*.xml</exclude>
 <exclude>*.properties</exclude>
 <exclude>*.yml</exclude>
 <exclude>*/*.properties</exclude>
 <exclude>*/*.yml</exclude>
 </excludes> <archive> <manifest> <!-- 将 classpath 增加到依赖形容 -->
 <addClasspath>true</addClasspath>
 <!--MANIFEST.MF 中 Class-Path 退出前缀 -->
 <!--lib 文件夹内容,须要 maven-dependency-plugin 插件补充 --> <classpathPrefix>lib/</classpathPrefix>
 <!--jar 包不蕴含惟一版本标识 -->
 <useUniqueVersions>false</useUniqueVersions>
 <!-- 指定入口类 -->
 <mainClass>com.htcs.modules.WebApplication</mainClass>
 </manifest> <manifestEntries> <!--MANIFEST.MF 中 Class-Path 退出资源文件目录,退出自定义门路,多个门路用空格隔开 -->
 <!-- 此处 resources 文件夹的内容,须要 maven-resources-plugin 插件补充上 --> <Class-Path>./resources/</Class-Path>
 </manifestEntries> </archive> <outputDirectory>${project.build.directory}</outputDirectory>
 </configuration> </plugin>
 <!-- 拷贝依赖 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}/lib/
                        </outputDirectory>
 </configuration> </execution> </executions> </plugin>
 <!-- 拷贝资源文件 copy-resources-->
 <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>
 <!--spring boot repackage,依赖 maven-jar-plugin 打包的 jar 包 从新打包成 spring boot 的 jar 包 -->
 <plugin>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-maven-plugin</artifactId>
 <configuration> <!-- 重写蕴含依赖,蕴含不存在的依赖,jar 里没有 pom 里的依赖 -->
 <includes>
 <include> <groupId>non-exists</groupId>
 <artifactId>non-exists</artifactId>
 </include> </includes> <layout>ZIP</layout>
 <!-- 应用内部配置文件,jar 包里没有资源文件 -->
 <addResources>true</addResources>
 <outputDirectory>${project.build.directory}</outputDirectory>
 </configuration> <executions> <execution> <goals> <goal>repackage</goal>
 </goals> <configuration> <!-- 配置 jar 包非凡标识 配置后,保留原文件,生成新文件 *-run.jar -->
 <!-- 配置 jar 包非凡标识 不配置,原文件命名为 *.jar.original,生成新文件 *.jar --> <!--<classifier>run</classifier>--> </configuration>
 </execution> </executions> </plugin> <plugin> <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-maven-plugin</artifactId>
 </plugin>
 <plugin> <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-surefire-plugin</artifactId>
 <version>2.22.2</version>
 <configuration> <skipTests>true</skipTests>
 </configuration> </plugin> </plugins>
</build>

2. 执行打包 maven 命令

mvn package

3. 拷贝相应文件到服务器

4. 在文件目录下执行运行脚本或命令

nohup java -jar htcsbusiness.jar > ./htcs.log 2>&1 &
正文完
 0