从零构建可视化jar包部署平台JarManage

简介: 在java项目部署过程中,由于内外部各种因素,可能会遇到一些感觉操作不便捷的场景,例如jar包未随系统自动启动需要每次手动重启

项目背景

在java项目部署过程中,由于内外部各种因素,可能会遇到一些感觉操作不便捷的场景,例如

  • jar包未随系统自动启动需要每次手动重启
  • 系统vpn堡垒机多重防御更新繁琐
  • 系统无图形化界面命令行操作复杂
  • 等等......

在工作中之前也总结了windows的Jar包部署工具linux下的jar包自动化部署脚本,这次就想着否能将二者统一结合,本着简单/高效/功能专一的原则,做出一

个可视化jar包部署平台,JarManage应运而生

功能介绍

项目地址:https://gitee.com/code2roc/jar-manage

支持在线创建项目,上传Jar包,自动备份,配置启动参数,注册系统服务,查看启动日志等功能,具有以下优点

  • 基于servlet开发,依赖简洁,部署包10MB左右
  • 结合嵌入式tomcat一键部署,无外部容器依赖
  • 使用h2db存储数据,无外部数据库依赖
  • 适配windows/linux平台,满足多种环境
  • 具体项目经平台部署后自动注册系统服务,无需担心服务器重启

系统架构图如下

系统截图展示

技术分析

平台识别

首先通过系统os识别是windows平台还是linux平台

String os = System.getProperty("os.name").toLowerCase();
if (os.startsWith("win")) {
   
   
   platform = DepolyPlatform.Windows;
}
AI 代码解读

通过system-release文件识别部分基于CentOS开发的Linux系统

String command = "cat /etc/system-release";
String result = CMDUtil.executeLinuxCommand(command);
if (result.startsWith("Red Hat")) {
   
   
   platform = DepolyPlatform.LinuxRedHat;
} else if (result.startsWith("CentOS")) {
   
   
   platform = DepolyPlatform.LinuxCentOS;
} else if (result.startsWith("openEuler")) {
   
   
   platform = DepolyPlatform.LinuxOpenEuler;
}
AI 代码解读

通过issue文件识别部分基于Ubuntu/Debian开发的Linux系统

command = "cat /etc/issue";
result = CMDUtil.executeLinuxCommand(command);
if (!StringUtil.isEmpty(result)) {
   
   
  if (result.startsWith("Ubuntu")) {
   
   
     platform = DepolyPlatform.LinuxUbuntu;
} else if (result.startsWith("Debian")) {
   
   
      platform = DepolyPlatform.LinuxDebian;
   }
}
AI 代码解读

windows注册服务

通过sc query命令判断服务状态

    public String getStatus(String serviceName) {
   
   
        String status = DepolyStatus.UnInstall;
        try {
   
   
            String command = "sc query " + serviceName;
            String commandResultFilePath = CMDUtil.executeWindowCommandStoreFile(command);
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(commandResultFilePath)));
            String line = reader.readLine();
            while (line != null) {
   
   
                if (line.trim().startsWith("STATE")) {
   
   
                    if (line.trim().substring(line.trim().indexOf(":") + 1, line.trim().indexOf(":") + 4).trim().equals("1"))
                        status = DepolyStatus.Stopped;
                    else if (line.trim().substring(line.trim().indexOf(":") + 1, line.trim().indexOf(":") + 4).trim().equals("2"))
                        status = DepolyStatus.Startting;
                    else if (line.trim().substring(line.trim().indexOf(":") + 1, line.trim().indexOf(":") + 4).trim().equals("3"))
                        status = DepolyStatus.Stopping;
                    else if (line.trim().substring(line.trim().indexOf(":") + 1, line.trim().indexOf(":") + 4).trim().equals("4"))
                        status = DepolyStatus.Running;
                }
                line = reader.readLine();
            }
        } catch (IOException e) {
   
   
            LogUtil.error(e);
        }
        return status;
    }
AI 代码解读

通过winsw这个开源项目配置exe和xml文件将jar包注册为windows服务,项目地址:https://github.com/winsw/winsw/

linux注册服务

通过systemctl status命令判断服务状态

    public String getStatus(String serviceName) {
   
   
        String status = DepolyStatus.UnInstall;
        try {
   
   
            String command = "systemctl status " + serviceName;
            String commandResultFilePath = CMDUtil.executeLinuxCommandWithStore(command);
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(commandResultFilePath)));
            String line = reader.readLine();
            while (line != null) {
   
   
                if (line.trim().startsWith("Active")) {
   
   
                    if (line.trim().indexOf("inactive (dead)") > 0)
                        status = DepolyStatus.Stopped;
                    else if (line.trim().indexOf("active (running)") > 0)
                        status = DepolyStatus.Running;
                    else if (line.trim().indexOf("failed") > 0)
                        status = DepolyStatus.Stopped;
                }
                line = reader.readLine();
            }
        } catch (IOException e) {
   
   
            LogUtil.error(e);
        }
        return status;
    }
