背景:
前阵子在帮朋友做完个简单的用户信息管理系统的时候,考虑要怎么帮他部署这个工程。因为他的电脑不一定有jdk、tomcat、mysql之类的,所以就想着怎么把war包、jdk、tomcat、mysql一起整合成一个安装包发给他,让他点击安装就可以使用,也不用输入网址。所以就在网上找找看怎么整合,可是找了半天不是说的不明白就是文件不全,经过几天的摸索,看了很多篇这类文章,终于弄清楚了,并且也成功实现了,下面就开始写怎么打包整合成exe文件。
这几个文件内容我都是先复制粘贴别人的,然后与 自己文件的 路径啊、工具的名称啊等等 相对照,最后改了很多地方才打包成功并且安装成功的。脚本语言都没学过…抽空要学下
一、准备
1、下载免安装的mysql、tomcat、jdk(我用的是:官网下载的mysql-5.5.40、tomcat8.0、jdk1.8,都是64位的);
2、下载Inno Setup 打包工具(我用的是:Inno Setup 5版);
3、我在D盘建了个文件夹,命名为test ;
4、把下载好的mysql-5.5.40、tomcat8.0一起复制到“D: est”下;
5、把下载好的jdk放在“D: estapache-tomcat-8.0.29in”下:
二、打包的准备,写脚本
1、mysql的准备
1.1 因为我在官网下的mysql没有my.ini,所以我就上网找了个,然后修改里面的内容就行,放在“D: estmysql-5.5.40-winx64”下,以免出错:(标红的是我修改的,下文也是如此)
==my.ini start===
# MySQL Server Instance Configuration File
# ----------------------------------------------------------------------
# Generated by the MySQL Server Instance Configuration Wizard
# Installation Instructions
# ----------------------------------------------------------------------
# On Linux you can copy this file to /etc/my.cnf to set global options,
# mysql-data-dir/my.cnf to set server-specific options
# (@localstatedir@ for this installation) or to
# ~/.my.cnf to set user-specific options.
# On Windows you should keep this file in the installation directory
# of your server (e.g. C:Program FilesMySQLMySQL Server X.Y). To
# make sure the server reads the config file use the startup option
# “–defaults-file”.
# To run run the server from the command line, execute this in a
# command line shell, e.g.
# mysqld --defaults-file=“D: estmysql-5.5.40-winx64my.ini”
# To install the server as a Windows service manually, execute this in a
# command line shell, e.g.
# mysqld --install MySQLXY --defaults-file=“D: estmysql-5.5.40-winx64my.ini”
# And then execute this in a command line shell to start the server, e.g.
# net start MySQLXY
# Guildlines for editing this file
# ----------------------------------------------------------------------
# In this file, you can use all long options that the program supports.
# If you want to know the options a program supports, start the program
# with the “–help” option.
# More detailed information about the individual options can also be
# found in the manual.
# CLIENT SECTION
# ----------------------------------------------------------------------
# The following options will be read by MySQL client applications.
# Note that only client applications shipped by MySQL are guaranteed
# to read this section. If you want your own MySQL client program to
# honor these values, you need to specify it as an option during the
# MySQL client library initialization.
[client]
port=3306
[mysql]
default-character-set=utf8
# SERVER SECTION
# ----------------------------------------------------------------------
# The following options will be read by the MySQL Server. Make sure that
# you have installed the server correctly (see above) so it reads this
# file.
[mysqld]
# The TCP/IP Port the MySQL Server will listen on
port=3306
#Path to installation directory. All paths are usually resolved relative to this.
basedir=“D:/test/mysql-5.5.40-winx64”
#Path to the database root
datadir=“D:/test/mysql-5.5.40-winx64/data/”
# The default character set that will be used when a new schema or table is
# created and no character set is defined
character-set-server=utf8
# The default storage engine that will be used when create new tables when
default-storage-engine=INNODB
# Set the SQL mode to strict
sql-mode=“STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION”
# The maximum amount of concurrent sessions the MySQL server will
# allow. One of these connections will be reserved for a user with
# SUPER privileges to allow the administrator to login even if the
# connection limit has been reached.
max_connections=100
# Query cache is used to cache SELECT results and later return them
# without actual executing the same query once again. Having the query
# cache enabled may result in significant speed improvements, if your
# have a lot of identical queries and rarely changing tables. See the
# “Qcache_lowmem_prunes” status variable to check if the current value
# is high enough for your load.
# Note: In case your tables change very often or if your queries are
# textually different every time, the query cache may result in a
# slowdown instead of a performance improvement.
query_cache_size=32M
# The number of open tables for all threads. Increasing this value
# increases the number of file descriptors that mysqld requires.
# Therefore you have to make sure to set the amount of open files
# allowed to at least 4096 in the variable “open-files-limit” in
# section [mysqld_safe]
table_open_cache=256
# Maximum size for internal (in-memory) temporary tables. If a table
# grows larger than this value, it is automatically converted to disk
# based table This limitation is for a single table. There can be many
# of them.
tmp_table_size=35M
# How many threads we should keep in a cache for reuse. When a client
# disconnects, the client’s threads are put in the cache if there aren’t
# more than thread_cache_size threads from before. This greatly reduces
# the amount of thread creations needed if you have a lot of new
# connections. (Normally this doesn’t give a notable performance
# improvement if you have a good thread implementation.)
thread_cache_size=8
#*** MyISAM Specific options
# The maximum size of the temporary file MySQL is allowed to use while
# recreating the index (during REPAIR, ALTER TABLE or LOAD DATA INFILE.
# If the file-size would be bigger than this, the index will be created
# through the key cache (which is slower).
myisam_max_sort_file_size=100G
# If the temporary file used for fast index creation would be bigger
# than using the key cache by the amount specified here, then prefer the
# key cache method. This is mainly used to force long character keys in
# large tables to use the slower key cache method to create the index.
myisam_sort_buffer_size=69M
# Size of the Key Buffer, used to cache index blocks for MyISAM tables.
# Do not set it larger than 30% of your available memory, as some memory
# is also required by the OS to cache rows. Even if you’re not using
# MyISAM tables, you should still set it to 8-64M as it will also be
# used for internal temporary disk tables.
key_buffer_size=50M
# Size of the buffer used for doing full table scans of MyISAM tables.
# Allocated per thread, if a full scan is needed.
read_buffer_size=64K
read_rnd_buffer_size=256K
# This buffer is allocated when MySQL needs to rebuild the index in
# REPAIR, OPTIMZE, ALTER table statements as well as in LOAD DATA INFILE
# into an empty table. It is allocated per thread so be careful with
# large settings.
sort_buffer_size=256K
#*** INNODB Specific options ***
# Use this option if you have a MySQL server with InnoDB support enabled
# but you do not plan to use it. This will save memory and disk space
# and speed up some things.
#skip-innodb
# Additional memory pool that is used by InnoDB to store metadata
# information. If InnoDB requires more memory for this purpose it will
# start to allocate it from the OS. As this is fast enough on most
# recent operating systems, you normally do not need to change this
# value. SHOW INNODB STATUS will display the current amount used.
#innodb_additional_mem_pool_size=14M
# If set to 1, InnoDB will flush (fsync) the transaction logs to the
# disk at each commit, which offers full ACID behavior. If you are
# willing to compromise this safety, and you are running small
# transactions, you may set this to 0 or 2 to reduce disk I/O to the
# logs. Value 0 means that the log is only written to the log file and
# the log file flushed to disk approximately once per second. Value 2
# means the log is written to the log file at each commit, but the log
# file is only flushed to disk approximately once per second.
innodb_flush_log_at_trx_commit=1
# The size of the buffer InnoDB uses for buffering log data. As soon as
# it is full, InnoDB will have to flush it to disk. As it is flushed
# once per second anyway, it does not make sense to have it very large
# (even with long transactions).
innodb_log_buffer_size=1M
# InnoDB, unlike MyISAM, uses a buffer pool to cache both indexes and
# row data. The bigger you set this the less disk I/O is needed to
# access data in tables. On a dedicated database server you may set this
# parameter up to 80% of the machine physical memory size. Do not set it
# too large, though, because competition of the physical memory may
# cause paging in the operating system. Note that on 32bit systems you
# might be limited to 2-3.5G of user level memory per process, so do not
# set it too high.
innodb_buffer_pool_size=96M
# Size of each log file in a log group. You should set the combined size
# of log files to about 25%-100% of your buffer pool size to avoid
# unneeded buffer pool flush activity on log file overwrite. However,
# note that a larger logfile size will increase the time needed for the
# recovery process.
innodb_log_file_size=20M
# Number of threads allowed inside the InnoDB kernel. The optimal value
# depends highly on the application, hardware as well as the OS
# scheduler properties. A too high value may lead to thread thrashing.
innodb_thread_concurrency=18
==my.ini end===
1.2 把创建数据库、表、用户数据的sql放在“D: estmysql-5.5.40-winx64data“下:
1.3 写mysql的批处理脚本,放在“D: estmysql-5.5.40-winx64in”下
==mysql_ini.bat start===
cd /d %~dp0
“%cd%mysqld.exe” --initialize-insecure --user=mysql --console
echo -----mysql init succee-----
pause;
mysqld install mysql --defaults-file=“D: estmysql-5.5.40-winx64my.ini”
echo -----mysql service install succee-----
pause;
net start mysql
sc config mysql start=auto
net stop mysql
net start mysql
echo 安装完毕
pause;
“%cd%mysqladmin” -u root password root
echo 修改密码完毕
pause;
cd …
“%cd%inmysql.exe” -uroot -proot < “%cd%datassm_work.sql”
echo 建表完毕
pause;
echo 建立新用户完毕
=mysql_init.bat end==
2、jdk的准备
2.1 写jdk的批处理脚本,放在“D: estapache-tomcat-8.0.29inJavajdk1.8.0_151in”下(注意路径哦)
==autoInstallJDK.bat start==
@echo off
echo
cd …
echo “%~dp0”
echo “%cd%”
set jdkpath=%cd%apache-tomcat-8.0.29inJavajdk1.8.0_151
echo %jdkpath%
setx JAVA_HOME “%jdkpath%” -m
setx CLASSPATH “.;%%JAVA_HOME%%lib ools.jar;%%JAVA_HOME%%libdt.jar” -m
echo %Path%
echo %Path%|find /i “%java_home%” && set IsNull=true || set IsNull=false
echo %IsNull%
if not %IsNull%==true (
reg add “HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlSession ManagerEnvironment” /v Path /t REG_SZ /d “%Path%;%%JAVA_HOME%%in;%%JAVA_HOME%%jrein” /f
setx Path “%%JAVA_HOME%%in;%Path%”
)
exit
==autoInstallJDK.bat start==
3、tomcat的准备
3.1 修改tomcat的service.bat文件,因为这个文件要配置tomcat8w.exe文件的java里的jvm
,如果不配置好服务里的tomcat是无法启动的,启动就会报。而且tomcat8w.exe也打不开,会出现这个错误。
因为有有删除的地方,不好标红,所以最好还是下载个beyond compare工具来与原来的文件进行比较,很清楚显示哪里不一样。
==service.bat start==
@echo off
rem Licensed to the Apache Software Foundation (ASF) under one or more
rem contributor license agreements. See the NOTICE file distributed with
rem this work for additional information regarding copyright ownership.
rem The ASF licenses this file to You under the Apache License, Version 2.0
rem (the "License"); you may not use this file except in compliance with
rem the License. You may obtain a copy of the License at
rem
rem distributed under the License is distributed on an "AS IS" BASIS,
rem http://www.apache.org/licenses/LICENSE-2.0
rem
rem Unless required by applicable law or agreed to in writing, software
rem ---------------------------------------------------------------------------
rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rem See the License for the specific language governing permissions and
rem limitations under the License.
rem Service is installed using default settings.
rem NT Service Install/Uninstall script
rem
rem Options
rem install Install the service using Tomcat8 as service name.
rem remove Remove the service from the System.
rem
rem Guess CATALINA_HOME if not defined
rem name (optional) If the second argument is present it is considered
rem to be new service name
rem ---------------------------------------------------------------------------
setlocal
set "CURRENT_DIR=%cd%"
echo The tomcat8.exe was not found...
rem if not "%CATALINA_HOME%" == "" goto gotHome
set "CATALINA_HOME=%cd%"
if exist "%CATALINA_HOME%in omcat8.exe" goto okHome
rem CD to the upper dir
cd ..
set "CATALINA_HOME=%cd%"
:gotHome
if exist "%CATALINA_HOME%in omcat8.exe" goto okHome
if not "%JRE_HOME%" == "" goto gotJreHome
echo The CATALINA_HOME environment variable is not defined correctly.
echo This environment variable is needed to run this program
goto end
:okHome
rem Make sure prerequisite environment variables are set
if not "%JAVA_HOME%" == "" goto gotJdkHome
set "JRE_HOME=%JAVA_HOME%jre"
echo Neither the JAVA_HOME nor the JRE_HOME environment variable is defined
echo Service will try to guess them from the registry.
goto okJavaHome
:gotJreHome
if not exist "%JRE_HOME%injava.exe" goto noJavaHome
if not exist "%JRE_HOME%injavaw.exe" goto noJavaHome
goto okJavaHome
:gotJdkHome
echo The JAVA_HOME environment variable is not defined correctly
if not exist "%JAVA_HOME%jreinjava.exe" goto okJavaHome
if not exist "%JAVA_HOME%jreinjavaw.exe" goto noJavaHome
if not exist "%JAVA_HOME%injavac.exe" goto noJavaHome
if not "%JRE_HOME%" == "" goto okJavaHome
set "JRE_HOME=%JAVA_HOME%jre"
goto okJavaHome
:noJavaHome
echo This environment variable is needed to run this program
shift
echo NB: JAVA_HOME should point to a JDK not a JRE
goto end
:okJavaHome
if not "%CATALINA_BASE%" == "" goto gotBase
set "CATALINA_BASE=%CATALINA_HOME%"
:gotBase
set "EXECUTABLE=%CATALINA_HOME%in omcat8.exe"
rem Set default Service name
set SERVICE_NAME=Tomcat8
set DISPLAYNAME=Apache Tomcat 8.0
if "x%1x" == "xx" goto doInstall
set SERVICE_CMD=%1
:displayUsage
if "x%1x" == "xx" goto checkServiceCmd
set SERVICE_NAME=%1
set DISPLAYNAME=Apache Tomcat 8.0 %1
shift
if "x%1x" == "xx" goto checkServiceCmd
echo Unknown parameter "%1"
goto displayUsage
:checkServiceCmd
if /i %SERVICE_CMD% == install goto doInstall
if /i %SERVICE_CMD% == remove goto doRemove
if /i %SERVICE_CMD% == uninstall goto doRemove
echo Unknown parameter "%SERVICE_CMD%"
echo.
echo The service '%SERVICE_NAME%' has been removed
echo Usage: service.bat install/remove [service_name]
goto end
:doRemove
rem Remove the service
echo Removing the service '%SERVICE_NAME%' ...
echo Using CATALINA_BASE: "%CATALINA_BASE%"
"%EXECUTABLE%" //DS//%SERVICE_NAME% ^
--LogPath "%CATALINA_BASE%logs"
if not errorlevel 1 goto removed
echo Failed removing '%SERVICE_NAME%' service
goto end
:removed
goto end
:doInstall
set "JRE_HOME=%JAVA_HOME%jre"
rem Install the service
echo Warning: Neither 'server' nor 'client' jvm.dll was found at JRE_HOME.
echo Installing the service '%SERVICE_NAME%' ...
echo Using CATALINA_HOME: "%CATALINA_HOME%"
echo Using CATALINA_BASE: "%CATALINA_BASE%"
echo Using JAVA_HOME: "%JAVA_HOME%"
echo Using JRE_HOME: "%JRE_HOME%"
rem Try to use the server jvm
rem set "JVM=%JRE_HOME%inserverjvm.dll"
rem if exist "%JVM%" goto foundJvm
rem Try to use the client jvm
set "JVM=%JRE_HOME%inclientjvm.dll"
if exist "%JVM%" goto foundJvm
set JVM="%JAVA_HOME%jreinclientjvm.dll"
--StdOutput auto ^
:foundJvm
echo Using JVM: "%JVM%"
set "CLASSPATH=%CATALINA_HOME%inootstrap.jar;%CATALINA_BASE%in omcat-juli.jar"
if not "%CATALINA_HOME%" == "%CATALINA_BASE%" set "CLASSPATH=%CLASSPATH%;%CATALINA_HOME%in omcat-juli.jar"
"%EXECUTABLE%" //IS//%SERVICE_NAME% ^
--Description "Apache Tomcat 8.0.29 Server - http://tomcat.apache.org/" ^
--DisplayName "%DISPLAYNAME%" ^
--Install "%EXECUTABLE%" ^
--LogPath "%CATALINA_BASE%logs" ^
--StdError auto ^
--JvmOptions "-Dcatalina.home=%CATALINA_HOME%;-Dcatalina.base=%CATALINA_BASE%;-Djava.endorsed.dirs=%CATALINA_HOME%endorsed;-Djava.io.tmpdir=%CATALINA_BASE% emp;-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager;-Djava.util.logging.config.file=%CATALINA_BASE%conflogging.properties" ^
--Classpath "%CLASSPATH%" ^
--Jvm "%CATALINA_HOME%inJavajdk1.8.0_151jreinserverjvm.dll" ^
--StartMode jvm ^
--StopMode jvm ^
--StartPath "%CATALINA_HOME%" ^
--StopPath "%CATALINA_HOME%" ^
--StartClass org.apache.catalina.startup.Bootstrap ^
--StopClass org.apache.catalina.startup.Bootstrap ^
--StartParams start ^
--StopParams stop ^
--JvmMs 128 ^
--JvmMx 256
if not errorlevel 1 goto installed
echo Failed installing '%SERVICE_NAME%' service
goto end
:installed
cd "%CURRENT_DIR%"
echo The service '%SERVICE_NAME%' has been installed.
:end
==service.bat end====
3.2 写tomcat的批处理脚本,放在“D: estapache-tomcat-8.0.29in”下:
==启动服务.bat start==
echo
call “%~dp0%service.bat” install tomcat8
echo
sc config tomcat8 start= auto
sc start tomcat8
rem 下面两句是设置到服务里自动启动的
wmic service where name=“tomcat8” changestartmode “automatic”
wmic service where name=“tomcat8” startservice
exit
==启动服务.bat end====
3.3 把打包好的war包 直接复制 放在“D: estapache-tomcat-8.0.29webapps”下:
好了,目前脚本什么的都弄完了,就开始整合.exe文件。
三、使用Inno Setup工具制作.exe文件
1、在“D: est”下创建一个.iss后缀的文件,然后复制我下面给出的文件内容,修改并执行它,工具就会在当前目录创建一个名为“Output”文件夹,里面存有一个.exe的安装包。
[Setup]区域的命名和路径之类的我就不详说了
==now.iss start==
; 脚本由 Inno Setup 脚本向导 生成!
[Setup]
; 注: AppId的值为单独标识该应用程序。
; 不要为其他安装程序使用相同的AppId值。
; (生成新的GUID,点击 工具|在IDE中生成GUID。)
AppId={{9E044575-9CD9-4751-B0BE-F6758BA94548}
AppName=Test
AppVersion=V0.01
AppVerName=TestServer V0.01
AppPublisher=TestTechnology
AppPublisherURL=http://www.baidu.com/
AppSupportURL=http://www.baidu.com/
AppUpdatesURL=http://www.baidu.com/
DefaultDirName={pf}Test
DefaultGroupName=TestSoftWare
AllowNoIcons=yes
OutputBaseFilename=Test
Compression=lzma
SolidCompression=yes
[Files]
;拷贝tomcat
Source:“D: estapache-tomcat-8.0.29*”;DestDir:“{app}apache-tomcat-8.0.29”;Flags:igNoreversion recursesubdirs createallsubdirs
;拷贝mysql
Source:“D: estmysql-5.5.40-winx64*”;DestDir:“{app}mysql-5.5.40-winx64”;Flags:igNoreversion recursesubdirs createallsubdirs
[Languages]
Name: “english”; MessagesFile: “compiler:Default.isl”
[Icons]
Name: “{group}{cm:UninstallProgram,学生管理系统}”; Filename: “{uninstallexe}”
Name: “{commondesktop}学生管理系统”; Filename: http://localhost:8080/SSMWork/html/login.html
[INI]
;修改数据库配置文件
Filename:“{app}mysql-5.5.40-winx64my.ini”;Section:“mysqld”;Key:“basedir”; String:“{app}mysql-5.5.40-winx64”
Filename:“{app}mysql-5.5.40-winx64my.ini”;Section:“mysqld”;Key:“datadir”; String:“{app}mysql-5.5.40-winx64data”
Filename:“{app}mysql-5.5.40-winx64my.ini”;Section:“mysqld”;Key:“port”; String:“3306”
Filename:“{app}mysql-5.5.40-winx64my.ini”;Section:“client”;Key:“port”; String:“3306”
[Run]
Filename: “{app}apache-tomcat-8.0.29inJavajdk1.8.0_151inautoInstallJDK.bat”;
Filename: “{app}mysql-5.5.40-winx64inmysql_init.bat”;
Filename: “{app}apache-tomcat-8.0.29in启动服务.bat”;
[UninstallDelete]
Type:filesandordirs;Name:“{app}apache-tomcat-8.0.29”
Type:filesandordirs;Name:“{app}mysql-5.5.40-winx64”
==now.iss end==
**mysql、tomcat、jdk、war包整合全部结束,共需要创建、修改、复制的有 8 个文件。**
jdk:autoInstallJDK.bat
tomcat:SSMWork.war,启动服务.bat,service.bat
Inno setup:now.iss
mysql:my.ini,mysql_init.bat,ssm_work.sql
我在我自己的vmware虚拟机和笔记本电脑都成功安装了!这两个都是全新的无配置环境的系统
总结:
打包过程经历了几天,导致这么久的主要问题是:1、网上的教程有些不全面,不够细致;2、还有自己的一些粗心大意,有些细节没看全,急躁,jishi成功的打包了exe文件,但安装就会出现问题。所以还是不管做什么事情,即使再简单,还是需要一丝不苟的对待它。
可能会遇到的问题,如何去解决:
1、安装包没安装成功,根据弹出的错误提示,去看是从哪里出错,然后再到那一个点来解决它;
2、安装成功后启动程序出现mysql、tomcat或者jdk有问题,可以根据日志去找找失败的原因,英文不懂可以找翻译软件翻译,这很关键。
以上就是整合所需的东西和过程,算是比较详细了,希望可以帮到有需要的人。第一次写文章可能有些问题,自己没有发现到,如有疑问可以提出问题互相交流,谢谢。
--------------------------
文件转存到阿里云盘了,由于目前(2022.05.26)阿里云盘不能分享压缩包,所以只能尝试存在word文档里面进行分享。
「jdk+tomcat+mysql+war」https://www.aliyundrive.com/s/mHNCxVjQ1Dh 提取码: m08e 点击链接保存,或者复制本段内容,打开「阿里云盘」APP ,无需下载极速在线查看,视频原画倍速播放。
参考博客:
https://blog.csdn.net/liuhaomatou/article/details/52576162
https://blog.csdn.net/dj0721/article/details/72462688