跳至主要內容

Windows 打包EXE部署

gavin-james开发随手记大约 6 分钟

Windows 打包EXE部署

打包内容: Java,MySQL,Springboot JAR + 第三方dll组件。

对于一些简单的WEB应用,使用诸如java,SQL,JAR/WAR期望将其统一打包成一个EXE安装包进行安装,并将其注册为Windows的服务。

2019-03-14 JWS在新版本中要收费,我去他大爷的。配置起来这么麻烦,远不及winsw.

准备

  • favicon.ico
  • JDK 免安装版本
  • MySQL免安装版本
  • Java Service Wrapper

工具

Inno Setup第一印象参考:

下载 Inno Setup Compile 5.6.

依据引导配置生成如下代码:

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

#define MyAppName "智能仓库管理系统"
#define MyAppVersion "0.1.5"
#define MyAppPublisher "康禾科技"
#define MyAppURL "https://localhost:8081/admin/"

[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={{038D6A71-180E-4CAB-AC9B-20DFFC791881}
AppName={#MyAppName}
AppVersion={#MyAppVersion}
;AppVerName={#MyAppName} {#MyAppVersion}
AppPublisher={#MyAppPublisher}
DefaultDirName=D:\khkj-store
DefaultGroupName={#MyAppName}
AllowNoIcons=yes
OutputDir=C:\Users\gavin\Desktop\khkj_resources
OutputBaseFilename=Setup
SetupIconFile=C:\Users\gavin\Desktop\khkj_resources\favicon.ico
Password=khkj
Compression=lzma
SolidCompression=yes

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

[Files]
Source: "C:\Users\gavin\Desktop\kjkj-store-install\*"; DestDir: "{app}"; Flags: ignoreversion recursesubdirs createallsubdirs
; NOTE: Don't use "Flags: ignoreversion" on any shared system files

[Icons]
Name: "{group}\{cm:UninstallProgram,{#MyAppName}}"; Filename: "{uninstallexe}"

[Run]
Filename: "{app}\install\install.bat";

[UninstallRun]
Filename:"{app}\uninstall\uninstall.bat";

JDK

JDK 的安装需要考虑

  • 下载JDK免安装版本
  • 环境变量 - JAVA_HOME, CLASS_PATH,PATH
  • JDK 的版本 - 特别要考虑jar和dll对版本的依赖
  • 将dll放置到jdk bin下,最好也放一份到jre下面

MySQL

MySQL 的安装需要考虑

SpringBoot生成的web app 的jar

有两种方式Java Services Wrapper和winsw

winsw

conf配置文件,xml 格式的 参考

官网

包下载

其它参考资料

JWS方式

参考

JWS下载

JWS官网

对于 JSW如果是用Springboot的JAR运行,特别要注意要使用springboot的jarlauncher类,而不是main class 所在的类,这是一个很大的坑 JSW配置参考如下:

#encoding=UTF-8
# Configuration files must begin with a line specifying the encoding
#  of the the file.
#
# NOTE - Please use src/conf/wrapper.conf.in as a template for your
#        own application rather than the values used for the
#        TestWrapper sample.

#********************************************************************
# Wrapper License Properties (Ignored by Community Edition)
#********************************************************************
# Professional and Standard Editions of the Wrapper require a valid
#  License Key to start.  Licenses can be purchased or a trial license
#  requested on the following pages:
# http://wrapper.tanukisoftware.com/purchase
# http://wrapper.tanukisoftware.com/trial

# Include file problems can be debugged by leaving only one '#'
#  at the beginning of the following line:
##include.debug

# The Wrapper will look for either of the following optional files for a
#  valid License Key.  License Key properties can optionally be included
#  directly in this configuration file.
#include ../conf/wrapper-license.conf
#include ../conf/wrapper-license-%WRAPPER_HOST_NAME%.conf

# The following property will output information about which License Key(s)
#  are being found, and can aid in resolving any licensing problems.
#wrapper.license.debug=TRUE

#********************************************************************
# Wrapper Localization
#********************************************************************
# Specify the language and locale which the Wrapper should use.
#wrapper.lang=en_US # en_US or ja_JP

# Specify the location of the language resource files (*.mo).
wrapper.lang.folder=..\lang

#********************************************************************
# Wrapper Java Properties
#********************************************************************
# Java Application
#  Locate the java binary on the system PATH:
wrapper.java.command=..\..\..\java\jdk_1.8_win32\bin\java
#  Specify a specific java binary:
#set.JAVA_HOME=/java/path
#wrapper.java.command=%JAVA_HOME%/bin/java

# Tell the Wrapper to log the full generated Java command line.
#wrapper.java.command.loglevel=INFO

# Java Main class.  This class must implement the WrapperListener interface
#  or guarantee that the WrapperManager class is initialized.  Helper
#  classes are provided to do this for you.
#  See the following page for details:
#  http://wrapper.tanukisoftware.com/doc/english/integrate.html
wrapper.java.mainclass=org.tanukisoftware.wrapper.WrapperSimpleApp

# Java Classpath (include wrapper.jar)  Add class path elements as
#  needed starting from 1
wrapper.java.classpath.1=..\lib\store.jar
wrapper.java.classpath.2=..\lib\wrapper.jar

# Java Library Path (location of Wrapper.DLL or libwrapper.so)
wrapper.java.library.path.1=..\lib\

# Java Bits.  On applicable platforms, tells the JVM to run in 32 or 64-bit mode.
wrapper.java.additional.auto_bits=FALSE

# Java Additional Parameters
wrapper.java.additional.1=

# Initial Java Heap Size (in MB)
#wrapper.java.initmemory=3

# Maximum Java Heap Size (in MB)
#wrapper.java.maxmemory=64

# Application parameters.  Add parameters as needed starting from 1
wrapper.app.parameter.1=org.springframework.boot.loader.JarLauncher

#********************************************************************
# Wrapper Logging Properties
#********************************************************************
# Enables Debug output from the Wrapper.
# wrapper.debug=TRUE

# Format of output for the console.  (See docs for formats)
wrapper.console.format=PM

# Log Level for console output.  (See docs for log levels)
wrapper.console.loglevel=INFO

# Log file to use for wrapper output logging.
wrapper.logfile=../logs/wrapper.log

# Format of output for the log file.  (See docs for formats)
wrapper.logfile.format=LPTM

# Log Level for log file output.  (See docs for log levels)
wrapper.logfile.loglevel=INFO

# Maximum size that the log file will be allowed to grow to before
#  the log is rolled. Size is specified in bytes.  The default value
#  of 0, disables log rolling.  May abbreviate with the 'k' (kb) or
#  'm' (mb) suffix.  For example: 10m = 10 megabytes.
wrapper.logfile.maxsize=0

# Maximum number of rolled log files which will be allowed before old
#  files are deleted.  The default value of 0 implies no limit.
wrapper.logfile.maxfiles=0

# Log Level for sys/event log output.  (See docs for log levels)
wrapper.syslog.loglevel=NONE

#********************************************************************
# Wrapper General Properties
#********************************************************************
# Allow for the use of non-contiguous numbered properties
wrapper.ignore_sequence_gaps=TRUE

# Do not start if the pid file already exists.
wrapper.pidfile.strict=TRUE

# Title to use when running as a console
wrapper.console.title=Test Wrapper Sample Application

#********************************************************************
# Wrapper JVM Checks
#********************************************************************
# Detect DeadLocked Threads in the JVM. (Requires Standard Edition)
wrapper.check.deadlock=TRUE
wrapper.check.deadlock.interval=10
wrapper.check.deadlock.action=RESTART
wrapper.check.deadlock.output=FULL

# Out Of Memory detection.
# (Ignore output from dumping the configuration to the console.  This is only needed by the TestWrapper sample application.)
wrapper.filter.trigger.999=wrapper.filter.trigger.*java.lang.OutOfMemoryError
wrapper.filter.allow_wildcards.999=TRUE
wrapper.filter.action.999=NONE
#  Ignore -verbose:class output to avoid false positives.
wrapper.filter.trigger.1000=[Loaded java.lang.OutOfMemoryError
wrapper.filter.action.1000=NONE
# (Simple match)
wrapper.filter.trigger.1001=java.lang.OutOfMemoryError
# (Only match text in stack traces if -XX:+PrintClassHistogram is being used.)
#wrapper.filter.trigger.1001=Exception in thread "*" java.lang.OutOfMemoryError
#wrapper.filter.allow_wildcards.1001=TRUE
wrapper.filter.action.1001=RESTART
wrapper.filter.message.1001=The JVM has run out of memory.

#********************************************************************
# Wrapper Email Notifications. (Requires Professional Edition)
#********************************************************************
# Common Event Email settings.
#wrapper.event.default.email.debug=TRUE
#wrapper.event.default.email.smtp.host=<SMTP_Host>
#wrapper.event.default.email.smtp.port=25
#wrapper.event.default.email.subject=[%WRAPPER_HOSTNAME%:%WRAPPER_NAME%:%WRAPPER_EVENT_NAME%] Event Notification
#wrapper.event.default.email.sender=<Sender email>
#wrapper.event.default.email.recipient=<Recipient email>

# Configure the log attached to event emails.
#wrapper.event.default.email.maillog=ATTACHMENT
#wrapper.event.default.email.maillog.lines=50
#wrapper.event.default.email.maillog.format=LPTM
#wrapper.event.default.email.maillog.loglevel=INFO

# Enable specific event emails.
#wrapper.event.wrapper_start.email=TRUE
#wrapper.event.jvm_prelaunch.email=TRUE
#wrapper.event.jvm_start.email=TRUE
#wrapper.event.jvm_started.email=TRUE
#wrapper.event.jvm_deadlock.email=TRUE
#wrapper.event.jvm_stop.email=TRUE
#wrapper.event.jvm_stopped.email=TRUE
#wrapper.event.jvm_restart.email=TRUE
#wrapper.event.jvm_failed_invocation.email=TRUE
#wrapper.event.jvm_max_failed_invocations.email=TRUE
#wrapper.event.jvm_kill.email=TRUE
#wrapper.event.jvm_killed.email=TRUE
#wrapper.event.jvm_unexpected_exit.email=TRUE
#wrapper.event.wrapper_stop.email=TRUE

# Specify custom mail content
wrapper.event.jvm_restart.email.body=The JVM was restarted.\n\nPlease check on its status.\n

#********************************************************************
# Wrapper Windows Service Properties
#********************************************************************
# WARNING - Do not modify any of these properties when an application
#  using this configuration file has been installed as a service.
#  Please uninstall the service before modifying this section.  The
#  service can then be reinstalled.

# Name of the service
wrapper.name=khkj-store-web

# Display name of the service
wrapper.displayname=khkj-store-web

# Description of the service
wrapper.description=khkj-store-web

# Service dependencies.  Add dependencies as needed starting from 1
wrapper.ntservice.dependency.1=

# Mode in which the service is installed.  AUTO_START, DELAY_START or DEMAND_START
wrapper.ntservice.starttype=AUTO_START

# Allow the service to interact with the desktop (Windows NT/2000/XP only).
wrapper.ntservice.interactive=FALSE

部署脚本

Install

@echo off

echo  Step 1: Install Java JDK

echo  ------begin----

:: 退到上级目录

cd ..

:: 获取jdk 的全路径,全路径=当前路径+jdk路径

:: "%~dp0" 这个是最起始的条用bat的cmd 的路径 所以我们要使用cd ..后退一个目录后用"%cd%" 来获取当前目录

echo  "%~dp0"

echo "%cd%"

::设置jdkpath变量

set jdkpath=%cd%\java\jdk_1.8_win32

echo %jdkpath%

::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代表忽略大小写  &&执行成功执行的命令  ||执行失败执行的命令

set installedJDK=false

echo %Path%|find /i "%JAVA_HOME%" && set installedJDK=true

if %installedJDK% == false setx Path "%%JAVA_HOME%%\bin;%Path%"
  
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


echo ----install zkemkeeper---

echo "%cd%"

::copy %cd%\dll\zkemkeeper.dll C:\Windows\SysWOW64
copy %cd%\dll\zkdll\* C:\Windows\SysWOW64
C:\Windows\SysWOW64\regsvr32  C:\Windows\SysWOW64\zkemkeeper.dll


echo ----install khkj-db

"%cd%\mysql\mysql-5.7.24-winx64\bin\mysqld.exe" install khkj-db

net start khkj-db

sc config khkj-db start=auto

::echo ----install khkj-store-web
::%cd%\store-app\wrapper\bin\wrapper -it  %cd%/store-app/wrapper/conf/store.conf

exit

Uninstall

@echo off

title 正在卸载中,请不要关闭

color 0a

::echo 停止并删除khkj-store-web

::net stop khkj-store-web

::sc delete khkj-store-web


echo 停止并删除MySQL

net stop khkj-db

sc delete khkj-db


echo 删除环境变量

echo 删除Java环境中...

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 CLASSPATH /f


echo 删除成功

其它

Process Explorer

Process Explorer使用图文教程, 可以用来查看Windows详细进程:

Session0穿透

Windows 常用的命令

Chrome WEB-Driver配置参数

JAVA从终端获取输入

中控考勤机配置