AI 代码解读

通过拷贝service文件到systemd/system目录下注册linux服务

yml配置文件识别

  • maven配置
        <dependency>
            <groupId>org.yaml</groupId>
            <artifactId>snakeyaml</artifactId>
            <version>1.26</version>
        </dependency>
AI 代码解读
  • 配置文件
jarmanage:
  port: 8555
  username: admin
  password: abcd@1234
  backupcount: 5
AI 代码解读
  • 工具类
    public static String getConfigValue(String configName){
   
   
        String configValue = "";
        try{
   
   
            Yaml yaml = new Yaml();
            InputStream resourceAsStream = new FileInputStream(new File("resources"+File.separator+"application.yml"));
            Map obj = yaml.load(resourceAsStream);
            Map<String,Object> param = (Map) obj.get("jarmanage");
            configValue = ConvertUtil.convert2String(param.get(configName));
        }catch (Exception e){
   
   
            LogUtil.error(e);
        }
        return configValue;
    }
AI 代码解读

h2database使用

  • maven引用
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>2.1.214</version>
        </dependency>
AI 代码解读
  • 工具类
    public static Connection getConnection() throws Exception {
   
   
        File file = new File("database");
        Connection conn = DriverManager.getConnection("jdbc:h2:file:" + file.getAbsolutePath() + File.separator + "manage", "root", "abcd@1234");
        return conn;
    }

    public static void executeSQL(String sql) {
   
   
        try {
   
   
            Connection conn = getConnection();
            Statement stmt = conn.createStatement();
            stmt.execute(sql);
            stmt.close();
            conn.close();
        } catch (Exception e) {
   
   
            LogUtil.error(e);
        }
    }
AI 代码解读

servelt内置tomcat打包

  • maven引用
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-core</artifactId>
            <version>9.0.35</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-el</artifactId>
            <version>9.0.35</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <version>9.0.35</version>
        </dependency>
AI 代码解读
  • 手动启动
            //启动tomcat服务
            // 1.创建一个内嵌的Tomcat
            Tomcat tomcat = new Tomcat();
            // 2.设置Tomcat端口
            tomcat.setPort(8555);
            // 3.设置工作目录,tomcat需要使用这个目录进行写一些东西
            final String baseDir = "workspace" + File.separator;
            tomcat.setBaseDir(baseDir);
            tomcat.getHost().setAutoDeploy(false);
            // 4. 设置webapp资源路径
            String webappDirLocation = "webapp" + File.separator;
            StandardContext ctx = (StandardContext) tomcat.addWebapp("/", new File(webappDirLocation).getAbsolutePath());
            // 5. 设置上下文路每径
            String contextPath = "";
            ctx.setPath(contextPath);
            ctx.addLifecycleListener(new Tomcat.FixContextListener());
            ctx.setName("jar-manage");
            tomcat.getHost().addChild(ctx);
            //6.启动
            tomcat.getConnector();
            tomcat.start();
            tomcat.getServer().await();
AI 代码解读
  • 打包包含引用类库,自定义配置xml,指定运行class
        <plugins>
            <plugin>
                <!-- 打包包含引用 -->
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptors>
                        <!-- 自定义配置 -->
                        <descriptor>package.xml</descriptor>
                    </descriptors>
                    <archive>
                        <manifest>
                            <!-- 运行类 -->
                            <mainClass>com.code2roc.jarmanage.Application</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
        </plugins>
AI 代码解读
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.1"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.1 https://maven.apache.org/xsd/assembly-2.1.1.xsd">
    <!-- TODO: a jarjar format would be better -->
    <id>depoly</id>
    <formats>
        <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <dependencySets>
        <dependencySet>
            <outputDirectory>/</outputDirectory>
            <useProjectArtifact>true</useProjectArtifact>
            <unpack>true</unpack>
            <scope>runtime</scope>
        </dependencySet>
    </dependencySets>
    <fileSets>
        <fileSet>
            <directory>src/main/webapp/</directory>
            <outputDirectory>/webapp</outputDirectory>
            <includes>
                <include>**/**</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>src/main/resources/</directory>
            <outputDirectory>/resources</outputDirectory>
            <includes>
                <include>**/**</include>
            </includes>
        </fileSet>
    </fileSets>
