How to create an installer for a Java EE Web appli

2020-08-01 08:31发布

问题:

Is there a way to create an installer that contains a whole Java EE Web app, and where the final user only has to execute a single installer without the need to install a whole production environment?

What I'm trying to do is to save them the installation of JDK + Tomcat, if you have a better solution, please provide it.

回答1:

The more simplest way would be to create a compressed file with all files that you need (Tomcat and JDK). Upon unzipping the file, this could run a command to start Tomcat or what you require. It will be necessary to consider that the paths to the Tomcat and to the JDK to be the same as in the original environment.

I saw that Winrar can generate executable file and cans run a command at the end of extraction. (Have you seen this when you install a new program? First unzipping, then install. ) This at least in Windows. Other libraries or programs might generate something like Winrar.



回答2:

For Windows environement you can create installer as follows as same you can prepare as by creating shell script for linux environment.

1, Package the binary distributions of the s/w (e.g. postgresql, jetty, jre etc...)

2, put your war file in jetty webapps

3, create data folder inside postgres

4, setting various environment variables and initialization using batch file. In my case I created following "install.bat" file.

REM set jdk environment
PATH=%~dp0Java\jre6\bin;%~dp0Java\jre6\lib;%~dp0pgsql\bin;%PATH%
setx PATH "%PATH%"

REM set postgres environment
SETX PGDATA "%~dp0pgsql\data"
SETX PGDATABASE postgres
SETX PGUSER postgres
SETX PGPORT 5432
SETX PGLOCALEDIR "%~dp0pgsql\share\locale"

REM initdb initialize the postgresql initialization properties
"%~dp0pgsql\bin\initdb" -D "%~dp0pgsql\data" -U postgres -A trust

REM put the temp.vbs script into startup
cscript //NoLogo //B "%~dp0start_startup_shortcut.vbs"
cscript //NoLogo //B "%~dp0start_desktop_shortcut.vbs"
cscript //NoLogo //B "%~dp0app_shortcut.vbs"
REM start the database service
"%~dp0pgsql\bin\pg_ctl" -D "%~dp0pgsql\data" start

timeout /T 10

"%~dp0pgsql\bin\createdb.exe" -U postgres dbName
"%~dp0pgsql\bin\psql.exe" -U postgres -d dbName -f "%~dp0backup.sql"

REM java -DSTART="%~dp0jetty8\start.ini" -jar "%~dp0jetty8\start.jar"

REM start the jetty server
java -Djetty.home="%~dp0jetty8" -jar "%~dp0jetty8\start.jar" > pb_log.txt


pause

5, create batch file to start services. For example "start.bat"

REM start the database service

"%~dp0pgsql\bin\pg_ctl" -D "%~dp0pgsql\data" start

java -Djetty.home="%~dp0jetty8" -jar "%~dp0jetty8\start.jar"

6, You can create application shortcuts as follows. For example "app_shortcut.vbs"

Dim FileName
FileName = "projectName"

Set wsShell= CreateObject("WScript.Shell")

Set wshSysEnv = wsShell.Environment("PROCESS")

sMyHomePath = wshSysEnv("HOMEPATH")

Set shortcut =
wsShell.CreateShortcut(CreateObject("WScript.Shell").SpecialFolders("Desktop")& + "\" + 
FileName + ".lnk")

shortcut.Description = "Start projectName"

currentDirectory = left(WScript.ScriptFullName,
(Len(WScript.ScriptFullName))-(len(WScript.ScriptName)))

shortcut.TargetPath = currentDirectory + "projectName.bat"

shortcut.Arguments = "/Arguments:Shortcut"

shortcut.Save

7, Start service desktop shortcuts as follows:

Dim FileName
FileName = "Start projectName Server"

Set wsShell= CreateObject("WScript.Shell")

Set wshSysEnv = wsShell.Environment("PROCESS")

sMyHomePath = wshSysEnv("HOMEPATH")

Set shortcut = 
wsShell.CreateShortcut(CreateObject("WScript.Shell").SpecialFolders("Desktop")& + "\" + 
FileName + ".lnk")

shortcut.Description = "Start projectName"

currentDirectory = left(WScript.ScriptFullName,
(Len(WScript.ScriptFullName))-(len(WScript.ScriptName)))

shortcut.TargetPath = currentDirectory + "start.bat"

shortcut.Arguments = "/Arguments:Shortcut"

shortcut.Save

8, Run application batch file

@echo off
netstat -o -n -a | findstr 0.0:9095
if %ERRORLEVEL% equ 0 (start http://localhost:9095) ELSE (echo "Please Start the Server"
pause)


回答3:

While a single installer may give the appearance of convenience it can start to be problematic in a number of use cases:

  • when Tomcat, the JDK or some other component has to be upgraded to resolve a security issue. It is easier for end users to do this with separate installers and you don't have to create a new installer with the updated bits
  • some users may want to run you application on a different app server to the one you bundle
  • if every app did this, users would end up with one JDK install + one container install for every app

Since both the JVM install and Tomcat install are trivial, I'd recommend shipping your app with some nice, clear installation instructions rather than as a single package. YMMV.



回答4:

If your web application is just a single WAR file and you can use any servlet 2.4 container, then consider using it embedded with the Winstone container:

http://winstone.sourceforge.net/#embedding

You have now reduced your problem to a single executable JAR, for which plenty of installers exist. Which one to choose depends on the actual circumstances.