At work we need to sometimes install a several tomcat instances on multiple machines. This was quite tedious to do manually so in order to speed things up, I wrote a powershell script which automates the installation. So the only thing I would need to do is to download the specific version that is needed and then run the script like:
.\install-tomcat.ps1 .\apache-tomcat-8.5.43-windows-x64.zip c:\applications\
and it would install the X number of instances.
Below is a simplified script which installs one instance automatically.
Parameters
Let’s create a new powershell file called install-tomcat.ps1 which takes two parameters, the zip-file (of tomcat) and the $destination. Save the directory where the script is for later use.
|
|
This is all good but lets add some parameter validation so that the file and destination has to exist
|
|
Unzip tomcat-zip
First we need to get the version number from the zip-filename to use later.
|
|
Now we can unzip the tomcat-zip file to a temporary directory
|
|
Copy directory to destination
Now we can copy the tomcat directory to the destination with the corret name.
|
|
In case we dont want to use the default ports of 8080/8005/8009 we need to change the values in tomcats server.xml.
We can enter our desired values in a json-file in the same directory.
ports.json:
|
|
Now we need to read the json-file from powershell
|
|
What we now need to do is to change the values in server.xml
|
|
Since I wanted to use the version number in the service name I had to rename tomcat8w.exe to the correct name also:
|
|
Now the only thing left is to install the services. Now you could make this value a parameter but like the other two but lets make it a promtable choice.
|
|
Now the only thing left is to install the services. Now you could make this value a parameter but like the other two but lets make it a promtable choice. Then we install it as a service and at the same time change some of the default configuration for it as well.
|
|
Complete script
Here’s the complete script in it’s glory.
install-tomcat.ps1:
|
|
ports.json:
|
|
If you need to install several tomcat instances, then you would need to loop through the number of instances and copy unpacked the tomcat-folder to the destination (with different names of course) to save some time and not need to unpack it several times
Comments