基於jenkins一鍵打包生成軟件exe

 梳理一下基於jenkins一鍵打包生成軟件exe:

 

Prerequisite:

將需要用到的文件上傳到svn指定位置,以便於隨時可以checkout使用。這裏把以下文件import到svn的路徑:https://192.168.xxx.xxx/svn/APL/trunk/products/system/setup/server

apache-tomcat-6.0.51

jdk1.6.0_43

mysql-5.7.17-win32

Xxxxxx.ico

install.bat

uninstall.bat

platform.iss

vcredist_x64.exe

vcredist2010_x64_XiaZaiBa.exe

也可以把這些文件放到服務器的某個位置,比如D:\setup

 

1.      Createa new job on Jenkins

SVNRepository URL:

https://192.168.1.9/svn/APL/trunk/products/system

 

Buildcommand:

setup\server\Jenkins_Build.bat

 

注意事項:

1)      腳本的關鍵其實是最後一步,調用Inno Setup工具進行封裝,但前提是要先在本機安裝Inno setup軟件,platform.iss/install.bat/uninstall.bat都通過SVN上export。

2)      Platform.iss是Inno setup的主運行程序,分析腳本內容如下:

OutputDir=E:\setup\setup

這裏,OutputDir是生成的exe軟件包的位置,本例中,是我本機的E盤

OutputBaseFilename=mysetup

這裏,OutputBaseFilename是生成的exe軟件包的名字,本例中,軟件包名字爲mysetup.exe。

Source:"E:\setup\jdk1.6.0_43\*"; DestDir: "C:\Program Files(x86)\jdk1.6.0_43"; Flags: ignoreversion recursesubdirs createallsubdirs

其中,Source路徑是本機存放的需要封裝的文件的路徑,DestDir路徑是封裝成exe後再運行時的對象機中的路徑,本例中,Source是我本機的E盤,DestDir是192.168.1.9服務器的C盤。

3)      以下五個文件都會放到{app}路徑下,本例中是192.168.1.9服務器的C:\ Program Files (x86)\AplIot下。

Source:"E:\setup\install.bat"; DestDir: "{app}"; Flags:ignoreversion

Source:"E:\setup\uninstall.bat"; DestDir: "{app}"; Flags:ignoreversion

Source:"E:\setup\AgriGarden.ico"; DestDir: "{app}"; Flags:ignoreversion

Source:"E:\setup\vcredist_x64.exe"; DestDir: "{app}"; Flags: ignoreversion

Source:"E:\setup\vcredist2010_x64_XiaZaiBa.exe"; DestDir: "{app}";Flags: ignoreversion

4)      這裏需要注意的是,vcredist_x64.exe和vcredist2010_x64_XiaZaiBa.exe是可執行文件,通過Platform.iss可以自啓動可執行文件,除了在[Files]裏增加Source和DestDir之外,還需要在[Run]裏添加Filename和路徑。

[Files]

Source: "E:\setup\vcredist_x64.exe";DestDir: "{app}"; Flags: ignoreversion

Source:"E:\setup\vcredist2010_x64_XiaZaiBa.exe"; DestDir: "{app}";Flags: ignoreversion

 

[Run]

Filename:"{app}\vcredist_x64.exe";Description:"正在安裝vcredist_x64.exe";

Filename:"{app}\vcredist2010_x64_XiaZaiBa.exe";Description:"正在安裝vcredist2010_x64_XiaZaiBa.exe";

5)      在[Run]裏會調用啓動install.bat,在[UninstallRun]裏會調用啓動uninstall.bat

[Run]

Filename:"{app}\install.bat";Description:"正在啓動或配置相關程序";

[UninstallRun]

Filename:"{app}\uninstall.bat";

 

2.      Verifythe job

Job成功完成後,去Platform.iss腳本里的OutputDir路徑下取exe文件,本例中是在E:\setup\setup中會生成mysetup.exe。

在本地的虛擬機中進行驗證:

1.      新建虛擬機

2.      控制面板->卸載程序->卸載和VC++Redistributable相關的程序

3.      將本機中的E:\setup\setup\mysetup.exe粘貼到虛擬機桌面,雙擊運行。

4.      運行成功後,登陸http://localhost:8080/AplIot/webSite-iot/html/login.html (選擇Chrome瀏覽器或者360瀏覽器)查看AplIot網頁是否可以正常運行。


附源碼:

; Script generated by the Inno Setup Script Wizard.
; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES!

