栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

打包用于Windows / Linux桌面的Java应用程序

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

打包用于Windows / Linux桌面的Java应用程序

为了跟进pauxu的回答,我在我的一个项目中使用了launch4j和NSIS,并认为展示我的使用方式会有所帮助。这是我为Windows做的事情。顺便说一句,我正在为Mac创建.app和.dmg,但是还没有弄清楚Linux怎么做。

launch4j和NSIS的项目副本

在我的项目中,我有一个“供应商”目录,在它的下面有一个“ launch4j”和“
nsis”目录。每个文件中都有每个应用程序的安装副本。我发现在项目本地拥有一个副本比强迫其他人安装两个产品并设置某种指向每个产品的环境变量要容易得多。

脚本文件

我的项目中还有一个“脚本”目录,其中包含项目的各种配置/脚本文件。首先是launch4j.xml文件:

<launch4jConfig>  <dontWrapJar>true</dontWrapJar>  <headerType>gui</headerType>  <jar>rpgam.jar</jar>  <outfile>rpgam.exe</outfile>  <errTitle></errTitle>  <cmdLine></cmdLine>  <chdir>.</chdir>  <priority>normal</priority>  <downloadUrl>http://www.rpgaudiomixer.com/</downloadUrl>  <supportUrl></supportUrl>  <customProcName>false</customProcName>  <stayAlive>false</stayAlive>  <manifest></manifest>  <icon></icon>  <jre>    <path></path>    <minVersion>1.5.0</minVersion>    <maxVersion></maxVersion>    <jdkPreference>preferJre</jdkPreference>  </jre>  <splash>    <file>..imagessplash.bmp</file>    <waitForWindow>true</waitForWindow>    <timeout>60</timeout>    <timeoutErr>true</timeoutErr>  </splash></launch4jConfig>

然后是NSIS脚本rpgam-setup.nsis。可以使用VERSION参数来帮助命名文件。

; The name of the installerName "RPG Audio Mixer"!ifndef VERSION    !define VERSION A.B.C!endif; The file to writeoutfile "..distinstallerswindowsrpgam-${VERSION}.exe"; The default installation directoryInstallDir "$PROGRAMFILESRPG Audio Mixer"; Registry key to check for directory (so if you install again, it will ; overwrite the old one automatically)InstallDirRegKey HKLM "SoftwareRPG_Audio_Mixer" "Install_Dir"# create a default section.section "RPG Audio Mixer"    SectionIn RO    ; Set output path to the installation directory.    SetOutPath $INSTDIR    File /r "..distlayoutwindows"    ; Write the installation path into the registry    WriteRegStr HKLM SOFTWARERPG_Audio_Mixer "Install_Dir" "$INSTDIR"    ; Write the uninstall keys for Windows    WriteRegStr HKLM "SoftwareMicrosoftWindowsCurrentVersionUninstallRPGAudioMixer" "DisplayName" "RPG Audio Mixer"    WriteRegStr HKLM "SoftwareMicrosoftWindowsCurrentVersionUninstallRPGAudioMixer" "UninstallString" '"$INSTDIRuninstall.exe"'    WriteRegDWORD HKLM "SoftwareMicrosoftWindowsCurrentVersionUninstallRPGAudioMixer" "NoModify" 1    WriteRegDWORD HKLM "SoftwareMicrosoftWindowsCurrentVersionUninstallRPGAudioMixer" "NoRepair" 1    WriteUninstaller "uninstall.exe"    ; read the value from the registry into the $0 register    ;readRegStr $0 HKLM "SOFTWAREJavaSoftJava Runtime Environment" CurrentVersion    ; print the results in a popup message box    ;messageBox MB_OK "version: $0"sectionEndSection "Start Menu Shortcuts"  CreateDirectory "$SMPROGRAMSRPG Audio Mixer"  CreateShortCut "$SMPROGRAMSRPG Audio MixerUninstall.lnk" "$INSTDIRuninstall.exe" "" "$INSTDIRuninstall.exe" 0  CreateShortCut "$SMPROGRAMSRPG AUdio MixerRPG Audio Mixer.lnk" "$INSTDIRrpgam.exe" "" "$INSTDIRrpgam.exe" 0SectionEndSection "Uninstall"    ; Remove registry keys    DeleteRegKey HKLM "SoftwareMicrosoftWindowsCurrentVersionUninstallRPGAudioMixer"    DeleteRegKey HKLM SOFTWARERPG_Audio_Mixer    ; Remove files and uninstaller    Delete $INSTDIRrpgam.exe    Delete $INSTDIRuninstall.exe    ; Remove shortcuts, if any    Delete "$SMPROGRAMSRPG Audio Mixer*.*"    ; Remove directories used    RMDir "$SMPROGRAMSRPG Audio Mixer"    RMDir "$INSTDIR"SectionEnd

蚂蚁整合

我的Ant构建文件(build.xml)中有一些目标可以处理上述问题。首先,我打电话给Ant导入launch4j的Ant任务:

<property name="launch4j.dir" location="vendor/launch4j" /><taskdef name="launch4j"     classname="net.sf.launch4j.ant.Launch4jTask"    classpath="${launch4j.dir}/launch4j.jar:${launch4j.dir}/lib/xstream.jar" />

然后,我有了一个用于创建包装可执行文件的简单目标:

<target name="executable-windows" depends="jar" description="Create Windows executable (EXE)">    <launch4j configFile="scripts/launch4j.xml" outfile="${exeFile}" /></target>

制作安装程序的另一个目标:

<target name="installer-windows" depends="executable-windows" description="Create the installer for Windows (EXE)">    <!-- Lay out files needed for building the installer -->    <mkdir dir="${windowsLayoutDirectory}" />    <copy file="${jarFile}" todir="${windowsLayoutDirectory}" />    <copy todir="${windowsLayoutDirectory}/lib">        <fileset dir="${libraryDirectory}" />        <fileset dir="${windowsLibraryDirectory}" />    </copy>    <copy todir="${windowsLayoutDirectory}/icons">         <fileset dir="${iconsDirectory}" />    </copy>    <copy todir="${windowsLayoutDirectory}" file="${exeFile}" />    <mkdir dir="${windowsInstallerDirectory}" />    <!-- Build the installer using NSIS -->    <exec executable="vendor/nsis/makensis.exe">        <arg value="/DVERSION=${version}" />        <arg value="scripts/rpgam-setup.nsi" />    </exec></target>

上面的部分只是将安装程序所需的文件复制到一个临时位置,而下半部分执行使用所有脚本制作安装程序的脚本。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/427976.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号