Bulding an multi-platform SWT application using An

2019-09-12 17:33发布

I'm writing an SWT application which can be used on Windows (32/64 bit) and Mac OSX (32/64 bit).

Apart from the JRE I rely on the SWT library found here. I can find four versions of the SWT library depending upon my target platforms (as mentioned above).

When building my application, how can I compile using the correct SWT Jar? If possible, I'd like to try and avoid hard-coding the Jar version, platform and architecture. The SWT Jars are named like this:

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

(My project will be an open source project. I'd like people to be able to download the source and build it and therefore I've thought of including all the four versions of the SWT Jars in the source distribution. I hope this is the correct approach of publishing code relying on third-part libraries.)

Thanks everyone.

1条回答
2楼-- · 2019-09-12 17:48

I've tried to accomplish it like this: I've installed the Ant Contrib tasks because it supports if statements. I've modified my build file to use to detect the OS platform and architecture.

When compiling, it uses all the 4 SWT Jars from my lib.dir but after compilation, it copies over only the necessary SWT Jar to my build directory. I guess this would keep my size of my final ZIP much smaller than keeping all four JARs.

<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>

This seems to work so far. I'll test it some more and add a comment.

查看更多
登录 后发表回答