install.xml
<?xml version="1.0" encoding="utf-8" standalone="yes" ?> <installation version="1.0"> <info> <appname>CLAY</appname> <appversion>1.0-0208</appversion> <url>http://www.clayz.com</url> <javaversion>1.6.0_12</javaversion> <authors> <author name="Clay Zhong" email="zjclay@gmail.com" /> </authors> </info> <!-- define install window --> <guiprefs width="500" height="380" resizable="no" /> <!-- language options --> <locale> <langpack iso3="eng" /> <langpack iso3="chn" /> </locale> <resources> <res id="LicencePanel.licence" src="Licence.txt" /> <res id="InfoPanel.info" src="Readme.txt" /> <res id="userInputSpec.xml" src="userInputSpec.xml" /> <res id="shortcutSpec.xml" src="shortcutSpec.xml" /> </resources> <variables> <variable name="InstallerFrame.logfilePath" value="$INSTALL_PATH/log/install.log" /> <variable name="desktopshortcutcheckboxenabled" value="true" /> </variables> <jar src="lib/install-utils.jar" /> <jar src="lib/mysql-connector-java-5.1.7-bin.jar" /> <listeners> <listener installer="ConfigurationListener" /> <listener installer="DatabaseGenerationlistener" /> </listeners> <panels> <panel classname="HelloPanel" /> <panel classname="InfoPanel" /> <panel classname="LicencePanel" /> <panel classname="TargetPanel" /> <panel classname="UserInputPanel" id="jdbc.parameters"> <validator classname="com.clay.install.JdbcConnectionValidator" /> </panel> <panel classname="UserInputPanel" id="mail.server.parameters" /> <panel classname="PacksPanel" /> <panel classname="ShortcutPanel" /> <panel classname="InstallPanel" /> <panel classname="FinishPanel" /> </panels> <packs> <pack name="Base" required="yes"> <description>The base files</description> <file src="Readme.txt" targetdir="$INSTALL_PATH" /> <file src="Licence.txt" targetdir="$INSTALL_PATH" /> <file src="icons" targetdir="$INSTALL_PATH" /> </pack> <pack name="Src" required="no"> <description>Source File</description> <file src="src" targetdir="$INSTALL_PATH/src" /> </pack> <pack name="App" required="yes"> <description>Application</description> <file src="WebRoot" targetdir="$INSTALL_PATH" /> </pack> </packs> <native type="izpack" name="ShellLink.dll" /> </installation>
userInputSpec.xml
<?xml version="1.0" encoding="utf-8"?> <userInput> <panel order="0" id="jdbc.parameters"> <field type="title" txt="Database Configuration" bold="true" size="1" /> <field type="divider" align="top" /> <field type="staticText" align="left" txt="Please input existing MySQL username and password." /> <field type="text" variable="dbUsername"> <spec txt="Username:" id="text.label" size="25" /> <validator class="com.izforge.izpack.util.NotEmptyValidator" txt="Username for MySQL can not be empty!" /> </field> <field type="password" variable="dbPassword"> <spec> <pwd txt="Password:" size="25" /> </spec> <validator class="com.izforge.izpack.util.NotEmptyValidator" txt="Password for MySQL can not be empty!" /> </field> </panel> <panel order="1" id="mail.server.parameters"> <field type="title" txt="Mail Server Configuration" bold="true" size="1" /> <field type="divider" align="top" /> <field type="staticText" align="left" txt="Please input your mail server account." /> <field type="text" variable="mailHost"> <spec txt="Mail Host:" id="text.label" size="25" /> <validator class="com.izforge.izpack.util.NotEmptyValidator" txt="Mail host can not be empty!" /> </field> <field type="text" variable="mailUserName"> <spec txt="Username:" id="text.label" size="25" /> <validator class="com.izforge.izpack.util.NotEmptyValidator" txt="Username for mail server can not be empty!" /> </field> <field type="password" variable="mailPassword"> <spec> <pwd txt="Password:" size="25" /> </spec> <validator class="com.izforge.izpack.util.NotEmptyValidator" txt="Password for mail server can not be empty!" /> </field> </panel> </userInput>
shortcutSpec.xml
<?xml version="1.0" encoding="UTF-8"?> <shortcuts> <skipIfNotSupported /> <programGroup defaultName="CLAY" location="applications||startMenu" /> <!-- Shortcut for start server --> <shortcut name="Start CLAY" target="$INSTALL_PATH\WebRoot\startup.bat" workingDirectory="$INSTALL_PATH\WebRoot" description="This starts CLAY" iconFile="$INSTALL_PATH\icons\izpack.ico" iconIndex="0" initialState="noShow" programGroup="yes" desktop="yes" applications="no" startMenu="yes" startup="no"> </shortcut> <!-- Shortcut for stop server --> <shortcut name="Stop CLAY" target="$INSTALL_PATH\WebRoot\startup.bat" workingDirectory="$INSTALL_PATH\WebRoot" description="This starts CLAY" iconFile="$INSTALL_PATH\icons\izpack.ico" iconIndex="0" initialState="noShow" programGroup="yes" desktop="yes" applications="no" startMenu="yes" startup="no"> </shortcut> <!-- Shortcut for uninstall --> <shortcut name="Uninstall CLAY" target="$INSTALL_PATH\Uninstaller\uninstaller.jar" workingDirectory="$INSTALL_PATH\Uninstaller" description="This uninstall CLAY" iconFile="$INSTALL_PATH\icons\izpack.ico" iconIndex="0" initialState="noShow" programGroup="yes" desktop="no" applications="no" startMenu="no" startup="no"> </shortcut> </shortcuts>
自定义validator,将其打包放到lib目录下
package com.clay.install;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import com.izforge.izpack.installer.AutomatedInstallData;
import com.izforge.izpack.installer.DataValidator;
/**
* @author Clay Zhong, Email: zjclay@gmail.com
* @date 2009-2-10
*/
public class JdbcConnectionValidator implements DataValidator {
@Override
public boolean getDefaultAnswer() {
return true;
}
@Override
public String getErrorMessageId() {
return "Can not connect to MySQL, please check your database.";
}
@Override
public String getWarningMessageId() {
return null;
}
@Override
public Status validateData(AutomatedInstallData arg) {
Connection connection = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost", arg
.getVariable("dbUsername"), arg.getVariable("dbPassword"));
} catch (ClassNotFoundException e) {
// TODO: log exception
return Status.ERROR;
} catch (SQLException e) {
// TODO: log exception
return Status.ERROR;
} finally {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
// TODO: do nothing and log exception
}
}
}
return Status.OK;
}
}
自定义listener
package com.clay.install;
import com.izforge.izpack.event.SimpleInstallerListener;
import com.izforge.izpack.installer.AutomatedInstallData;
import com.izforge.izpack.util.AbstractUIProgressHandler;
/**
* @author Clay Zhong, Email: zjclay@gmail.com
* @date 2009-2-10
*/
public class DatabaseGenerationlistener extends SimpleInstallerListener {
private String sqlPath = "sql/init.sql";
public void beforePacks(AutomatedInstallData idata, Integer npacks,
AbstractUIProgressHandler handler) throws Exception {
StringBuilder command = new StringBuilder("mysql -u").append(
idata.getVariable("dbUsername")).append(" -p").append(
idata.getVariable("dbPassword")).append(" < ").append(sqlPath);
java.lang.Runtime.getRuntime().exec("cmd /c " + command.toString());
}
}
package com.clay.install;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import com.izforge.izpack.PackFile;
import com.izforge.izpack.event.SimpleInstallerListener;
/**
* @author Clay Zhong, Email: zjclay@gmail.com
* @date 2009-2-10
*/
public class ConfigurationListener extends SimpleInstallerListener {
public void afterFile(File file, PackFile packFile) throws Exception {
if (file.getName().equals("database.properties")) {
// modify username and password
StringBuilder stringBuilder = new StringBuilder();
BufferedReader reader = null;
BufferedWriter writer = null;
try {
reader = new BufferedReader(new FileReader(file));
String temp = null;
while ((temp = reader.readLine()) != null) {
stringBuilder.append(temp);
stringBuilder.append("\n");
}
String fileBody = stringBuilder.toString();
fileBody.replaceAll("root", "test");
writer = new BufferedWriter(new FileWriter(file));
writer.write(fileBody);
} catch (Exception e) {
// TODO: log exception
} finally {
try {
if (reader != null) {
reader.close();
}
if (writer != null) {
writer.close();
}
} catch (Exception e) {
// TODO: log exception
}
}
}
}
}
部署listener步骤如下:
- 将之拷贝到[IzPackRoot]/src/lib/[MyPackagePath]/MyCompilerListener.java。
- 在[IzPackRoot]/src/build.xml中添加build-compiler-listener
<build-installer-listener name="DatabaseGenerationlistener"> <include name="com/clay/install/DatabaseGenerationlistener.java" /> </build-installer-listener> <build-installer-listener name="ConfigurationListener"> <include name="com/clay/install/ConfigurationListener.java" /> </build-installer-listener>
- 编译build.xml。
- 在install.xml中添加listener。
- 命令行运行
java -jar D:/IzPack/lib/compiler.jar E:/CLAY/install.xml -b E:/CLAY -o E:/CLAY/install.jar