#define MyAppName "AplIot" 
#define MyAppVersion "1.0" 
#define MyAppPublisher "AgriGarden" 
#define MyAppURL "http://www.agri-garden.com/" 
#define MyAppExeName "MyProg.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={{FC6B755E-7269-44BD-ADD1-3E641794BDA9}
AppName={#MyAppName}
AppVersion={#MyAppVersion}
;AppVerName={#MyAppName} {#MyAppVersion}
AppPublisher={#MyAppPublisher}
AppPublisherURL={#MyAppURL}
AppSupportURL={#MyAppURL}
AppUpdatesURL={#MyAppURL}
DefaultDirName={pf}\{#MyAppName}
DisableProgramGroupPage=yes
OutputDir=E:\setup\setup
OutputBaseFilename=mysetup
Compression=lzma
SolidCompression=yes
SetupIconFile=AgriGarden.ico
[Languages]
Name: "english"; MessagesFile: "compiler:Default.isl"

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

[Files]
Source: "E:\setup\apache-tomcat-6.0.51\*"; DestDir: "C:\Program Files (x86)\apache-tomcat-6.0.51"; Flags: ignoreversion recursesubdirs createallsubdirs
Source: "E:\setup\jdk1.6.0_43\*"; DestDir: "C:\Program Files (x86)\jdk1.6.0_43"; Flags: ignoreversion recursesubdirs createallsubdirs
Source: "E:\setup\mysql-5.7.17-win32\*"; DestDir: "C:\Program Files (x86)\mysql-5.7.17-win32"; Flags: ignoreversion recursesubdirs createallsubdirs
Source: "E:\setup\install.bat"; DestDir: "{app}"; Flags: ignoreversion
Source: "E:\setup\uninstall.bat"; DestDir: "{app}"; Flags: ignoreversion
;Source: "D:\setup\項目啓動.html"; DestDir: "{app}"; Flags: ignoreversion
Source: "E:\setup\AgriGarden.ico"; DestDir: "{app}"; Flags: ignoreversion
Source: "E:\setup\vcredist_x64.exe"; DestDir: "{app}"; Flags: ignoreversion
Source: "E:\setup\vcredist2010_x64_XiaZaiBa.exe"; DestDir: "{app}"; Flags: ignoreversion
; 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

[Run]
Filename:"{app}\vcredist_x64.exe";Description:"正在安裝vcredist_x64.exe";
Filename:"{app}\vcredist2010_x64_XiaZaiBa.exe";Description:"正在安裝vcredist2010_x64_XiaZaiBa.exe";
Filename:"{app}\install.bat";Description:"正在啓動或配置相關程序";

[UninstallRun]
Filename:"{app}\uninstall.bat";
[UninstallDelete]
Type:filesandordirs;Name:"C:\Program Files (x86)\apache-tomcat-6.0.51" 
Type:filesandordirs;Name:"C:\Program Files (x86)\mysql-5.7.17-win32"
@echo off 
title 運維繫統正在安裝中,請不要手動關閉
color 0a

echo compil32 環境變量
set InnoSetup=C:\Program Files (x86)\Inno Setup 5\

echo jdk 環境變量
set jdkpath=C:\Program Files (x86)\jdk1.6.0_43

::setx variable value -m
:: setx 這個是用來設置環境變量的,並且會寫入註冊表. variable,會被覆蓋,此操作不可逆的
:: variable 代表鍵 value代表值 -m代表寫入系統環境變量,如果沒有該參數會寫入當前用戶環境變量.

setx JAVA_HOME "%jdkpath%" -m

setx CLASSPATH ".:%%JAVA_HOME%%\lib\tools.jar;%%JAVA_HOME%%\lib\dt.jar" -m

::追加path環境變量  find 後面/i代表忽略大小寫   &&執行成功執行的命令  ||執行失敗執行的命令
echo %Path%|find /i "%JAVA_HOME%" && set IsNull=true || set IsNull=false
echo %IsNull%
if not %IsNull%==true (
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path /t REG_SZ /d "%Path%;%%JAVA_HOME%%\bin;%%JAVA_HOME%%\jre\bin" /f

::設置當前用戶環境變量
setx Path "%%JAVA_HOME%%\bin;%Path%;%InnoSetup%"
)

echo mysql 環境變量
set mysqlpath=C:\Program Files (x86)\mysql-5.7.17-win32

::setx variable value -m
:: setx 這個是用來設置環境變量的,並且會寫入註冊表. variable,會被覆蓋,此操作不可逆的
:: variable 代表鍵 value代表值 -m代表寫入系統環境變量,如果沒有該參數會寫入當前用戶環境變量.

setx MYSQL_HOME "%mysqlpath%" -m

::追加path環境變量  find 後面/i代表忽略大小寫   &&執行成功執行的命令  ||執行失敗執行的命令
echo %Path%|find /i "%MYSQL_HOME%" && set IsNull=true || set IsNull=false
echo %IsNull%
if not %IsNull%==true (
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path /t REG_SZ /d "%Path%;%%MYSQL_HOME%%\bin" /f

::設置當前用戶環境變量
setx Path "%%JAVA_HOME%%\bin;%%MYSQL_HOME%%\bin;%Path%" 
)

echo tomcat 環境變量
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v CATALINA_HOME /t REG_EXPAND_SZ /d "C:\Program Files (x86)\apache-tomcat-6.0.51" /f

echo mysql 環境變量
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v MYSQL_HOME /t REG_EXPAND_SZ /d "C:\Program Files (x86)\mysql-5.7.17-win32" /f

::echo 安裝vcredist_x64.exe
::start /wait .\vcredist_x64.exe /verysilent sp-

::echo 安裝vcredist2010_x64_XiaZaiBa.exe
::start /wait .\vcredist2010_x64_XiaZaiBa.exe /verysilent sp-

echo 啓動mysql並更新密碼
"C:\Program Files (x86)\mysql-5.7.17-win32\bin\mysqld.exe" --install

net start mysql
::設置mysql服務自啓動
sc config mysql start=auto
"C:\Program Files (x86)\mysql-5.7.17-win32\bin\mysql.exe" -u root -e "update mysql.user set authentication_string=password('root') where user='root' and Host = 'localhost'; "

net stop mysql
net start mysql
echo mysql啓動完畢

echo 啓動tomcat
set JAVA_HOME=C:\Program Files (x86)\jdk1.6.0_43
set CATALINA_HOME=C:\Program Files (x86)\apache-tomcat-6.0.51
set CATALINA_BASE=C:\Program Files (x86)\apache-tomcat-6.0.51

cd /d C:\Program Files (x86)\apache-tomcat-6.0.51\bin

::安裝tomcat到服務中
call service.bat install tomcat

::設置tomcat服務自啓動
sc config tomcat start=auto
net start tomcat
exit
@echo off
title abc系統正在卸載中,請不要手動關閉
color 0a
echo 刪除環境變量
reg delete "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v JAVA_HOME /f 
reg delete "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v CATALINA_HOME /f
reg delete "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v MYSQL_HOME /f
reg delete "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v CLASSPATH /f

setx Path ""

echo 停止並刪除tomcat
net stop tomcat
cd C:\Program Files (x86)\tomcat6\bin
cd ..\..\
call service.bat remove
sc delete tomcat
echo 停止並刪除mysql
net stop mysql
sc delete mysql
exit
@echo off
::創建文件夾APL用來打包
md APL
cd projects/server/AplIot

:: compile svn code
echo start compiling java source code ...
javac  -encoding UTF-8 -classpath .;WebRoot\WEB-INF\lib\* src\com\zhyd\apliot\common\*.java src\com\zhyd\apliot\controller\*.java  src\com\zhyd\apliot\dao\*.java src\com\zhyd\apliot\dao\impl\*.java src\com\zhyd\apliot\filter\*.java  src\com\zhyd\apliot\listener\*.java src\com\zhyd\apliot\modbus\dao\*.java src\com\zhyd\apliot\modbus\dao\impl\*.java src\com\zhyd\apliot\modbus\model\*.java  src\com\zhyd\apliot\model\*.java  src\com\zhyd\apliot\server\dao\*.java src\com\zhyd\apliot\server\dao\impl\*.java src\com\zhyd\apliot\task\*.java src\com\zhyd\apliot\test\*.java src\com\zhyd\apliot\util\*.java src\com\zhyd\apliot\xml\dao\*.java  src\com\zhyd\apliot\xml\dao\impl\*.java  -d WebRoot\WEB-INF\classes

:: 生成AplIot.war
cd WebRoot
jar -cvf AplIot.war .\*

@echo off
echo 當前CMD默認目錄:%cd%
cd ..\..\..\..

:: 將SVN上需要用的文件copy到本地APL文件夾
echo start copying ......
echo 當前CMD默認目錄:%cd%
xcopy /e/r/c "setup\server\*" "APL"

:: copy AplIot.war to apache-tomcat-6.0.51\webapps
echo move AplIot.war to tomcat\webapps ...
::move e:\jenkins\workspace\aaa\projects\server\AplIot\WebRoot\AplIot.war e:\jenkins\workspace\aaa\APL\apache-tomcat-6.0.51\webapps
echo 當前CMD默認目錄:%cd%
move %cd%\projects\server\AplIot\WebRoot\AplIot.war %cd%\APL\apache-tomcat-6.0.51\webapps

echo copying sql ...
xcopy /s "E:\setup\mysql-5.7.17-win32.rar" .

echo unzip sql.rar ...
start winrar x mysql-5.7.17-win32.rar

echo delete mysql-5.7.17-win32.rar
del /s /q mysql-5.7.17-win32.rar

:: 調用Inno Setup工具進行封裝,前提是要先在本機安裝Inno setup軟件
echo compiling platform.iss ... Inno Setup needs to be installed first ....
::compil32 /cc platform.iss
iscc platform.iss



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章