web项目打包成exe在windows下一键安装 jdk+Tomcat+mysql
Posted 极限森林
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了web项目打包成exe在windows下一键安装 jdk+Tomcat+mysql相关的知识,希望对你有一定的参考价值。
最近因为公司销售人员不懂软件,所以每次部署项目都需要技术人员到客户现场进行部署,很不方便。所以公司就让把我们的项目做成傻瓜式安装,这样就可以让销售人员去安装,也给技术省了很多事。我在网上找了很多教程但是有些地方还是不全,而且每个人写的也都不一样,所以在制作过程中遇到很多问题,经过几天研究终于实现了,现在就把我整理、研究的写下来,希望可以帮到更多人。
一:打包工具
在网上下载一个 Inno steup的制作exe文件工具
下面是我整理好的compile.iss文件(以下这个可以直接复制过去把要改的东西改掉就可以直接用了):
; Script generated by the Inno Setup Script Wizard.
; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES!
#define MyAppName "xxxx管理系统" <------软件的名称
#define MyAppVersion "V1.0" <-------软件的版本
#define MyAppPublisher "xxxxxx信息科技有限公司" <------公司名称
#define MyAppURL "http://www.xxxx.com/" <------公司网址
[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={{DCCFE898-2E2D-4C7B-9A45-C81B3A50F80C}
AppName={#MyAppName}
AppVersion={#MyAppVersion}
;AppVerName={#MyAppName} {#MyAppVersion}
AppPublisher={#MyAppPublisher}
AppPublisherURL={#MyAppURL}
AppSupportURL={#MyAppURL}
AppUpdatesURL={#MyAppURL}
DefaultDirName=D:\\soft <------默认安装的路径
DefaultGroupName={#MyAppName}
OutputBaseFilename=setup
SetupIconFile=C:\\Users\\g5000\\Desktop\\soft\\logo.ico <------安装图标的路径
Compression=lzma
SolidCompression=yes
PrivilegesRequired=admin
[Languages]
Name: "chinesesimp"; MessagesFile: "compiler:Default.isl"
[Files] <------需要打包成exe文件的jdk+Tomcat+mysql文件的路径
Source: "C:\\Users\\g5000\\Desktop\\soft\\*"; 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}"
Name: "{commondesktop}\\xxxxxx理系统"; Filename: http://localhost:9080 <------ tomcat访问的端口,这里改成9080是为了避免冲突
[Run] <------ 一键安装后要运行一下三个bat文件,Flags: runhidden; 表示运行是隐藏的
Filename: "{app}\\tomcat6\\Set_Env.bat"; Flags: runhidden;
Filename: "{app}\\tomcat6\\bin\\启动服务.bat"; Flags: runhidden;
Filename: "{app}\\MySQL\\bin\\启动服务.bat"; Flags: runhidden;
二:jdk+Tomcat+mysql具体的修改和整理
1:三个文件目录:
mysql:
tomcat6:
jdk(jdk放在tomcat的bin目录下):
2:需要修改或创建的文件
mysql的my.ini文件:
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It\'s a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.
[mysqld]
character_set_server=utf8
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
# These are commonly set, remove the # and set as required.
# basedir = .....
# datadir = .....
port = 3307
# server_id = .....
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
[client]
port=3307
default-character-set=utf8
[mysql]
default-character-set=utf8
启动服务.bat文件:
cd /d %~dp0
"%cd%\\mysqld.exe" -install HQmysql
net start HQmysql
tomcat6的Set_Env.bat文件(jdk,配置环境变量并写到注册表里去):
@echo off
echo
cd ..
echo "%~dp0"
echo "%cd%"
set jdkpath=%cd%\\tomcat6\\bin\\Java\\jdk1.6.0_45
echo %jdkpath%
setx JAVA_HOME "%jdkpath%" -m
setx CLASSPATH ".;%%JAVA_HOME%%\\lib\\tools.jar;%%JAVA_HOME%%\\lib\\dt.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_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%"
)
exit
tomcat启动服务.bat文件:
echo
call "%~dp0%service.bat" install tomcat6
echo
sc config tomcat6 start= auto
sc start tomcat6
rem 下面两句是设置到服务里自动启动的
wmic service where name="tomcat6" changestartmode "automatic"
wmic service where name="tomcat6" startservice
exit
下面重点说下tomcat的service.bat文件,因为这个文件要配置tomcat6w.exe文件的java里的jvm
,如果不配置好服务里的tomcat是无法启动的,启动就会报。而且tomcat6w.exe也打不开,会出现这个错误。所以service.bat文件很重要,下面给出我修改的service.bat,可以把这个复制下去后用beyond compare这个工具和原来tomcat里的service.bat文件比较下就可以清楚的看到我修改的东西。如果想弄清楚service.bat文件里语句的走向流程的话可以在开始的时候把@echo off改成@echo on然后在结尾的地方加暂停(pause),然后保存下,双击运行,就会弹出
然后把这里面的语句和service.bat里的语句进行对比就会明白service里语句是怎么运行的,以及它的作用。
service.bat配置如下:
@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 http://www.apache.org/licenses/LICENSE-2.0
rem
rem Unless required by applicable law or agreed to in writing, software
rem distributed under the License is distributed on an "AS IS" BASIS,
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 ---------------------------------------------------------------------------
rem NT Service Install/Uninstall script
rem
rem Options
rem install Install the service using Tomcat6 as service name.
rem Service is installed using default settings.
rem remove Remove the service from the System.
rem
rem name (optional) If the second argument is present it is considered
rem to be new service name
rem ---------------------------------------------------------------------------
setlocal
rem Guess CATALINA_HOME if not defined
set "CURRENT_DIR=%cd%"
rem if not "%CATALINA_HOME%" == "" goto gotHome
set "CATALINA_HOME=%cd%"
if exist "%CATALINA_HOME%\\bin\\tomcat6.exe" goto okHome
rem CD to the upper dir
cd ..
set "CATALINA_HOME=%cd%"
:gotHome
if exist "%CATALINA_HOME%\\bin\\tomcat6.exe" goto okHome
echo The tomcat6.exe was not found...
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
if not "%JRE_HOME%" == "" goto gotJreHome
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%\\bin\\java.exe" goto noJavaHome
if not exist "%JRE_HOME%\\bin\\javaw.exe" goto noJavaHome
goto okJavaHome
:gotJdkHome
set "JRE_HOME=%JAVA_HOME%\\jre"
if not exist "%JAVA_HOME%\\jre\\bin\\java.exe" goto okJavaHome
if not exist "%JAVA_HOME%\\jre\\bin\\javaw.exe" goto noJavaHome
if not exist "%JAVA_HOME%\\bin\\javac.exe" goto noJavaHome
if not "%JRE_HOME%" == "" goto okJavaHome
set "JRE_HOME=%JAVA_HOME%\\jre"
goto okJavaHome
:noJavaHome
echo The JAVA_HOME environment variable is not defined correctly
echo This environment variable is needed to run this program
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%\\bin\\tomcat6.exe"
rem Set default Service name
set SERVICE_NAME=Tomcat6
set DISPLAYNAME=HQTomcat6
if "x%1x" == "xx" goto doInstall
set SERVICE_CMD=%1
shift
if "x%1x" == "xx" goto checkServiceCmd
set SERVICE_NAME=%1
set DISPLAYNAME=HQTomcat6 %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%"
:displayUsage
echo.
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
echo The service \'%SERVICE_NAME%\' has been removed
goto end
:doInstall
set "JRE_HOME=%JAVA_HOME%\\jre"
rem Install the service
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%\\bin\\server\\jvm.dll"
rem if exist "%JVM%" goto foundJvm
rem Try to use the client jvm
set "JVM=%JRE_HOME%\\bin\\client\\jvm.dll"
if exist "%JVM%" goto foundJvm
echo Warning: Neither \'server\' nor \'client\' jvm.dll was found at JRE_HOME.
set JVM="%JAVA_HOME%\\jre\\bin\\client\\jvm.dll"
:foundJvm
echo Using JVM: "%JVM%"
set "CLASSPATH=%CATALINA_HOME%\\bin\\bootstrap.jar;%CATALINA_BASE%\\bin\\tomcat-juli.jar"
if not "%CATALINA_HOME%" == "%CATALINA_BASE%" set "CLASSPATH=%CLASSPATH%;%CATALINA_HOME%\\bin\\tomcat-juli.jar"
"%EXECUTABLE%" //IS//%SERVICE_NAME% ^
--Description "Apache Tomcat 6.0.45 Server - http://tomcat.apache.org/" ^
--DisplayName "%DISPLAYNAME%" ^
--Install "%EXECUTABLE%" ^
--LogPath "%CATALINA_BASE%\\logs" ^
--StdOutput auto ^
--StdError auto ^
--Classpath "%CLASSPATH%" ^
--Jvm "%CATALINA_HOME%\\bin\\Java\\jdk1.6.0_45\\jre\\bin\\client\\jvm.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 ^
--JvmOptions "-Dcatalina.home=%CATALINA_HOME%;-Dcatalina.base=%CATALINA_BASE%;-Djava.endorsed.dirs=%CATALINA_HOME%\\endorsed;-Djava.io.tmpdir=%CATALINA_BASE%\\temp;-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager;-Djava.util.logging.config.file=%CATALINA_BASE%\\conf\\logging.properties" ^
--JvmMs 128 ^
--JvmMx 256
if not errorlevel 1 goto installed
echo Failed installing \'%SERVICE_NAME%\' service
goto end
:installed
echo The service \'%SERVICE_NAME%\' has been installed.
:end
cd "%CURRENT_DIR%"
主要的东西都在这里,然后还有tomcat端口的配置我在这里就不说了。参考博客:
http://blog.csdn.net/liuhaomatou/article/details/52576162
以上是关于web项目打包成exe在windows下一键安装 jdk+Tomcat+mysql的主要内容,如果未能解决你的问题,请参考以下文章
Windows服务项目打包成安装包(Windows服务)-----------VS2017项目程序打包成.msi或者.exe