SpringBoot项目打包为exe,一键运行,整合mysql+jre

SpringBoot项目打包(exe+jre+mysql)一键安装,吐血总结,实测可用

一、打包

- 1、将SpringBoot项目打包为jar包(数据库配置建议改为本地,因后续数据库安装在本地)
( 以下为pom文件)

    <!--打包方式-->
	<packaging>jar</packaging>
	 <!--构建插件-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

1.1 修改完成后,使用idea自带的打包工具打包,maven package,生成jar包;
1.2 该jar包可直接使用Java -jar 运行(jar包位于工程target目录下)。

二、下载exe4j工具

在这里插入图片描述
1. 准备一个新的文件夹将jar包和jre放到一起
在这里插入图片描述
2. exe4j将jar包打包为exe应用

2.1 百度找一个exe4j的注册码
在这里插入图片描述

2.2 Next选择第二个“JAR in EXE mode”

在这里插入图片描述

2.3 Next填写名称及生成位置

在这里插入图片描述

2.4 选择操作系统32位或者64位

在这里插入图片描述
在这里插入图片描述
2.5 左边选择第5步,配置启动参数,点击绿色的➕添加jar包,选则启动类
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
2.6 填写jre版本,配置jre位置
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
2.7 一路Next结束
在这里插入图片描述
在这里插入图片描述
eg.如果你不要数据库,此时就结束了,运行即可,随后在任务管理器可以看到你运行的程序,浏览器访问即可.
在这里插入图片描述

三、下载Info Setup编译器

在这里插入图片描述
3.1 准备一个包含所需数据库的mysql安装目录(这样安装后可直接使用初始化数据,不用导入sql)
在这里插入图片描述
3.2 编写mysql_init.bat脚本和mysql_stop.bat,将文件放到bin目录下
在这里插入图片描述
mysql_init.bat,使用txt编写时注意格式位ANSI编码

cd %~dp0
cd ..
del %cd%\my.ini
echo 删除完成
echo [mysqld]>> my.ini
echo # 设置3306端口>> my.ini
echo port=3306>> my.ini
echo # 设置mysql的安装目录>> my.ini
echo basedir=%cd%>> my.ini
echo # 设置mysql数据库的数据的存放目录>> my.ini
echo datadir=%cd%\data>> my.ini
echo # 允许最大连接数>> my.ini
echo max_connections=200>> my.ini
echo # 允许连接失败的次数。这是为了防止有人从该主机试图攻击数据库系统>> my.ini
echo max_connect_errors=10>> my.ini
echo # 服务端使用的字符集默认为UTF8>> my.ini
echo character-set-server=utf8>> my.ini
echo # 创建新表时将使用的默认存储引擎>> my.ini
echo default-storage-engine=INNODB>> my.ini
echo [mysql]>> my.ini
echo # 设置mysql客户端默认字符集>> my.ini
echo default-character-set=utf8>> my.ini
echo [client]>> my.ini
echo # 设置mysql客户端连接服务端时默认使用的端口>> my.ini
echo port=3306>> my.ini
echo default-character-set=utf8>> my.ini
echo my.ini生成成功
set inipath=%cd%\my.ini
cd bin
"%cd%\mysqld.exe" -remove mysql
"%cd%\mysqld.exe" -install mysql --defaults-file="%inipath%"
"%cd%\mysqld.exe" --initialize-insecure --user=mysql --console
net start mysql
sc config mysql start=auto
net stop mysql
net start mysql
echo 安装完毕

mysql_stop.bat

cd %~dp0
net stop mysql
sc delete mysql-job
"%cd%\mysqld.exe" -remove mysql
echo mysql卸载完成
exit

3.3 打开Info Setup,选择第二个
在这里插入图片描述
3.4 一直下一步到选择exe,添加mysql和jre,弹框选择是,意思是包含所有文件
在这里插入图片描述
在这里插入图片描述

3.5 继续下一步直到选择文件输出路径
在这里插入图片描述
3.6 下一步,直到立即编译脚本,选择 否
在这里插入图片描述
3.7 修改脚本
原脚本

; 脚本由 Inno Setup 脚本向导 生成!
; 有关创建 Inno Setup 脚本文件的详细资料请查阅帮助文档!

#define MyAppName "我的程序"
#define MyAppVersion "1.5"
#define MyAppPublisher "我的公司"
#define MyAppURL "http://www.example.com/"
#define MyAppExeName "myExe.exe"

