需求:由于java web项目,需要依赖mysql数据库、 tomcat容器 、jdk等,部署需要一定技术支持才能够完成, *** 作繁琐,希望简化。
目标:制作一个exe一键安装相关依赖并启动项目,简化部署 *** 作,到达快速演示的目的。
需要技术:批处理、inno setup打包工具
*** 作步骤:
一、准备相关文件
二、编写安装脚本串联部署 *** 作、卸载脚本、数据库初始化脚本等
例如:install.bat
@echo off title 软件配置安装中,请不要手动关闭 Rd "%WinDir%system32rainss_perm" >NUL 2>NUL Md "%WinDir%System32rainss_perm" 2>NUL||(Echo 请使用右键管理员身份运行!&&Pause >nul&&Exit) Rd "%WinDir%System32rainss_perm" 2>NUL %1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exit color 0a echo ++++++++++++++ 设置环境变量 set JAVA_HOME=%~dp0java set PATH=%PATH%;%%JAVA_HOME%%jrebin set CLASSPATH=.;%%JAVA_HOME%%jrelib set RegV=HKLMSYSTEMCurrentControlSetControlSession ManagerEnvironment reg add "%RegV%" /v "JAVA_HOME" /d "%JAVA_HOME%" /f reg add "%RegV%" /v "Path" /t REG_EXPAND_SZ /d "%PATH%" /f reg add "%RegV%" /v "CLASSPATH" /d "%CLASSPATH%" /f echo ++++++++++++++ 环境变量设置完毕 set MPath=%~dp0mysql set "MPath=%Mpath:=\%" cd /d %MPath% echo ++++++++++++++ 检查服务MySQL sc query mysql>nul &&( echo ++++++++++++++ 存在MySQL服务 & echo ++++++++++++++ 安装是否继续?Y/N ) || goto install set /p rains=继续吗?(Y/N): if "%rains%" == "Y" (goto reinstall) else (if "%rains%" == "y" (goto reinstall) else ( echo ++++++++++++++ 安装已经取消 & goto finish )) exit :reinstall echo ++++++++++++++ 正在停止MySQL net stop mysql echo ++++++++++++++ 删除 MySQL服务 sc delete mysql echo ++++++++++++++ 开始写入配置文件 ( echo # For advice on how to change settings please see echo # http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html echo # *** DO NOT EDIT THIS FILE. It's a template which will be copied to the echo # *** default location during install, and will be replaced if you echo # *** upgrade to a newer version of MySQL. echo [mysqld] echo port = 3306 echo basedir = %MPath% echo datadir = %MPath%data echo # server_id = ..... echo # Remove leading # to set options mainly useful for reporting servers. echo # The server defaults are faster for transactions and fast SELECTs. echo # Adjust sizes as needed, experiment to find the optimal values. echo # join_buffer_size = 128M echo # sort_buffer_size = 2M echo # read_rnd_buffer_size = 2M echo sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES )>my.ini echo ++++++++++++++ 配置文件写入完成 echo ++++++++++++++ 删除数据目录 rmdir /s/q data cd bin echo ++++++++++++++ 注册MySQL服务 mysqld install MySQL echo ++++++++++++++ 创建数据目录 mysqld --initialize-insecure mysqld install echo ++++++++++++++ 正在启动MySQL net start mysql echo ++++++++++++++ 修改MySQL密码 mysql -u root -e "use mysql;update user set authentication_string = password('123456'), password_expired = 'N', password_last_changed = now() where user = 'root';flush privileges;" echo 至此,数据库 *** 作完成 goto end exit :install echo ++++++++++++++ 开始写入配置文件 ( echo # For advice on how to change settings please see echo # http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html echo # *** DO NOT EDIT THIS FILE. It's a template which will be copied to the echo # *** default location during install, and will be replaced if you echo # *** upgrade to a newer version of MySQL. echo [mysqld] echo port = 3306 echo basedir = %MPath% echo datadir = %MPath%data echo # server_id = ..... echo # Remove leading # to set options mainly useful for reporting servers. echo # The server defaults are faster for transactions and fast SELECTs. echo # Adjust sizes as needed, experiment to find the optimal values. echo # join_buffer_size = 128M echo # sort_buffer_size = 2M echo # read_rnd_buffer_size = 2M echo sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES )>my.ini echo ++++++++++++++ 配置文件写入完成 cd bin echo ++++++++++++++ 注册MySQL服务 mysqld install MySQL echo ++++++++++++++ 创建数据目录 mysqld --initialize-insecure mysqld install echo ++++++++++++++ 正在启动MySQL net start mysql echo ++++++++++++++ 修改MySQL密码 mysql -u root -e "use mysql;update user set authentication_string = password('123456'), password_expired = 'N', password_last_changed = now() where user = 'root';flush privileges;" goto end; exit :end echo ++++++++++++++ 密码初始化成功 :finish echo ++++++++++++++ mysql安装结束 echo ++++++++++++++ 开始导入数据 set sql1path=%~dp0sqldatabase.sql set sql2path=%~dp0sqlxxx.sql set logpath=%~dp0dblog.txt set tomcatpath=%~dp0tomcatbin %~dp0mysqlbinmysql.exe -h localhost -uroot -p123456 < %sql1path%>>%logpath% 2>&1 %~dp0mysqlbinmysql.exe -h localhost -uroot -p123456 --default-character-set=utf8 xxx < %sql2path%>>%logpath% 2>&1 echo ++++++++++++++ 导入数据结束 del /f /s /q %~dp0sql*.* rd /s /q %~dp0sql echo ++++++++++++++ 初始化目录 xcopy /y /e /h %~dp0xxxc:xxx >nul cd %tomcatpath% echo ++++++++++++++ 停止tomcat服务 call shutdown.bat echo ++++++++++++++ 启动tomcat服务 call startup.bat echo ++++++++++++++ 浏览器打开系统 http://localhost:8080/xxx for /f "tokens=3,4" %%a in ('"reg query HKEY_CLASSES_ROOThttpshellopencommand"') do (set SoftWareRoot=%%a %%b) start http://localhost:8080/xxx del %0
三、使用inno setup工具打包成安装程序
配置文件如下:
; script generated by the Inno Setup script Wizard. ; SEE THE documentATION FOR DETAILS ON CREATING INNO SETUP script FILES! #define MyAppName "xxx系统" #define MyAppVersion "V1.2" #define MyAppPublisher "武汉xxx股份有限公司" #define MyAppURL "http://www.xxx.com/" [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={{DCCFE898-2E2D-4C7B-9A45-C81B3A50F80C} AppName={#MyAppName} AppVersion={#MyAppVersion} ;AppVerName={#MyAppName} {#MyAppVersion} AppPublisher={#MyAppPublisher} AppPublisherURL={#MyAppURL} AppSupportURL={#MyAppURL} AppUpdatesURL={#MyAppURL} DefaultDirName=d:\soft DefaultGroupName={#MyAppName} OutputbaseFilename=zgfk-setup SetupIconFile=C:项目工作ynjw部署windowsinnosetuplogo.ico Compression=lzma SolidCompression=yes PrivilegesRequired=admin UninstallDisplayName=卸载{#MyAppName} [Languages] Name: "chinesesimp"; MessagesFile: "compiler:Default.isl" [Files] ;拷贝tomcat Source:".tomcat*";DestDir:"{app}tomcat";Flags:igNoreversion recursesubdirs createallsubdirs ;拷贝data Source:".xxx*";DestDir:"{app}xxx";Flags:igNoreversion recursesubdirs createallsubdirs ;拷贝mysql Source:".mysql*";DestDir:"{app}mysql";Flags:igNoreversion recursesubdirs createallsubdirs ;拷贝java包 Source:".java*";DestDir:"{app}java";Flags:igNoreversion recursesubdirs createallsubdirs ;拷贝sql包 Source:".sql*";DestDir:"{app}sql";Flags:igNoreversion recursesubdirs createallsubdirs ;拷贝脚本包 Source:".other*";DestDir:"{app}";Flags:igNoreversion recursesubdirs createallsubdirs [Icons] Name: "{group}{cm:ProgramOnTheWeb,{#MyAppName}}"; Filename: "{#MyAppURL}" Name: "{group}{cm:UninstallProgram,{#MyAppName}}"; Filename: "{uninstallexe}" [Run] ;修改服务配置文件 Filename:"{app}install.bat";Flags:runhidden;Description:"正在启动或配置相关程序"; [UninstallRun] Filename:"{app}uninstall.bat";Flags:runhidden; [UninstallDelete] Type:filesandordirs;Name:"{app}mysql" Type:filesandordirs;Name:"{app}tomcat" Type:filesandordirs;Name:"{app}xxx" Type:filesandordirs;Name:"{app}java" Type:files;Name:"{app}InstallUtil.InstallLog"
四、最终成品
需要演示,一键安装,使用完毕一键卸载,无残留。
五、相关批处理下载:
javaweb项目安装卸载批处理.zip
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)