【问题标题】:Bulding an multi-platform SWT application using Ant使用 Ant 构建多平台 SET 应用程序
【发布时间】:2012-06-05 18:37:11
【问题描述】:

我正在编写一个可在 Windows(32/64 位)和 Mac OSX(32/64 位)上使用的 SWT 应用程序。

除了 JRE,我还依赖 here 找到的 SWT 库。根据我的目标平台(如上所述),我可以找到四个版本的 SWT 库。

在构建我的应用程序时,如何使用正确的 SWT Jar 进行编译?如果可能的话,我想尽量避免对 Jar 版本、平台和架构进行硬编码。 SWT Jars 的命名如下:

  • swt-win32-x86_64.jar
  • swt-win32-x86_32.jar
  • swt-macosx-x86_32.jar
  • swt-macosx-x86_64.jar

(我的项目将是一个开源项目。我希望人们能够下载源代码并构建它,因此我考虑在源代码分发中包含所有四个版本的 SWT Jars。我希望这是依赖第三方库发布代码的正确方法。)

谢谢大家。

【问题讨论】:

标签: java ant build jar swt


【解决方案1】:

我尝试过这样完成它:我安装了 Ant Contrib 任务,因为它支持 if 语句。我修改了构建文件以用于检测操作系统平台和架构。

编译时,它使用我lib.dir 中的所有 4 个 SWT Jars,但编译后,它只将必要的 SWT Jar 复制到我的构建目录。我想这将使我的最终 ZIP 的大小比保留所有四个 JAR 小得多。

<target name="copy" depends="compile">
    <if>
    <os family="windows"/>
    <then>
        <exec dir="." executable="cmd" outputproperty="command.ouput">
            <arg line="/c SET ProgramFiles(x86)"/>
        </exec>
        <if>
            <contains string="${command.ouput}" substring="Program Files (x86)"/>
            <then>
                <copy file="${lib.dir}/swt-win32-x86_64.jar" tofile="${jar.dir}/SWT.jar"/>
            </then>
            <else>
                <copy file="${lib.dir}/swt-win32-x86_32.jar" tofile="${jar.dir}/SWT.jar"/>
            </else>
        </if>
    </then>
    <elseif>
        <os family="unix"/>
        <then>
            <exec dir="." executable="/bin/sh" outputproperty="command.ouput">
                <arg line="/c uname -m"/>
            </exec>
            <if>
                <contains string="${command.ouput}" substring="_64"/>
                <then>
                    <copy file="${lib.dir}/swt-macosx-x86_64.jar" tofile="${jar.dir}/SWT.jar"/>
                </then>
                <else>
                    <copy file="${lib.dir}/swt-macosx-x86_32.jar" tofile="${jar.dir}/SWT.jar"/>
                </else>
            </if>
        </then>
    </elseif>
    </if>
</target>

到目前为止,这似乎有效。我将对其进行更多测试并添加评论。

【讨论】:

  • 你说When compiling, it uses all the 4 SWT Jars from my lib.dir 我能感觉到的问题是你可能会选择一个可能与目标机器不兼容的版本(取决于你在类路径中提到它们的顺序)。例如,为 macosx 选择 win32 jar,或为 win32 选择 win64 jar。
  • 你说得有道理。我将尝试文件的顺序及其效果并在此处发布。
猜你喜欢
  • 1970-01-01
  • 2014-06-15
  • 2011-11-09
  • 1970-01-01
  • 2012-05-12
  • 2011-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多