一. 准备工作
下载nsis相关工具包,点击此下载
1. 安装程序:nsis-2.46-setup.exe
2. 编辑程序:cnisedit203.exe
3. 帮助文档:NSIS205帮助文档.rar
4. 第三方库:ExecCmd.dll、AddPath.nsh、WriteEnvStr.nsh
二. 安装nsis-2.4.6-setup.exe
1. 安装完后,右键nsi格式的文件,如果没有出现Compile NSIS Script和Compile NSIS Script(Choose Compressor)两个选项,
可尝试右键nsi格式文件---属性---常规---更改打开方式为记事本。
2. 将ExecCmd.dll复制到"NSIS安装路径\Plugins"下
三. 制作nsi打包脚本:ims1.0.0-jdk1.6.0_21&tomcat6.0.33.nsi
; Script generated by the HM NIS Edit Script Wizard. ;------------------------------常量定义------------------------------- ;定义产品名称常量
!define PRODUCT_NAME "企业信息管理系统" ;定义产品版本常量
!define PRODUCT_VERSION "1.0.0" ;定义发布公司名称常量
!define PRODUCT_PUBLISHER "ims" ;定义产品发布公司网址常量
!define PRODUCT_WEB_SITE "http://www.ims.com" ;定义反安装键值常量
!define PRODUCT_UNINST_KEY "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}" ;定义反安装ROOT键值常量
!define PRODUCT_UNINST_ROOT_KEY "HKLM" ;定义开机自动启动ROOT键值常量
!define PRODUCT_AUTORUN_ROOT_KEY "HKCU" ;定义开机自动启动键值常量
!define PRODUCT_AUTORUN_KEY "Software\Microsoft\Windows\CurrentVersion\Run" ;定义系统访问地址
!define URL "http://localhost:8080/ims/index.html" ;应用路径常量
!define APP_CATALOG "app" ;资源路径常量
!define RESOURCE_CATALOG "resource" ;目标路径常量
!define TARGET_CATALOG "target" ;定义JDK资源存放路径常量
!define JDK_RESOURCE_CATALOG "java\jdk1.6.0_21" ;定义TOMCAT的资源存放路径常量
!define TOMCAT_RESOURCE_CATALOG "container\apache-tomcat-6.0.33" ;定义MYSQL的资源存放路径常量
!define MYSQL_RESOURCE_CATALOG "db\mysql-server-5.1" ;定义数据存放路径常量
!define DATA_RESOURCE_CATALOG "data" ;定义图标存放路径常量
!define ICON_RESOURCE_CATALOG "icon" ;定义JAVA_HOME常量常量
!define JAVA_HOME "$INSTDIR\${JDK_RESOURCE_CATALOG}" ;定义CATALINA_HOME常量常量
!define CATALINA_HOME "$INSTDIR\${TOMCAT_RESOURCE_CATALOG}" ;定义MYSQL_HOME常量常量
!define MYSQL_HOME "$INSTDIR\${MYSQL_RESOURCE_CATALOG}" ;------------------------------第三方库导入------------------------------- ; MUI 1.67 compatible ------
!include "MUI.nsh" ;导入打包程序第三方库
!include "third\AddPath.nsh"
!include "third\WriteEnvStr.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" ;------------------------------安装步骤向导界面定义------------------------------- ;欢迎页面
!insertmacro MUI_PAGE_WELCOME ;授权页面
!insertmacro MUI_PAGE_LICENSE "..\${RESOURCE_CATALOG}\licence.txt" ;目录选择页面
!insertmacro MUI_PAGE_DIRECTORY ;安装页面
!insertmacro MUI_PAGE_INSTFILES ;完成页面
!insertmacro MUI_PAGE_FINISH ;反安装页面
!insertmacro MUI_UNPAGE_INSTFILES ;语言选择
!insertmacro MUI_LANGUAGE "SimpChinese" ;------------------------------打包后安装文件名/输出路径/安装路径定义------------------------------- ;产品名称及产品版本
Name "${PRODUCT_NAME} ${PRODUCT_VERSION}" ;输出文件名称
OutFile "..\${TARGET_CATALOG}\${PRODUCT_NAME}-${PRODUCT_VERSION}.exe" ;程序安装路径
InstallDir "$PROGRAMFILES\ims1.0.0" ;显示安装进度
ShowInstDetails show ;显示反安装进度
ShowUnInstDetails show ;------------------------------提示对话框定义------------------------------- ;安装前提示
Function .onInit
MessageBox MB_YESNO "请先确认该电脑上没有安装JDK、Apache Tomcat、Mysql软件。是否继续?" IDYES NoAbort
Abort
NoAbort:
FunctionEnd ;安装失败提示
Function .onInstFailed
MessageBox MB_OK "安装失败,请检查您的系统环境。"
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
FunctionEnd ;------------------------------菜单以及注册表信息注册------------------------------- ;菜单注册
Section -AdditionalIcons
SetOutPath $INSTDIR
;WriteIniStr "$INSTDIR\${PRODUCT_NAME}.url" "InternetShortcut" "URL" "${PRODUCT_WEB_SITE}"
CreateDirectory "$SMPROGRAMS\${PRODUCT_NAME}"
CreateShortCut "$SMPROGRAMS\${PRODUCT_NAME}\${PRODUCT_NAME}.lnk" "${URL}"
CreateShortCut "$SMPROGRAMS\${PRODUCT_NAME}\Uninstall.lnk" "$INSTDIR\uninst.exe"
;CreateShortCut "$DESKTOP\${PRODUCT_NAME}.lnk" "${URL}" "" "$INSTDIR\icon\ims.ico"
SectionEnd ;注册表注册
Section -Post
WriteRegStr ${PRODUCT_AUTORUN_ROOT_KEY} "${PRODUCT_AUTORUN_KEY}" "${PRODUCT_NAME}" "${CATALINA_HOME}\bin\runbat.vbs"
WriteUninstaller "$INSTDIR\uninst.exe"
WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayName" "$(^Name)"
WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "UninstallString" "$INSTDIR\uninst.exe"
WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayVersion" "${PRODUCT_VERSION}"
WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "URLInfoAbout" "${PRODUCT_WEB_SITE}"
WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "Publisher" "${PRODUCT_PUBLISHER}"
SectionEnd ;------------------------------资源复制打包------------------------------- ;将JDK_RESOURCE_CATALOG下的所有文件资源拷贝至安装目录下
Section "JDK_RESOURCE_COPY" SEC01
sectionin 1
sectionin RO
SetOutPath "$INSTDIR\${JDK_RESOURCE_CATALOG}"
SetOverwrite try
File /r "..\${RESOURCE_CATALOG}\${JDK_RESOURCE_CATALOG}\*.*"
SectionEnd ;将TOMCAT_RESOURCE_CATALOG下的所有文件资源拷贝至安装目录下
Section "TOMCAT_RESOURCE_COPY" SEC02
sectionin 1
sectionin RO
SetOutPath "$INSTDIR\${TOMCAT_RESOURCE_CATALOG}"
SetOverwrite try
File /r "..\${RESOURCE_CATALOG}\${TOMCAT_RESOURCE_CATALOG}\*.*"
SectionEnd ;将MYSQL_RESOURCE_CATALOG下的所有文件资源拷贝至安装目录下
Section "MYSQL_RESOURCE_COPY" SEC03
sectionin 1
sectionin RO
SetOutPath "$INSTDIR\${MYSQL_RESOURCE_CATALOG}"
SetOverwrite try
File /r "..\${RESOURCE_CATALOG}\${MYSQL_RESOURCE_CATALOG}\*.*" ;设置初始化SQL文件路径
WriteINIStr "${MYSQL_HOME}\my.ini" mysqld "init-file" '"${MYSQL_HOME}\init.sql"'
WriteINIStr "${MYSQL_HOME}\my.ini" mysqld "basedir" '"${MYSQL_HOME}"'
WriteINIStr "${MYSQL_HOME}\my.ini" mysqld "datadir" '"$INSTDIR\${DATA_RESOURCE_CATALOG}\mysql"'
FlushINI "${MYSQL_HOME}\my.ini"
SectionEnd ;将DATA_RESOURCE_COPY下的所有文件资源拷贝至安装目录下
Section "DATA_RESOURCE_COPY" SEC04
sectionin 1
sectionin RO
SetOutPath "$INSTDIR\${DATA_RESOURCE_CATALOG}"
SetOverwrite try
File /r "..\${DATA_RESOURCE_CATALOG}\*.*"
SectionEnd ;将ICON_RESOURCE_COPY下的所有文件资源拷贝至安装目录下
Section "ICON_RESOURCE_COPY" SEC06
sectionin 1
sectionin RO
SetOutPath "$INSTDIR\${ICON_RESOURCE_CATALOG}"
SetOverwrite try
File /r "..\${RESOURCE_CATALOG}\${ICON_RESOURCE_CATALOG}\*.*"
CreateShortCut "$DESKTOP\${PRODUCT_NAME}.lnk" "${URL}" "" "$INSTDIR\icon\ims.ico"
SectionEnd ;将APP_RESOURCE_COPY下的所有文件资源拷贝至安装目录下
Section "APP_RESOURCE_COPY" SEC07
sectionin 1
sectionin RO
SetOutPath "$INSTDIR\${TOMCAT_RESOURCE_CATALOG}\webapps"
SetOverwrite try
File /r "..\${APP_CATALOG}\*.*"
SectionEnd ;------------------------------环境变量注册及反注册------------------------------- ;注册环境变量
Section -EnvironmentVariables
;JDK安装根路径
Push JAVA_HOME
Push '${JAVA_HOME}'
Call WriteEnvStr
Push '${JAVA_HOME}\bin'
Call AddToPath ;TOMCAT安装根路径
Push CATALINA_HOME
Push '${CATALINA_HOME}'
Call WriteEnvStr
Push '${CATALINA_HOME}\bin'
Call AddToPath ;MySQL安装根路径
Push MYSQL_HOME
Push '${MYSQL_HOME}'
Call WriteEnvStr
Push '${MYSQL_HOME}\bin'
Call AddToPath
SectionEnd ;反注册环境变量
Function un.removeEnvPath
Push JAVA_HOME
Call un.DeleteEnvStr
Push '${JAVA_HOME}\bin'
Call un.RemoveFromPath 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
FunctionEnd ;------------------------------服务的安装/启动/停止/反安装------------------------------- ;安装并启动后台服务
Section -Service
Call InstallMySQLService
Call InstallTomcatService Call startMySQLService
Call startTomcatProcess
SectionEnd ;安装TOMCAT服务
Function InstallTomcatService
SetOutPath "$INSTDIR\${TOMCAT_RESOURCE_CATALOG}\bin"
ExecCmd::exec /TIMEOUT=10000 "service.bat install"
FunctionEnd ;安装MYSQL服务
Function InstallMySQLService
ExecWait "$INSTDIR\${MYSQL_RESOURCE_CATALOG}\bin\mysqld -install"
FunctionEnd ;启动TOMCAT进程
Function startTomcatProcess
SetOutPath "${CATALINA_HOME}\bin"
nsExec::Exec /TIMEOUT=120000 '"$SYSDIR\cscript.exe" "${CATALINA_HOME}\bin\runbat.vbs"'
FunctionEnd ;启动MySQL服务
Function startMySQLService
ExecCmd::exec /TIMEOUT=60000 "net start MySQL"
Sleep 1500
SetOutPath "$INSTDIR\${MYSQL_RESOURCE_CATALOG}\bin"
Sleep 1500
ExecCmd::exec /TIMEOUT=60000 "mysql.exe -uroot -p123456 <ims.sql"
FunctionEnd ;停止TOMCAT服务
Function un.stopTomcatService
ExecCmd::exec /TIMEOUT=120000 "net stop Tomcat6"
FunctionEnd ;停止MySQL服务
Function un.stopMySQLService
ExecCmd::exec /TIMEOUT=20000 "net stop MySQL"
FunctionEnd ;删除TOMCAT服务
function un.removeTomcatService
SetOutPath "$INSTDIR\${TOMCAT_RESOURCE_CATALOG}\bin"
ExecCmd::exec /TIMEOUT=120000 "service.bat remove"
functionend ;删除MySQL服务
function un.removeMySQLService
ExecWait "$INSTDIR\${MYSQL_RESOURCE_CATALOG}\bin\mysqld -remove"
functionend ;------------------------------反安装------------------------------- Section Uninstall
ExecCmd::exec /TIMEOUT=120000 "taskkill /im tomcat6.exe /f" Call un.stopTomcatService
Call un.stopMySQLService Call un.removeTomcatService
Call un.removeMySQLService Delete "$DESKTOP\${PRODUCT_NAME}.lnk"
Delete "$SMPROGRAMS\${PRODUCT_NAME}\Uninstall.lnk"
Delete "$SMPROGRAMS\${PRODUCT_NAME}\${PRODUCT_NAME}.lnk"
RMDir "$SMPROGRAMS\${PRODUCT_NAME}" Delete "$INSTDIR\${PRODUCT_NAME}.url"
Delete "$INSTDIR\uninst.exe"
RMDir /r "$INSTDIR" DeleteRegKey ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}"
DeleteRegValue ${PRODUCT_AUTORUN_ROOT_KEY} "${PRODUCT_AUTORUN_KEY}" "${PRODUCT_NAME}" Call un.removeEnvPath
SetAutoClose true
SectionEnd
四. 编译前准备
1. 新建resource、target、script三个文件夹
2. 将jdk1.6.0_21、tomcat6.0.33放置到resource文件夹下
3. 创建licence.txt文件,放置到resource文件夹下
4. 将ims1.0.0-jdk1.6.0_21&tomcat6.0.33.nsi放置到script文件夹下
5. 在script下新建third文件夹,将AddPath.nsh、WriteEnvStr.nsh放置到该文件夹下
五. 编译nsi脚本
右键nsi脚本,选择Compile NSIS Script进行编译,最终在target目录下生成exe安装包。