[Setup]
;: AppId的值为单独标识该应用程序。
; 不要为其他安装程序使用相同的AppId值。
; (若要生成新的 GUID,可在菜单中点击 "工具|生成 GUID")
AppId={{4A78DE64-0E3F-4EA1-BCE1-7A01E66BC1FF}
AppName={#MyAppName}
AppVersion={#MyAppVersion}
;AppVerName={#MyAppName} {#MyAppVersion}
AppPublisher={#MyAppPublisher}
AppPublisherURL={#MyAppURL}
AppSupportURL={#MyAppURL}
AppUpdatesURL={#MyAppURL}
DefaultDirName={autopf}\{#MyAppName}
DisableProgramGroupPage=yes
; 以下行取消注释,以在非管理安装模式下运行(仅为当前用户安装)。
;PrivilegesRequired=lowest
OutputDir=C:\Users\wkwyM\Desktop\test
OutputBaseFilename=mysetup
Compression=lzma
SolidCompression=yes
WizardStyle=modern

[Languages]
Name: "chinesesimp"; MessagesFile: "compiler:Default.isl"

[Tasks]
Name: "desktopicon"; Description: "{cm:CreateDesktopIcon}"; GroupDescription: "{cm:AdditionalIcons}"; Flags: unchecked

[Files]
Source: "C:\Users\wkwyM\Desktop\test\myExe.exe"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Users\wkwyM\Desktop\test\mysql-8.0.27-winx64\*"; DestDir: "{app}"; Flags: ignoreversion recursesubdirs createallsubdirs
Source: "C:\Users\wkwyM\Desktop\test\jre\*"; DestDir: "{app}"; Flags: ignoreversion recursesubdirs createallsubdirs
; 注意: 不要在任何共享系统文件上使用“Flags: ignoreversion”

[Icons]
Name: "{autoprograms}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"
Name: "{autodesktop}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"; Tasks: desktopicon

[Run]
Filename: "{app}\{#MyAppExeName}"; Description: "{cm:LaunchProgram,{#StringChange(MyAppName, '&', '&&')}}"; Flags: nowait postinstall skipifsilent


3.8 修改后脚本

; 脚本由 Inno Setup 脚本向导 生成!
; 有关创建 Inno Setup 脚本文件的详细资料请查阅帮助文档!

#define MyAppName "我的程序"
#define MyAppVersion "1.5"
#define MyAppPublisher "我的公司"
#define MyAppURL "http://www.example.com/"
#define MyAppExeName "myExe.exe"
#define MysqlName "mysql-8.0.27-winx64"

[Setup]
;: AppId的值为单独标识该应用程序。
; 不要为其他安装程序使用相同的AppId值。
; (若要生成新的 GUID,可在菜单中点击 "工具|生成 GUID")
AppId={{26838C49-A6E9-440C-9DA3-0DFA2352119C}
AppName={#MyAppName}
AppVersion={#MyAppVersion}
;AppVerName={#MyAppName} {#MyAppVersion}
AppPublisher={#MyAppPublisher}
AppPublisherURL={#MyAppURL}
AppSupportURL={#MyAppURL}
AppUpdatesURL={#MyAppURL}
DefaultDirName={autopf}\{#MyAppName}
DisableProgramGroupPage=yes
; 以下行取消注释,以在非管理安装模式下运行(仅为当前用户安装)。
;PrivilegesRequired=lowest
OutputDir=C:\Users\wkwyM\Desktop\jarTest
OutputBaseFilename=线缆1.2
SetupIconFile=C:\Users\wkwyM\Desktop\jarTest\Tyrannosaurus rex.ico
Compression=lzma
SolidCompression=yes
WizardStyle=modern

[Languages]
Name: "chinesesimp"; MessagesFile: "compiler:Default.isl"

[Tasks]
Name: "desktopicon"; Description: "{cm:CreateDesktopIcon}"; GroupDescription: "{cm:AdditionalIcons}"; Flags: unchecked

[Files]
Source: "C:\Users\wkwyM\Desktop\jarTest\{#MyAppExeName}"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Users\wkwyM\Desktop\jarTest\jre\*"; DestDir: "{app}\jre"; Flags: ignoreversion recursesubdirs createallsubdirs
Source: "C:\Users\wkwyM\Desktop\jarTest\var\*"; DestDir: "c:\var"; Flags: ignoreversion recursesubdirs createallsubdirs
Source: "C:\Users\wkwyM\Desktop\jarTest\{#MysqlName}\*"; DestDir: "{app}\{#MysqlName}"; Flags: ignoreversion recursesubdirs createallsubdirs
; 注意: 不要在任何共享系统文件上使用“Flags: ignoreversion”

[Icons]
Name: "{autoprograms}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"
Name: "{autodesktop}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"; Tasks: desktopicon

[Run]
Filename: "{app}\{#MysqlName}\bin\mysql_init.bat";
Filename: "{app}\{#MyAppExeName}"; Description: "{cm:LaunchProgram,{#StringChange(MyAppName, '&', '&&')}}"; Flags: nowait postinstall skipifsilent

[UninstallRun]
Filename: "{app}\{#MysqlName}\bin\mysql_stop.bat";

[Code]
// 自定义函数,判断软件是否运行,参数为需要判断的软件的exe名称
function CheckSoftRun(strExeName: String): Boolean;
// 变量定义
var ErrorCode: Integer;
var bRes: Boolean;
var strFileContent: AnsiString;
var strTmpPath: String;  // 临时目录
var strTmpFile: String;  // 临时文件,保存查找软件数据结果
var strCmdFind: String;  // 查找软件命令
var strCmdKill: String;  // 终止软件命令
begin
  strTmpPath := GetTempDir();
  strTmpFile := Format('%sfindSoftRes.txt', [strTmpPath]);
  strCmdFind := Format('/c tasklist /nh|find /c /i "%s" > "%s"', [strExeName, strTmpFile]);
  strCmdKill := Format('/c taskkill /f /t /im %s', [strExeName]);
  bRes := ShellExec('open', ExpandConstant('{cmd}'), strCmdFind, '', SW_HIDE, ewWaitUntilTerminated, ErrorCode);
  if bRes then begin
      bRes := LoadStringFromFile(strTmpFile, strFileContent);
      strFileContent := Trim(strFileContent);
      if bRes then begin
         if StrToInt(strFileContent) > 0 then begin
            if MsgBox(ExpandConstant('{cm:checkSoftTip}'), mbConfirmation, MB_OKCANCEL) = IDOK then begin
             // 终止程序
             ShellExec('open', ExpandConstant('{cmd}'), strCmdKill, '', SW_HIDE, ewNoWait, ErrorCode);
             Result:= true;// 继续安装
            end else begin
             Result:= false;// 安装程序退出
             Exit;
            end;
         end else begin
            // 软件没在运行
            Result:= true;
            Exit;
         end;
      end;
  end;
  Result :=true;
end;

// 重复安装检测
function InitializeSetup():boolean;
var
  MykeynotExist:boolean;
  ResultCode: Integer;
  uicmd: String;
begin
  MykeynotExist:= true;
  if RegQueryStringValue(HKEY_LOCAL_MACHINE, 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{4A78DE64-0E3F-4EA1-BCE1-7A01E66BC1FF}_is1', 'UninstallString', uicmd) then
  begin
  MsgBox('{#MyAppName}已经安装,你确定要先卸载,再安装!', mbInformation, MB_OK)
  MyKeynotExist:= false;
  Exec(RemoveQuotes(uicmd), '', '', SW_SHOW, ewWaitUntilTerminated, ResultCode);
  end;
  Result:= MykeynotExist
end;

// 卸载时关闭软件
function InitializeUninstall(): Boolean;
begin
  Result := CheckSoftRun('{#MyAppExeName}');
end; 

procedure CurUninstallStepChanged(CurUninstallStep: TUninstallStep);
begin
    if CurUninstallStep = usDone then
    begin
       //删除 {app} 文件夹及其中所有文件
    DelTree(ExpandConstant('{app}'), True, True, True);
    end;
end;

[CustomMessages]
chinesesimp.checkSoftTip=检测到软件正在运行!%n%n点击"确定"终止软件后继续操作,否则点击"取消"

不同点:
在这里插入图片描述
其余的为自定义函数,建议直接使用,均为友好性提示

3.9 编译运行,等进度条完成后安装使用即可
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

3.10 安装完成后不要运行,一定要以管理员身份运行
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 8
    点赞
  • 63
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
要将Spring Boot项目打包exe文件,可以按照以下步骤进行操作: 1. 首先,确保你的Spring Boot项目已经配置了正确的插件,以便生成一个可执行的JAR文件。在项目的pom.xml文件中,添加或更新以下内容: ``` <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <executable>true</executable> </configuration> </plugin> </plugins> </build> ``` 这样配置后,使用Maven构建项目时,会生成一个可执行的JAR文件。 2. 下载并安装exe4j,它是一个用于将JAR文件打包exe文件的工具。你可以从官网地址https://exe4j.apponic.com/download/下载并安装。 3. 打开exe4j安装目录下的bin/exe4j.exe文件。 4. 在操作面板中,选择"JAR in EXE"模式,并点击下一步。 5. 输入应用名称和输出路径,然后点击下一步。 6. 选择启动模式和应用名称,可以选择GUI启动、打印台启动或Windows服务启动。 7. 点击完成,exe4j会将JAR文件和批处理文件打包成一个exe文件。 另外,如果你想将exe文件和JRE打包合并,以便在没有JDK环境的电脑上也能运行,你可以下载并安装inno setup工具。你可以从官网地址https://jrsoftware.org/isinfo.php下载并安装inno setup,然后按照它的说明进行操作。 请注意,以上步骤仅供参考,具体操作可能会因个人环境和需求而有所不同。 #### 引用[.reference_title] - *1* *3* [Spring Boot Web项目打包exe文件](https://blog.csdn.net/HongZeng_CSDN/article/details/130369913)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [SpringBoot项目打成.exe程序](https://blog.csdn.net/weixin_44635157/article/details/122817808)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值