• maven assembly打包生成Java应用启动脚本bat和sh


    1. maven插件介绍

    springboot应用通过maven插件appassembler-maven-plugi生成启动脚本bat和sh。根据官网介绍,这个插件主要用于生成启动 java应用程序的脚本,能将项目依赖jar能够打包目录中,并且它们加入启动脚本类路径中。

    主要命令
    appassembler:assemble 打包项目并且配置bin 启动脚本,可以理解为使用类似spring-boot-maven-plugin打包出来项目,可以通过java -jar 方式启动项目,但是不支持stop、satus、restart这些操作,比较原始。
    appassembler:create-repository 创建一个 appassembler 存储库,就是将工程打成jar
    appassembler:generate-daemons 生成基于 JSW 的守护进程包装器,大多数人都是使用这个。

    参考文档
    appassembler-maven-plugin 插件为 SpringBoot 项目生成启动脚本
    https://www.jb51.net/article/191442.htm

    2. 生成启动脚本

    2.1 创建springboot应用

    2.2 创建启动测试类

    MavenAssemblyDemoApplication

    package com.zrj.maven.assembly.demo;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    /**
     * MavenAssemblyDemoApplication
     *
     * @author zrj
     * @since 20221117
     */
    @RestController
    @SpringBootApplication
    public class MavenAssemblyDemoApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(MavenAssemblyDemoApplication.class, args);
        }
    
        /**
         * Hello World!
         * If you open a web browser to localhost:8080, you should see the following output:
         *
         * @see 1 https://docs.spring.io/spring-boot/docs/2.7.5/reference/htmlsingle/
         */
        @RequestMapping("/")
        String home() {
            long currentTime = System.currentTimeMillis();
            String helloTime = currentTime + ":Hello World!";
            System.out.println(helloTime);
            return helloTime;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    2.3 maven依赖配置

    pom.xml

    
    <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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0modelVersion>
        <parent>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-parentartifactId>
            <version>2.7.5version>
            <relativePath/> 
        parent>
    
        <groupId>com.zrjgroupId>
        <artifactId>maven-assembly-demoartifactId>
        <version>1.0.0-SNAPSHOTversion>
        <name>maven-assembly-demoname>
        <description>Demo project for Spring Bootdescription>
    
        <properties>
            <java.version>1.8java.version>
        properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
            dependency>
    
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-testartifactId>
                <scope>testscope>
            dependency>
        dependencies>
    
        
        <build>
            <plugins>
                <plugin>
                    <groupId>org.codehaus.mojogroupId>
                    <artifactId>appassembler-maven-pluginartifactId>
                    <version>2.1.0version>
    
                    
                    
                    <executions>
                        <execution>
                            <id>make-assemblyid>
                            <phase>packagephase>
                            <goals>
                                <goal>assemblegoal>
                            goals>
                        execution>
                    executions>
    
                    <configuration>
                        
                        <repositoryLayout>flatrepositoryLayout>
                        
                        <configurationSourceDirectory>src/main/resourcesconfigurationSourceDirectory>
                        <includeConfigurationDirectoryInClasspath>trueincludeConfigurationDirectoryInClasspath>
                        
                        <copyConfigurationDirectory>truecopyConfigurationDirectory>
                        
                        <assembleDirectory>${project.build.directory}/clientassembleDirectory>
                        
                        <configurationDirectory>confconfigurationDirectory>
                        
                        <repositoryName>librepositoryName>
                        
                        <binFolder>binbinFolder>
    
                        
                        <platforms>
                            <platform>windowsplatform>
                            <platform>unixplatform>
                        platforms>
    
                        
                        <binFileExtensions>
                            <unix>.shunix>
                            <windows>.batwindows>
                        binFileExtensions>
    
                        
                        <encoding>UTF-8encoding>
                        <logsDirectory>logslogsDirectory>
                        <tempDirectory>tmptempDirectory>
    
                        
                        <extraJvmArguments>-Xms128mextraJvmArguments>
    
                        <programs>
                            <program>
                                
                                <mainClass>com.zrj.maven.assembly.demo.MavenAssemblyDemoApplicationmainClass>
                                
                                <name>startname>
                            program>
                        programs>
                    configuration>
                plugin>
            plugins>
        build>
    
    project>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106

    3. 测试验证

    3.1 执行命令

    执行命令或者使用idea
    执行命令:maven install
    idea:maven-lifecycle依次执行clean,install

    3.2 生成脚本

    |—— target
    |   |—— client
    |   |   |—— bin
    |   |   |   |—— start.sh              # linux平台下的启动脚本
    |   |   |   |—— start.bat            # windows平台下的启动脚本
    |   |—— conf
    |   |   |—— application.properties    # 项目配置文件
    |   |—— lib
    |   |   |—— 各种依赖jar包
    |   |—— logs
    |   |—— tmp
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    在这里插入图片描述

    3.3 启动脚本验证

    ./start.bat 或者./start.sh
    
    • 1

    浏览器访问:http://localhost:8080/
    刷新页面:1668682483758:Hello World!

  • 相关阅读:
    demo:flex每个属性展示
    FFmpeg 命令:从入门到精通 | ffppeg 命令提取音视频数据
    GPU使用和管理经验
    使用代理IP常见问题及解答
    SSM整合_年轻人的第一个增删改查_新增
    .NET中使用Quartz
    SimpleAdapter和RecyclerView纪录
    Qt软件开发-Qt编译zlib完成文件压缩解压(win10)
    软件工程测试与度量课程学习---基本测试过程----线性模型
    C++类内定义友元却无需类外声明的特殊情况
  • 原文地址:https://blog.csdn.net/m0_37583655/article/details/127909634