发布:2020/7/6 9:59:32作者:管理员 来源:本站 浏览次数:1121
首先我们来了解下,Tomcat 解压版配置需要做哪些事情:
service.bat install 默认的安装方式,注意.bat文件必须要将命令行目录切换到bat文件目录下,否则会不成功
JDK环境变量添加,与注册表操作类SysEnvironment 类前面已经添加了,这里就不重复了(请看C# 制作Java +Mysql+Tomcat 环境安装程序,一键式安装)
注意前面的MyInstallerClassDll安装类 的OnAfterInstall方法已经将Tomcat添加到环境变量(前面也添加了,这里将代码全部列出)
using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Configuration.Install; using System.Windows.Forms; using System.IO; using System.Text; using System.Diagnostics; namespace CustomAction {
[RunInstaller(true)] public partial class MyInstallerClassDll : Installer { public MyInstallerClassDll() {
InitializeComponent();
} protected override void OnBeforeInstall(IDictionary savedState) { string server = this.Context.Parameters["server"]; string user = this.Context.Parameters["user"]; base.OnBeforeInstall(savedState);
} public override void Install(IDictionary stateSaver) { string installPath = this.Context.Parameters["targetdir"]; string server = this.Context.Parameters["server"]; string user = this.Context.Parameters["user"]; //Mysql的配置文件 IniFile ini = new IniFile(installPath + @"MySQL\MySQL Server 5.5\my.ini"); //mysql安装路径 ini.Write("mysqld", "basedir", installPath + @"MySQL\MySQL Server 5.5\"); //Mysql数据文件夹 ini.Write("mysqld", "datadir", installPath + @"MySQL\MySQL Server 5.5\Data\"); base.Install(stateSaver);
} protected override void OnAfterInstall(IDictionary savedState) { string installPath = this.Context.Parameters["targetdir"]; string mysqlpath = installPath + @"MySQL\MySQL Server 5.5\bin"; string jrePath = installPath + @"Java\jre6\bin"; string iniPath = installPath + @"MySQL\MySQL Server 5.5\my.ini"; string tomcatPath = installPath + @"Tomcat\Tomcat6\bin";
InstallOrUninstallMysql(mysqlpath, iniPath, true); //设置Mysql环境变量 SysEnvironment.SetPath(mysqlpath); //设置JRE环境变量 SysEnvironment.SetPath(jrePath); //设置Tomcat环境变量 SysEnvironment.SetPath(tomcatPath);
SysEnvironment.SetSysEnvironment("CATALINA_HOME", tomcatPath);
SysEnvironment.SetSysEnvironment("CATALINA_BASE", tomcatPath);
InstallOrUninstallTomcate( tomcatPath, true); base.OnAfterInstall(savedState);
} /// <summary> /// 安装于卸载Tomcat服务 /// </summary> /// <param name="tomcatPath"></param> /// <param name="yesNo">true为安装,false为卸载</param> private static void InstallOrUninstallTomcate(string tomcatPath, bool yesNo) {
Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
process.Start();
process.StandardInput.WriteLine("cd " + tomcatPath);
process.StandardInput.WriteLine(tomcatPath.Substring(2) + " cd"); if (yesNo)
{
process.StandardInput.WriteLine("service.bat install");//默认服务名为Tomcat6 process.StandardInput.WriteLine("net start Tomcat6");//启动服务 process.StandardInput.WriteLine("exit");
} else {
process.StandardInput.WriteLine("net stop Tomcat6");//停止服务 process.StandardInput.WriteLine("service.bat uninstall");
process.StandardInput.WriteLine("exit");
}
process.Close();
} /// <summary> /// 安装或者卸载Mysql服务 /// </summary> /// <param name="mysqlpath"></param> /// <param name="iniPath"></param> /// <param name="isInstall">为true时安装,否则为卸载</param> private static void InstallOrUninstallMysql(string mysqlpath, string iniPath, bool isInstall) { //安装Mysql服务 Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
process.Start();
process.StandardInput.WriteLine("");
process.StandardInput.WriteLine("cd " + mysqlpath);
process.StandardInput.WriteLine(mysqlpath.Substring(2) + " cd"); //mysqld --install MySQLXY --defaults-file="D:\Program Files\MySQL\MySQL Server 5.5\my.ini" if (isInstall)
{
process.StandardInput.WriteLine("mysqld --install MySQL --defaults-file=" + '"' + iniPath + '"');
process.StandardInput.WriteLine("net start mysql");
} else {
process.StandardInput.WriteLine("net stop mysql"); //mysqld --install MySQLXY --defaults-file="D:\Program Files\MySQL\MySQL Server 5.5\my.ini" process.StandardInput.WriteLine("mysqld --remove MySQL --defaults-file=" + '"' + iniPath + '"');
}
process.StandardInput.WriteLine("");
process.StandardInput.WriteLine("");
process.StandardInput.WriteLine("exit"); //Writefile(installPath,process.StandardOutput.ReadToEnd().ToString()); process.Close();
} public override void Uninstall(IDictionary savedState) { string installPath = this.Context.Parameters["targetdir"]; string mysqlpath = installPath + @"MySQL\MySQL Server 5.5\bin"; string iniPath = installPath + @"MySQL\MySQL Server 5.5\my.ini"; string tomcatPath = installPath + @"Tomcat\Tomcat6\bin";
InstallOrUninstallMysql(mysqlpath, iniPath, true);
InstallOrUninstallTomcate(tomcatPath,false); base.Uninstall(savedState);
} /// <summary> /// 写日志(这个是我用来记录测试用) /// </summary> /// <param name="path"></param> /// <param name="msg"></param> public void Writefile(string path, string msg) { string file = path + @"日志.txt"; if (File.Exists(file))
File.Delete(file); using (StreamWriter sw = new StreamWriter(file, true))
{
sw.WriteLine(msg);
}
}
}
}
到此,切换安装项目到自定义视图
看安装与卸载的操作是否已经定义,若没有,右键安装--添加要自定义的操作即可,编译生成安装包,若没有出错,就可以了安装运行了,
注意安装完成后重启电脑,再在服务里面启动tomcat服务,在浏览器里输入localhost:8080,出现Tomcat网页,说明Ok。
© Copyright 2014 - 2024 柏港建站平台 ejk5.com. 渝ICP备16000791号-4