使用NSIS制作JavaEE项目的Windows安装包脚本。
包含了对JDK,Tomcat,MySQL,FTP Server FileZilla的系统配置和启停,属于典型的安装好无需做任何配置和更改的一键式安装包。MySQL属于免安装版本。
里面涉及了获取本机IP的脚本,需要将ip.dll放在NSIS安装路径下的Plugins目录。
同样涉及了修改XML文件的脚本,主要是用来配置FTP的配置文件,需要将xml.dll放在NSIS安装路径下的插件目录,同时将XML.nsh文件放到NSIS安装路径下的Include目录。
具体脚本如下:
; Script generated by the HM NIS Edit Script Wizard. ; HM NIS Edit Wizard helper defines !define PRODUCT_NAME "XXX系统" !define PRODUCT_VERSION "V1.0.0 Trial Edition" !define PRODUCT_PUBLISHER "XXXX公司" !define PRODUCT_UNINST_KEY "Software/Microsoft/Windows/CurrentVersion/Uninstall/${PRODUCT_NAME}" !define PRODUCT_UNINST_ROOT_KEY "HKLM" !define PRODUCT_STARTMENU_REGVAL "NSIS:sTARTmENUdIR" SetCompressor lzma ; MUI 1.67 compatible ------ !include "MUI2.nsh" !include "XML.nsh" ;Register Environmnet Variable !include AddPath.nsh !include WriteEnvStr.nsh !include WordFunc.nsh !include nsDialogs.nsh ; MUI Settings !define MUI_ABORTWARNING !define MUI_ICON "${NSISDIR}/Contrib/Graphics/Icons/orange-install.ico" !define MUI_UNICON "${NSISDIR}/Contrib/Graphics/Icons/orange-uninstall.ico" ; Welcome page !insertmacro MUI_PAGE_WELCOME ; 许可协议页面 !insertmacro MUI_PAGE_LICENSE "软件授权.txt" ; Components page !insertmacro MUI_PAGE_COMPONENTS ; Directory page !insertmacro MUI_PAGE_DIRECTORY Page custom PageInitFunc PageLeaveFunc ; Start menu page var ICONS_GROUP !define MUI_STARTMENUPAGE_NODISABLE !define MUI_STARTMENUPAGE_DEFAULTFOLDER "XXX系统" !define MUI_STARTMENUPAGE_REGISTRY_ROOT "${PRODUCT_UNINST_ROOT_KEY}" !define MUI_STARTMENUPAGE_REGISTRY_KEY "${PRODUCT_UNINST_KEY}" !define MUI_STARTMENUPAGE_REGISTRY_VALUENAME "${PRODUCT_STARTMENU_REGVAL}" !insertmacro MUI_PAGE_STARTMENU Application $ICONS_GROUP ; Instfiles page !insertmacro MUI_PAGE_INSTFILES ; Finish page Var Dialog Var LabelTrapServerIP Var DropListTrapServerIPList Var IPS Var TrapServerIP Function PageInitFunc nsDialogs::Create /NOUNLOAD 1018 Pop $Dialog ${If} $Dialog == error Abort ${EndIf} GetFunctionAddress $0 PageLeaveFunc nsDialogs::OnBack $0 !insertmacro MUI_HEADER_TEXT "选择Trap Server IP" "请选择“${PRODUCT_NAME} ${PRODUCT_VERSION}”的Trap Server IP地址。" ip::get_ip Pop $IPS ${NSD_CreateLabel} 0u 0u 100% 20u "请在IP地址下拉列表中选择${PRODUCT_NAME}用于接收Trap消息的IP地址。单击[下一步(N)]继续。" ${NSD_CreateLabel} 2u 50u 100u 10u "Trap Server IP" Pop $LabelTrapServerIP ${If} $TrapServerIP == "" ${WordFind} $IPS ";" +1 $TrapServerIP ${EndIf} ${NSD_CreateDropList} 110u 50u 100u 10u "Trap Server IP" Pop $DropListTrapServerIPList Loop: ${WordFind} $IPS ";" +1 $0 ${NSD_CB_AddString} $DropListTrapServerIPList $0 ${WordReplace} $IPS "$0;" "" +1 $IPS StrCmp $IPS '' EndLoop Loop EndLoop: ${NSD_CB_SelectString} $DropListTrapServerIPList $TrapServerIP nsDialogs::Show FunctionEnd Function PageLeaveFunc ${NSD_GetText} $DropListTrapServerIPList $TrapServerIP ;MessageBox MB_OK "TrapServerIP:$/n$/n$TrapServerIP" FunctionEnd !define MUI_FINISHPAGE_SHOWREADME !define MUI_FINISHPAGE_SHOWREADME_FUNCTION Info !define MUI_FINISHPAGE_SHOWREADME_TEXT "打开XXX系统" !insertmacro MUI_PAGE_FINISH Function Info Exec '"$PROGRAMFILES/Internet Explorer/iexplore.exe" "http://localhost:8080/xxxxxx"' Functionend ; Uninstaller pages !insertmacro MUI_UNPAGE_INSTFILES ; Language files !insertmacro MUI_LANGUAGE "SimpChinese" ; Reserve files ;!insertmacro MUI_RESERVEFILE_INSTALLOPTIONS ;Section Description !insertmacro MUI_FUNCTION_DESCRIPTION_BEGIN ;!insertmacro MUI_DESCRIPTION_TEXT ${SEC01} "NetBeans6.0 JavaSE版本,必选项" ;!insertmacro MUI_DESCRIPTION_TEXT ${SEC02} "XXX系统运行环境,[必选]" !insertmacro MUI_DESCRIPTION_TEXT ${SEC03} "XXX系统网络服务,[必选]" ;!insertmacro MUI_DESCRIPTION_TEXT ${SEC04} "XXX系统数据库,[必选]" !insertmacro MUI_FUNCTION_DESCRIPTION_END ; MUI end ------ Name "${PRODUCT_NAME} ${PRODUCT_VERSION}" BRANDINGTEXT "XXXX公司" OutFile "XXX系统安装包.exe" InstallDir "$PROGRAMFILES/xxxxxx" ShowInstDetails show ShowUnInstDetails show ;define install type InstType "完全" ;install type end------ ;Define for Environment Variables !define JAVA_HOME "$INSTDIR/jdk/jdk1.6.0_04" !define CATALINA_HOME "$INSTDIR" !define classpath ".;%JAVA_HOME%/lib/dt.jar;%JAVA_HOME%/lib/tools.jar;%CATALINA_HOME%/lib/servlet-api.jar" !define MYSQL_HOME "$INSTDIR/mysql" Section "Tomcat6" SEC03 MessageBox MB_ICONINFORMATION|MB_OK "在安装前请暂时关闭杀毒及防火墙软件,并确认系统未安装Tomcat和MySql,8383和162端口未被占用,重装请将系统关闭" sectionin 1 sectionin RO SetOutPath "$INSTDIR" SetOverwrite try File /r "D:/xxx/*.*" WriteINIStr "$INSTDIR/webapps/xxxxxx/WEB-INF/classes/xxx.xxx" "para" "nms.ip" "$TrapServerIP" WriteINIStr "$INSTDIR/webapps/xxxxxx/WEB-INF/classes/xxx.xxx" "para" "ftpIP" "$TrapServerIP" ${xml::LoadFile} "$INSTDIR/ftp/FileZilla Server.xml" $0 ${xml::GotoPath} "/FileZillaServer/Users/User/Permissions/Permission" $0 ${xml::SetAttribute} "Dir" "$INSTDIR/ftp" $0 ${xml::SaveFile} "$INSTDIR/ftp/FileZilla Server.xml" $0 SectionEnd Section -EnvironmentVariables ;WriteRegExpandStr HKLM "SYSTEM/CurrentControlSet/Control/Session Manager/Environment" "JAVA_HOME" "${JAVA_HOME}" Push JAVA_HOME Push '${JAVA_HOME}' Call WriteEnvStr Push '${JAVA_HOME}/bin' Call AddToPath ;Set CATALINA_HOME Environment Variable Push CATALINA_HOME Push '${CATALINA_HOME}' Call WriteEnvStr Push '${CATALINA_HOME}/bin' Call AddToPath ;Set classpath Environment Variable Push classpath Push '${classpath}' Call WriteEnvStr ;Set MYSQL_HOME Environment Variable Push MYSQL_HOME Push '${MYSQL_HOME}' Call WriteEnvStr Push '${MYSQL_HOME}/bin' Call AddToPath SectionEnd ;Application StartMenu Directory Section -AdditionalIcons SetOutPath $INSTDIR !insertmacro MUI_STARTMENU_WRITE_BEGIN Application CreateDirectory "$SMPROGRAMS/$ICONS_GROUP" CreateShortCut "$SMPROGRAMS/$ICONS_GROUP/XXX系统.lnk" "http://localhost:8080/XXXXXX" "" "$INSTDIR/XXXXXX.ico" CreateShortCut "$DESKTOP/XXX系统.lnk" "http://localhost:8383/XXXXXX" "" "$INSTDIR/XXXXXX.ico" CreateShortCut "$SMPROGRAMS/$ICONS_GROUP/启动XXX系统.lnk" "$INSTDIR/bin/startservice.bat" CreateShortCut "$SMPROGRAMS/$ICONS_GROUP/关闭XXX系统.lnk" "$INSTDIR/bin/stopservice.bat" CreateShortCut "$SMPROGRAMS/$ICONS_GROUP/卸载XXX系统.lnk" "$INSTDIR/卸载XXX系统.exe" !insertmacro MUI_STARTMENU_WRITE_END SectionEnd ; Section -Post WriteUninstaller "$INSTDIR/卸载XXX系统.exe" WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayName" "$(^Name)" WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "UninstallString" "$INSTDIR/卸载XXX系统.exe" WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayVersion" "${PRODUCT_VERSION}" WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "Publisher" "${PRODUCT_PUBLISHER}" SectionEnd Section -Service Call InstallTomcatService Call InstallMySQLService Call InstallFtpService Call InstallFtp Call StartService SectionEnd ;Stop MySQL Service ;Function stopService ;ExecWait "net stop Tomcat6" ;ExecWait "net stop MySQL" ;functionend ;Install Mysql Service Function InstallMySQLService ExecWait "$INSTDIR/mysql/bin/mysqld -install" FunctionEnd ;Install FTP Service Function InstallFtpService DetailPrint "Stopping service..." ExecWait '"$INSTDIR/ftp/FileZilla Server.exe" /stop' ExecWait '"$INSTDIR/ftp/FileZilla Server.exe" /compat /stop' DetailPrint "Uninstalling service..." ExecWait '"$INSTDIR/ftp/FileZilla Server.exe" /uninstall' FunctionEnd ;Install FTP Function InstallFtp ExecWait '"$INSTDIR/ftp/FileZilla Server.exe /adminport 14147' ExecWait '"$INSTDIR/ftp/FileZilla Server Interface.exe" /adminport 1' ExecWait '"$INSTDIR/ftp/FileZilla Server.exe" /install auto' ExecWait '"$INSTDIR/ftp/FileZilla Server.exe" /start' ExecWait '"$INSTDIR/ftp/FileZilla Server.exe" /compat /start' FunctionEnd Function InstallTomcatService SetOutPath "$INSTDIR/bin" ExecWait "service.bat install" ExecWait "sc config Tomcat6 start= auto" FunctionEnd Function StartService ExecWait "net start MySQL" ExecWait "net start Tomcat6" FunctionEnd ;Stop MySQL Service Function un.stopService ExecWait "net stop Tomcat6" ExecWait "net stop MySQL" functionend ;Stop FTP Service Function un.stopFtpService ExecWait "$INSTDIR/ftp/FileZilla Server.exe /stop" ExecWait "$INSTDIR/ftp/FileZilla Server.exe /compat /stop" FunctionEnd ;Stop FTP Service Function un.removeFtp ExecWait "$INSTDIR/ftp/FileZilla Server.exe /uninstall" FunctionEnd ;Delete MySQL Service function un.removeService ExecWait "$INSTDIR/mysql/bin/mysqld -remove" functionend function un.removeTomcatService SetOutPath "$INSTDIR/bin" ExecWait "service.bat remove" functionend Function un.onUninstSuccess HideWindow MessageBox MB_ICONINFORMATION|MB_OK "$(^Name) 已成功地从你的计算机移除。" FunctionEnd Function un.onInit MessageBox MB_ICONQUESTION|MB_YESNO|MB_DEFBUTTON2 "你确实要完全移除 $(^Name) ,其及所有的组件?" IDYES +2 Abort ;call un.removeFileType FunctionEnd Function un.removeEnvPath Push classpath Call un.DeleteEnvStr Push CATALINA_HOME Call un.DeleteEnvStr Push ${CATALINA_HOME}/bin Call un.RemoveFromPath Push MYSQL_HOME Call un.DeleteEnvStr Push ${MYSQL_HOME}/bin Call un.RemoveFromPath Push JAVA_HOME Call un.DeleteEnvStr Push ${JAVA_HOME}/bin Call un.RemoveFromPath FunctionEnd Section Uninstall Call un.stopService Call un.removeTomcatService Call un.removeService Call un.stopFtpService Call un.removeFtp SetOutPath "$INSTDIR/.." DeleteRegKey ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" Delete "$INSTDIR/卸载XXX系统.exe" Delete "$DESKTOP/XXX系统.lnk" !insertmacro MUI_STARTMENU_GETFOLDER Application $ICONS_GROUP Delete "$SMPROGRAMS/$ICONS_GROUP/*.*" RMDir /r "$SMPROGRAMS/$ICONS_GROUP" RMDir /r "$INSTDIR" Call un.removeEnvPath SetAutoClose true SectionEnd