</assembly>
AI 代码解读
目录
打赏
0
0
0
0
146
分享
相关文章
如何实现上传jar直接部署成功,这篇文章直接带你上手springboot实现jar包热更新!
本文详细讲解了在Spring Boot应用中实现Jar包热更新的实践方法。通过自定义类加载器(`HotClassLoader`),动态加载和卸载指定目录下的Jar包,结合Spring Bean动态注册机制,使新加载的类能够被Spring容器管理。同时,提供了文件上传接口,方便用户手动触发Jar包更新。文章还强调了安全性、依赖管理和线程安全等注意事项,并给出了测试步骤和总结,帮助开发者高效实现热更新功能,减少服务中断和提升开发效率。
|
3月前
|
Maven系统级别依赖:解决部署时Jar包缺失问题
以上就是关于Maven系统级别依赖解决部署时Jar包缺失问题的解答,希望对你有所帮助。在软件开发中,遇到问题并解决问题是常态,希望你能够善用这些工具,解决你遇到的问题。
129 28
利用(K8S)配合Docker部署jar包
通过Docker打包并部署到Kubernetes(K8S)集群的过程。首先,通过SpringBoot生成jar包,接着在K8S环境中创建并编辑Dockerfile文件。随后构建Docker镜像,并将其推送到镜像仓库。最后,通过一系列kubectl命令(如get pods、get svc、logs等),展示了如何在K8S中管理应用,包括查看Pod状态、服务信息、Pod日志以及重启Pod等操作。
439 3
阿里云云效产品使用合集之如何下载流水线构建过程中生成的jar
云效作为一款全面覆盖研发全生命周期管理的云端效能平台,致力于帮助企业实现高效协同、敏捷研发和持续交付。本合集收集整理了用户在使用云效过程中遇到的常见问题,问题涉及项目创建与管理、需求规划与迭代、代码托管与版本控制、自动化测试、持续集成与发布等方面。
震惊!如何在linux下部署项目,部署/运行jar包 超详细保姆级教程!
如何在Linux系统下部署和运行Java项目jar包,包括传输文件到Linux、使用nohup命令运行jar包、查看端口状态、杀死进程和查看项目运行状态,以及如何解决“没有主清单属性”的错误。
1462 2
震惊!如何在linux下部署项目,部署/运行jar包 超详细保姆级教程!
|
12月前
|
[JarEditor]可直接修改jar包的IDEA插件
### 修改JAR包变得更简单:JarEditor插件简介 **背景:** 开发中常需修改JAR包中的class文件,传统方法耗时费力。JarEditor插件让你一键编辑JAR包内文件,无需解压。 **插件使用:** 1. **安装:** 在IDEA插件市场搜索JarEditor并安装。 2. **修改class:** 打开JAR文件中的class,直接编辑,保存后一键构建更新JAR。 3. **文件管理:** 右键菜单支持在JAR内新增/删除/重命名文件等操作。 4. **搜索:** 使用内置搜索功能快速定位JAR包内的字符串。
958 2
[JarEditor]可直接修改jar包的IDEA插件
构建Springboot项目、实现简单的输出功能、将项目打包成可以执行的JAR包(详细图解过程)
这篇文章详细介绍了构建SpringBoot项目的过程,包括新建工程、选择环境配置、添加依赖、项目结构说明,并演示了如何编写一个简单的Controller控制器实现输出功能,最后讲解了如何使用Maven将项目打包成可执行的JAR包,并提供了运行JAR包的命令和测试效果。
构建Springboot项目、实现简单的输出功能、将项目打包成可以执行的JAR包(详细图解过程)
Serverless 应用引擎操作报错合集之上传自定义JAR包,启动时报错,是什么导致的
Serverless 应用引擎(SAE)是阿里云提供的Serverless PaaS平台,支持Spring Cloud、Dubbo、HSF等主流微服务框架,简化应用的部署、运维和弹性伸缩。在使用SAE过程中,可能会遇到各种操作报错。以下是一些常见的报错情况及其可能的原因和解决方法。
PolarDB产品使用问题之部署到服务器上的Java应用(以jar包形式运行)无法连接,如何解决
PolarDB产品使用合集涵盖了从创建与管理、数据管理、性能优化与诊断、安全与合规到生态与集成、运维与支持等全方位的功能和服务,旨在帮助企业轻松构建高可用、高性能且易于管理的数据库环境,满足不同业务场景的需求。用户可以通过阿里云控制台、API、SDK等方式便捷地使用这些功能,实现数据库的高效运维与持续优化。

热门文章

最新文章

AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等

登录插画

登录以查看您的控制台资源

管理云资源
状态一览
快捷访问