IzPack替换文本文件中的变量

时间:2010-12-27 12:13:46

标签: java installer izpack

我正在试图弄清楚如何在文本文件中使用IzPack替换变量。看起来它应该是一件简单的事情,但我找不到使用现有文档做这件事的具体例子。

有什么想法吗?

提前致谢。

2 个答案:

答案 0 :(得分:5)

我假设使用file或fileset标记将要处理的文件添加到其中一个包中。为了处理该文件(仅在安装过程结束时发生),必须将该文件的可分析标记添加到同一个包中。例如

<packs>
    <pack name="Base" required="yes">
        <description>Application and all its dependencies.</description>
        <fileset dir="dependencies" targetdir="$INSTALL_PATH/dependencies" />
        <file src="Licence.txt" targetdir="$INSTALL_PATH" />
        <file src="application.properties" targetdir="$INSTALL_PATH/dependencies" />
        <file src="run.bat" targetdir="$INSTALL_PATH" os="windows" />
        <file src="run.sh" targetdir="$INSTALL_PATH" os="unix" />
        <parsable targetfile="$INSTALL_PATH/run.bat" os="windows" />
        <parsable targetfile="$INSTALL_PATH/run.sh" os="unix" />
        <parsable targetfile="$INSTALL_PATH/dependencies/application.properties" />
    </pack>
</packs>

上面的例子中有三个可解析的标签 - 两个OS依赖,一个OS独立。目标文件首先被复制到相应文件标签中指定的相应目标目录,然后通过用文件中的变量名替换它们的值进行处理。

答案 1 :(得分:2)

01es' answer上构建这是一个示例,您可以让用户使用UserInputPanel为应用程序的数据选择路径,然后将该路径写入安装目录中的配置文件供您阅读。

包含您要替换的变量的示例config.xml

<?xml version="1.0" encoding="UTF-8"?>
<Entries>
  <Entry>
    <Key>appDataDir</Key>
    <!-- IzPack will substitute this -->
    <Value>$appDataDir</Value>
  </Entry>
</Entries>

userInputSpec.xml:

<userInput>
  <panel id="panel1">
  <field type="dir" variable="appDataDir">
    <spec size="20" set="$USER_HOME\AppData\Roaming\$APP_NAME" mustExist="false" create ="true"/>
    <os family="windows"/>
  </field>
  </panel>
</userInput>

installer.xml:

<?xml version="1.0" encoding="UTF-8"?><installation version="1.0">
  <info>
    <appname>Your app</appname>
    <appversion>0.0.1</appversion>
    <!-- Try to run as the administrator on Windows to be able to install under "C:\Program Files" -->
    <run-privileged condition="izpack.windowsinstall" />
  </info>

  <locale>
    <langpack iso3="eng" />
  </locale>

  <resources>
    <res id="userInputSpec.xml" src="userInputSpec.xml" parse="yes" type="xml" />
  </resources>

  <panels>
    <panel classname="UserInputPanel" id="panel1" />
    <panel classname="InstallPanel" />
    <panel classname="FinishPanel" />
  </panels>

  <packs>
    <pack name="Core" id="core.package" required="yes">
      <description>The base files that need to be part of the app</description>

      <!-- The runnable application should be in this directory -->
      <fileset dir="YourAppDir" targetdir="$INSTALL_PATH/YourAppDir">
        <include name="**" />
      </fileset>

      <!-- This file contains placeholder variables starting with $ that Izpack substitutes with values that the user enters during installation in the UserInputPanel -->
      <parsable targetfile="$INSTALL_PATH/YourAppDir/config.xml" /> 

    </pack>
  </packs>
</installation>