目录
- 封装文档说明
- 如有跨域问题在ems服务增加配置
- 服务启动自动打开浏览器
- 使用工具 Launch4j 把jar包打成exe执行文件
- 现在还不能给用户用,因为缺少jre,后面整合资源会把jre一起打包
- 使用inno setup合并资源,mysql之类的服务,最终打包成exe安装包
封装文档说明
主要封装的作用:简化用户的操作,一键安装,直接使用,无需多余配置
直接把安装包甩给用户,用户开箱即用!!!
前端访问后端url设为http://localhost:1060/api/
后端mysql连接设为localhost 用户名root,密码tecloman
SpringBoot使用内置tomcat启动vue
前端dist打包后的文件夹放到/resource/static目录下
后端配置增加
spring:web:resources:static-locations: classpath:/static/
如有跨域问题在ems服务增加配置
import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.web.cors.CorsConfiguration;import org.springframework.web.cors.UrlBasedCorsConfigurationSource;import org.springframework.web.filter.CorsFilter;@Configurationpublic class CorsConfig {@Beanpublic CorsFilter corsFilter() {CorsConfiguration config = new CorsConfiguration();config.setAllowCredentials(true); // 如果需要携带cookie,设为trueconfig.addAllowedMethod("*"); // 允许所有HTTP方法config.addAllowedHeader("*"); // 允许所有请求头// 显式列出允许的源或者使用allowedOriginPatterns// 允许来自localhost:1060的请求config.addAllowedOrigin("http://localhost:1060");// 或者使用allowedOriginPatterns// config.setAllowedOriginPatterns(Arrays.asList("http://*.yourdomain.com"));UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();source.registerCorsConfiguration("/**", config); // 对所有路径应用CORS配置return new CorsFilter(source);}}
服务启动自动打开浏览器
import lombok.extern.slf4j.Slf4j;import org.springframework.boot.CommandLineRunner;import org.springframework.stereotype.Component;import java.io.IOException;@Slf4j@Componentpublic class StartupRunner implements CommandLineRunner {@Overridepublic void run(String... args) {// 服务启动打开浏览器openBrowser("http://localhost:1060/hss_web/index.html");}private void openBrowser(String url) {String os = System.getProperty("os.name").toLowerCase();try {if (os.contains("win")) {Runtime.getRuntime().exec("cmd /c start " + url);} else if (os.contains("mac") || os.contains("darwin")) {Runtime.getRuntime().exec("open " + url);}} catch (IOException e) {System.err.println("Failed to open browser: " + e.getMessage());}}}
使用工具 Launch4j 把jar包打成exe执行文件
服务打成jar包
下载封装程序相关软件.zip,解压后如下
将ems服务jar包放到解析目录,改名为ems.jar
打开Launch4j文件夹下的launch4j.exe,并打开配置文件,ems.xml
界面如下,只要jar包放在解压目录则不用更改
唯一需要改的地方,更改启动配置文件
最后点击齿轮保存文件并生成exe文件,就会像图1一样多了ems.exe文件,本机可直接启动,
现在还不能给用户用,因为缺少jre,后面整合资源会把jre一起打包
使用inno setup合并资源,mysql之类的服务,最终打包成exe安装包
安装合并工具Inno Setup v6.0.4 汉化版,在解压目录下
打开innosetup 再打开封装脚本文件.iss,如下图
```yaml
; Script generated by the Inno Setup Script Wizard.
; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES!#define MyAppName "EmsApp"
#define MyAppVersion "1.5"
#define MyAppPublisher "tecloman"
#define MyAppURL "https://www.tecloman.com/"
#define MyAppExeName "ems.exe"[Setup]
; NOTE: The value of AppId uniquely identifies this application.
; Do not use the same AppId value in installers for other applications.
; (To generate a new GUID, click Tools | Generate GUID inside the IDE.)
AppId={{FC10B53B-96FE-4703-AFD3-10E414E9EE47}
AppName={#MyAppName}
AppVersion={#MyAppVersion}
AppVerName={#MyAppName} {#MyAppVersion}
AppPublisher={#MyAppPublisher}
AppPublisherURL={#MyAppURL}
AppSupportURL={#MyAppURL}
AppUpdatesURL={#MyAppURL}
DefaultDirName={pf}\{#MyAppName}
DisableProgramGroupPage=yes
OutputDir=C:\Users\Administrator\Desktop
OutputBaseFilename=emsSetup
;SetupIconFile=C:\Users\Administrator\Desktop\add\jdk1.8-windows-x64_00001.ico
Compression=lzma
SolidCompression=yes[Languages]
Name: "English"; MessagesFile: "compiler:Default.isl"[Tasks]
Name: "desktopicon"; Description: "{cm:CreateDesktopIcon}"; GroupDescription: "{cm:AdditionalIcons}"; Flags: unchecked
[Files]
Source: "C:\Users\Administrator\Desktop\add\ems.exe"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Users\Administrator\Desktop\add\*"; DestDir: "{app}"; Flags: ignoreversion recursesubdirs createallsubdirs
; NOTE: Don't use "Flags: ignoreversion" on any shared system files
[Icons]
Name: "{commonprograms}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"
Name: "{commondesktop}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"; Tasks: desktopicon
[Setup]
PrivilegesRequired=admin
SetupLogging=yes[INI]
;修改数据库配置文件
Filename:"{app}\mysql\my.ini";Section:"mysqld";Key:"basedir"; String:"{app}\mysql";
Filename:"{app}\mysql\my.ini";Section:"mysqld";Key:"datadir"; String:"{app}\mysql\data";
[Code]
[Run]
;Filename: "notepad.exe"; Parameters: "{app}\mysql\my.ini"; StatusMsg: "正在检查my.ini配置文件...请确认修改后关闭Notepad以继续安装。"; Flags: waituntilterminated ;Filename: "{app}\mysql.bat"; Description: "正在执行启动脚本..."; Flags: runhidden mysqld --initialize --console
Filename: "{app}\{#MyAppExeName}"; Description: "{cm:LaunchProgram,{#StringChange(MyAppName, '&', '&&')}}"; Flags: nowait postinstall skipifsilent
Filename: "{app}\mysql.bat"; Description: "正在执行MySQL服务安装和启动脚本..."; Flags: shellexec[UninstallRun]
; 停止MySQL服务
Filename: "net"; Parameters: "stop MySQLService"; StatusMsg: "正在停止MySQL服务..."; Flags: runhidden waituntilterminated
; 卸载MySQL服务
Filename: "{app}\mysql\bin\mysqld.exe"; Parameters: "--remove MySQLService"; WorkingDir: "{app}\mysql\bin"; StatusMsg: "正在卸载MySQL服务..."; Flags: runhidden waituntilterminated
资源路径下的mysql,我是初始化过的,设置过密码 root tecloman,所以安装完成后只需要注册服务并启动就可以了,这由mysql.bat做处理,我们需要提前把EMS相关的数据库提前建立好,所以在资源路径下的mysql\data目录下就得先弄好,最后生成安装包,用户解压即可使用
# 初始化,密码会显示在控制台 最后一行mysqld --initialize --console# 注册服务mysqld --install MysqlService --defaults-file="C:\Users\Administrator\Desktop\add\mysql\my.ini"# 启动服务net start MysqlService# 登陆mysql -uroot -pHj6u5Kp.)ti9# 修改密码ALTER USER 'root'@'localhost' IDENTIFIED BY 'tecloman';# 刷新权限 FLUSH PRIVILEGES;
如果注册服务失败,肯定是配置文件有问题,注册成功后会在/mysql目录生成data目录,data目录主要存放ems相关的数据库数据
连接数据库把ems相关的数据拷贝到当前mysql就可以了,mysql就可以放到资源目录下,把刚创建的mysql服务停止移除,sc delete MySqlService 否则打包程序运行不了
资源目录详情
jre是运行jar必备的,
mysql就是刚初始化过的,不要直接把未初始化过的放过来,因为mysql初始化是不能手动设置密码的,
mysql.bat是安装后运行的批处理文件,如下,安装包会自动执行
set “CURRENT_DIR=%~dp0” 这代表当前目录的变量(安装目录)
setlocal enabledelayedexpansionset "CURRENT_DIR=%~dp0"@echo 注册服务"%CURRENT_DIR%mysql\bin\mysqld.exe" --install MySQLService --defaults-file="%CURRENT_DIR%mysql\my.ini"@echo 启动服务net start MySQLServicepause
编译
会在输出目录生成一个exe安装文件
innosetup语法大全