🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
案例代码:https://gitee.com/flymini/codes02/tree/master/maven_plugins_/com-learn-plugin02 **** appassembler-maven-plugin 插件可以为 java 项目自动生成启动脚本,并且支持多个平台(win,unix/linux、macos)。 <br/> appassembler-maven-plugin 插件为 SpringBoot 项目生成启动脚本,步骤如下: **1. pom中引入插件:appassembler-maven-plugin** ```xml <build> <plugins> <!-- spring-boot-maven-plugin插件与appassembler-maven-plugin插件不兼容,不要将spring-boot-maven-plugin插件引进来 --> <!-- <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> --> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>appassembler-maven-plugin</artifactId> <version>2.1.0</version> <configuration> <platforms> <platform>unix</platform> <platform>windows</platform> </platforms> <!--打包后生成的target目录路径,如D:/workspace/com-learn-plugin02/target--> <assembleDirectory>${project.build.directory}/${project.name}</assembleDirectory> <!-- flat与lib共同决定将项目用的的所有jar包复制到lib目录下 --> <repositoryLayout>flat</repositoryLayout> <repositoryName>lib</repositoryName> <!--启动脚本存放在bin目录--> <binFolder>bin</binFolder> <!--配置文件存放在conf目录路径--> <configurationDirectory>conf</configurationDirectory> <!--是否copy配置文件--> <copyConfigurationDirectory>true</copyConfigurationDirectory> <!--从哪里copy配置文件--> <configurationSourceDirectory>src/main/resources</configurationSourceDirectory> <includeConfigurationDirectoryInClasspath>true</includeConfigurationDirectoryInClasspath> <binFileExtensions> <!-- 针对不同平台生成不同类型的启动脚本 --> <unix>.sh</unix> <windows>.bat</windows> </binFileExtensions> <encoding>UTF-8</encoding> <logsDirectory>logs</logsDirectory> <tempDirectory>tmp</tempDirectory> <daemons> <daemon> <!-- 启动脚本的名称,Linux平台上就是:app,windows平台上就是app.bat --> <id>app</id> <!-- 启动类 --> <mainClass>com.learn.plugin02.Plugin02Application</mainClass> <platforms> <platform>jsw</platform> </platforms> <generatorConfigurations> <generatorConfiguration> <generator>jsw</generator> <includes> <include>linux-x86-32</include> <include>linux-x86-64</include> <include>windows-x86-32</include> <include>windows-x86-64</include> </includes> </generatorConfiguration> </generatorConfigurations> <jvmSettings> <!-- 启动时的一下jvm参数配置 --> <extraArguments> <extraArgument>-server</extraArgument> <extraArgument>-Xms256M</extraArgument> <extraArgument>-Xmx256M</extraArgument> <extraArgument>-Xss512k</extraArgument> <extraArgument>-Xloggc:logs/demo_gc.log</extraArgument> <extraArgument>-verbose:gc</extraArgument> <extraArgument>-XX:+HeapDumpOnOutOfMemoryError</extraArgument> <extraArgument>-XX:HeapDumpPath=logs/java_heapdump.hprof</extraArgument> </extraArguments> </jvmSettings> </daemon> </daemons> <programs> <program> <mainClass>com.learn.plugin02.Plugin02Application</mainClass> <id>demoApp</id> </program> </programs> </configuration> <!-- 如果不配置 generate-daemons,则打包命令为 mvn clean package appassembler:generate-daemons --> <!-- 如果配置了 generate-daemons,打包命令可以是 mvn clean package 也可以是 mvn clean package appassembler:generate-daemons --> <executions> <execution> <inherited>true</inherited> <phase>package</phase> <goals> <goal>generate-daemons</goal> </goals> </execution> </executions> </plugin> </plugins> </build> ``` **2. 执行下面的打包命令** ```shell mvn clean package appassembler:generate-daemons ``` **3. 打包后生成的目录** appassembler 插件打包后生成的资源统统放在 target/generated-resources 目录下。 ``` |—— target | |—— generated-resources | | |—— appassembler | | | |—— jsw | | | | |—— app | | | | | |—— bin | | | | | | |—— app # linux平台下的启动脚本 | | | | | | |—— app.bat # windows平台下的启动脚本 | | | | | | |—— wrapper-linux-x86-32 | | | | | | |—— wrapper-linux-x86-64 | | | | | | |—— wrapper-linux-x86-32.exe | | | | | | |—— wrapper-linux-x86-64.exe | | | | | |—— conf | | | | | | |—— application.yml # 项目配置文件 | | | | | | |—— wrapper.conf # 运行环境配置文件 | | | | | |—— lib | | | | | | |—— 大量的jar包 | | | | | |—— logs | | | | | |—— tmp ``` **4. windows平台上启动项目** 只需要 app 目录下的资源,可以将 app 目录的复制出来。windows 平台需要以管理员身份打开cmd窗口,否则无法执行脚本。 ```shell (1)首次执行需要 app.bat install > app.bat install (2)启动项目 > app.bat start (3)查询项目状态 > app.bat status (4)停止项目 > app.bat stop (5)重启项目 > app.bat restart ``` **5. linux平台上启动项目** 只需要 app 目录下的资源,将 app 目录打包为`.zip`、或`.tar.gz`压缩包,然后上传到 Linux 机器上。 ```shell (1)解压 $ unzip app.zip (2)赋予脚本执行权限 $ cd app $ chmod +x bin/app bin/wrapper-linux-x86-* (3)启动项目 $ ./bin/app start (4)查询项目状态 $ ./bin/app status (5)停止项目 $ ./bin/app stop (6)重启项目 $ ./bin/app restart (7)查看日志 $ tail -f logs/wrapper.log ```