springboot jar包依赖,配置文件分离集成windows服务

springboot jar包依赖,配置文件分离集成windows服务

背景

项目需要抽离依赖,配置文件,达到同个jar包可多部署。

项目环境

windows系统 ;jdk1.8,idea工具;WinSW.NET4.exe;tail工具(后面有说);

分离后目录

  • 项目test
    — config
    ————yml,xml,static文件
    — lib
    ————项目依赖jar包
    — 启动jar包
    — logs
    ————日志文件

在这里插入图片描述
ps: springboot配置文件加载顺序

  1. config/application.yml(项目根目录中config目录下)
  2. application.yml(项目根目录下)
  3. resources/config/application.yml
  4. resources/application.yml

pom文件修改

<build>
		<!--jar包名称-->
        <finalName>test</finalName>
        <plugins>
            <!--打包jar-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                	<!-- 去除资源配置文件 -->
                    <excludes>
                        <exclude>*.yml</exclude>
                    </excludes>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <!--MANIFEST.MF 中 Class-Path 加入前缀-->
                            <classpathPrefix>lib/</classpathPrefix>
                            <!--jar包不包含唯一版本标识-->
                            <useUniqueVersions>false</useUniqueVersions>
                            <!--指定入口类-->
                            <mainClass>com.test.Application</mainClass>
                        </manifest>
                    </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>

            <!--spring boot repackage,依赖 maven-jar-plugin 打包的jar包-->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <!--重写包含依赖,包含不存在的依赖,jar里没有pom里的依赖-->
                    <includes>
                        <include>
                            <groupId>null</groupId>
                            <artifactId>null</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>${spring.profiles.active}</classifier>-->
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

到这里我们的项目重新打包后的目录结构会变成下面这种,启动直接使用java -jar test.jar启动即可,遇到乱码问题可用java -Dfile.encoding=utf-8 -jar test.jar或先运行chcp 65001改变cmd的编码方式。

在这里插入图片描述

windows服务集成

下载地址:WinSW.NET4.exe
在这里插入图片描述
配置xml文件,修改xml文件与jar包同名

<configuration>
    <id>jar包名</id>
    <name>jar包名</name>
    <description>服务描述</description>
    <executable>java</executable>
    <arguments>-Dfile.encoding=utf-8 -jar D:\test\test.jar</arguments>
      <!-- 开机启动 -->
      <!--<startmode>Automatic</startmode>-->
      <!-- 日志配置 -->
    <logpath>D:\test\logs\service</logpath>
    <log mode="roll-by-time">
    <pattern>yyyyMMdd</pattern>
    </log>
</configuration>

修改WinSw.NET4.EXE和jar包同名,存放根目录节点
在这里插入图片描述
运行dos窗口安装服务必须以管理员身份运行
安装命令:test.exe install
卸载命令:test.exe uninstall
安装成功后
启动:net start test
停止:net stop test
在这里插入图片描述
安装成功后win + R 输入services.msc命令查看test服务
在这里插入图片描述

编写脚本启动

编写一些简单的启动脚本,方便部署操作。保存为**.bat**文件即可。tail查看文章底部参考链接

  1. 启动脚本
@echo off
echo "启动test"
net start test
tail -f ../logs/test.log
@pause
  1. 停止脚本
@echo off
echo "关闭test服务"
net stop test
echo "关闭成功"
@pause

最终程序目录:
在这里插入图片描述

启动脚本中tail程序可以实现然windows类似于linux系统查看日志
具体参考下:https://www.cnblogs.com/wangfeixiong/p/8948121.html

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值