You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot Gradle Plugin 2.x自定义启动器布局工厂使用及服务部署咨询

在Spring Boot Gradle Plugin 2.x中使用自定义启动器布局工厂(适配ActiveMQ Artemis+Commons Daemon)

我帮你梳理下具体的实现步骤,解决你遇到的版本兼容和自定义布局问题:

一、先搞定Spring Boot 2.x插件与ActiveMQ Artemis的版本兼容

首先升级Gradle插件到2.x稳定版,这样就不会出现自动降级ActiveMQ的问题了。在build.gradle里配置:

plugins {
    id 'org.springframework.boot' version '2.7.15' // 选一个2.x的稳定版本
    id 'io.spring.dependency-management' version '1.0.15.RELEASE'
    id 'java'
}

// 主动管控依赖版本,确保Artemis不会被降级
dependencyManagement {
    imports {
        mavenBom "org.springframework.boot:spring-boot-dependencies:2.7.15"
    }
    dependencies {
        // 指定你需要的Artemis版本,和Spring Boot 2.x兼容即可
        dependency "org.apache.activemq:artemis-jms-server:2.28.0"
        dependency "org.apache.activemq:artemis-core-client:2.28.0"
    }
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
    implementation 'org.springframework.boot:spring-boot-starter-artemis'
    // 引入Commons Daemon依赖
    implementation 'commons-daemon:commons-daemon:1.3.3'
    // 其他业务依赖...
}

二、实现自定义启动器布局工厂

Spring Boot 2.x中,布局工厂需要实现LayoutFactory接口,用来控制打包后的Jar结构,适配Commons Daemon的运行要求。

1. 编写自定义LayoutFactory类

package com.yourpackage;

import org.springframework.boot.loader.tools.Layout;
import org.springframework.boot.loader.tools.LayoutFactory;
import org.springframework.boot.loader.tools.Layouts;
import java.io.File;

public class DaemonLayoutFactory implements LayoutFactory {
    @Override
    public Layout getLayout(File source) {
        // 基于默认的Jar布局扩展,按需调整结构
        return new Layouts.Jar() {
            // 如果需要自定义启动类,在这里指定Commons Daemon适配的启动类
            @Override
            public String getLauncherClassName() {
                return "com.yourpackage.DaemonLauncher";
            }

            // 可选:重写getLibraryDestination等方法,调整依赖Jar的存放路径
            // @Override
            // public String getLibraryDestination(String libraryName, LibraryScope scope) {
            //     return "lib/" + libraryName;
            // }
        };
    }
}

2. 实现Commons Daemon适配的启动类

要让Spring Boot应用能被Commons Daemon管理,需要实现Daemon接口:

package com.yourpackage;

import org.apache.commons.daemon.Daemon;
import org.apache.commons.daemon.DaemonContext;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ConfigurableApplicationContext;

public class DaemonLauncher implements Daemon {
    private ConfigurableApplicationContext appContext;

    @Override
    public void init(DaemonContext daemonContext) throws Exception {
        // 初始化逻辑,比如处理启动参数
    }

    @Override
    public void start() throws Exception {
        // 启动Spring Boot应用
        appContext = SpringApplication.run(YourSpringBootApplication.class);
    }

    @Override
    public void stop() throws Exception {
        // 优雅停止应用
        if (appContext != null) {
            SpringApplication.exit(appContext);
        }
    }

    @Override
    public void destroy() {
        // 资源清理逻辑
    }
}

三、在Gradle中配置自定义布局工厂

在build.gradle的springBoot配置块里指定你的布局工厂:

springBoot {
    mainClass = 'com.yourpackage.YourSpringBootApplication'
    // 指定自定义布局工厂的全类名
    layoutFactory = 'com.yourpackage.DaemonLayoutFactory'
}

// 或者直接在bootJar任务中配置
bootJar {
    layoutFactory = new com.yourpackage.DaemonLayoutFactory()
}

四、打包与部署为Commons Daemon服务

执行./gradlew bootJar打包后,得到的Jar就可以用Commons Daemon的工具(Windows用Procrun,Linux用JSVC)配置成系统服务了。比如Linux下用JSVC的命令示例:

jsvc -cp your-app.jar:commons-daemon-1.3.3.jar \
     -user your-user \
     com.yourpackage.DaemonLauncher

这样就完成了整个配置,既解决了ActiveMQ Artemis的版本兼容问题,又实现了自定义启动器布局来适配Commons Daemon服务运行。

内容的提问来源于stack exchange,提问作者l_mcqueen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:16:13