为多平台 SWT 应用程序为每个平台构建一个 jar [英] Build one jar per platform for multi-platform SWT application

查看:22
本文介绍了为多平台 SWT 应用程序为每个平台构建一个 jar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于这个主题已经有几个问题,但似乎没有一个能正常工作.

There are a couple of questions to this topic already, but none of them seem to work properly.

以下是它们的列表:

我的要求是构建一个 ant 脚本,该脚本创建每个平台一个 jar,即一个用于 Windows x86,一个用于 Windows x64、Linux x86/x64 等.

My requirement is to build an ant script that creates one jar per platform, i.e. one for Windows x86, one for Windows x64, Linux x86/x64 and so on.

有人有更深入的了解吗?

Does anyone have any further insight?

使用上述方法,我无法产生可行的解决方案.它要么以未自动加载 SWT jar 文件或未包含在类路径中而结束.

Using the aforementioned methods, I was not able to produce a workable solution. It either ended with the SWT jar file not automatically being loaded or it not being included in the classpath.

如果有人能提出一个可行的例子(最好包括完整的源代码),那就太好了!

If someone can come up with a working example (ideally including the complete source code), that would be grand!

推荐答案

对,所以我终于想出了一个在三个平台上都成功测试过的解决方案.

Right, so I finally came up with a solution that I successfully tested on three platforms.

两个神奇的组件是 jar-in-jar-loader 和一个合适的构建脚本.

The two magic components are the jar-in-jar-loader and a proper build script.

可以在此处找到带有注释的构建脚本:

The build script with comments can be found here:

<project name="RandomApp" basedir="." default="clean-build">

    <property name="src.dir" value="src" />

    <!-- Define the necessary paths -->
    <property name="build.dir" value="bin_temp" />
    <property name="lib.dir" value="lib" />
    <property name="lib.deploy.dir" value="lib_swt" />
    <property name="classes.dir" value="${build.dir}/classes" />
    <property name="jar.dir" value="${build.dir}/jar" />
    <property name="img.dir" value="img" />
    <property name="res.dir" value="res" />

    <!-- Define the main class -->
    <property name="main-class" value="org.baz.desktop.randomapp.gui.RandomApp" />

    <path id="base-classpath">
        <fileset dir="${lib.dir}" includes="**/*.jar" />
    </path>

    <!-- Define the class path -->
    <path id="classpath">
        <fileset dir="${lib.dir}" includes="**/*.jar" />
        <fileset dir="${lib.deploy.dir}" includes="**/swt_win32_x64.jar" />
    </path>

    <!-- Clean previously built files -->
    <target name="clean">
        <delete dir="${build.dir}" />
    </target>

    <!-- Compile the project -->
    <target name="compile">
        <mkdir dir="${classes.dir}" />
        <javac srcdir="${src.dir}" destdir="${classes.dir}" classpathref="classpath" includeantruntime="false" />
    </target>

    <macrodef name="createclasspath">
        <attribute name="name" />
        <attribute name="swtlib" />
        <sequential>
            <pathconvert property="@{name}.classpath" pathsep=" ">
                <path refid="base-classpath" />
                <fileset dir="${lib.deploy.dir}" includes="**/swt_@{swtlib}.jar" />
                <mapper>
                    <chainedmapper>
                        <flattenmapper />
                        <globmapper from="*.jar" to="*.jar" />
                    </chainedmapper>
                </mapper>
            </pathconvert>
        </sequential>
    </macrodef>

    <!-- Define classpath and create the jar folder -->
    <target name="pre_jar" depends="compile">
        <!-- Linux 32bit -->
        <createclasspath name="win86" swtlib="win32_x86" />
        <!-- Linux 64bit -->
        <createclasspath name="win64" swtlib="win32_x64" />
        <!-- Windows 32bit -->
        <createclasspath name="linux86" swtlib="linux_gtk_x86" />
        <!-- Windows 64bit -->
        <createclasspath name="linux64" swtlib="linux_gtk_x64" />
        <!-- MacOS 32bit -->
        <createclasspath name="macos86" swtlib="macos_x86" />
        <!-- MacOS 64bit -->
        <createclasspath name="macos64" swtlib="macos_x64" />

        <mkdir dir="${jar.dir}" />
    </target>

    <macrodef name="createjar">
        <attribute name="swtlib" />
        <attribute name="swtclasspath" />
        <sequential>
            <jar destfile="${jar.dir}/${ant.project.name}_@{swtlib}.jar" basedir="${classes.dir}">
                <manifest>
                    <attribute name="Main-Class" value="org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader" />
                    <attribute name="Rsrc-Main-Class" value="${main-class}" />
                    <attribute name="Class-Path" value="." />
                    <attribute name="Rsrc-Class-Path" value="./ @{swtclasspath}" />
                </manifest>

                <zipgroupfileset dir="${lib.dir}" includes="**/jar-in-jar-loader.jar" />
                <zipfileset dir="${lib.deploy.dir}" includes="**/swt_@{swtlib}.jar" />
                <zipfileset dir="${lib.dir}" includes="**/*.jar" excludes="**/jar-in-jar-loader.jar" />
            </jar>
        </sequential>
    </macrodef>

    <!-- Create the jar files -->
    <target name="jar" depends="pre_jar">
        <!-- Linux 32bit -->
        <createjar swtlib="linux_gtk_x86" swtclasspath="${linux86.classpath}" />
        <!-- Linux 64bit -->
        <createjar swtlib="linux_gtk_x64" swtclasspath="${linux64.classpath}" />
        <!-- Windows 32bit -->
        <createjar swtlib="win32_x86" swtclasspath="${win86.classpath}" />
        <!-- Windows 64bit -->
        <createjar swtlib="win32_x64" swtclasspath="${win64.classpath}" />
        <!-- MacOS 32bit -->
        <createjar swtlib="macos_x86" swtclasspath="${macos86.classpath}" />
        <!-- MacOS 64bit -->
        <createjar swtlib="macos_x64" swtclasspath="${macos64.classpath}" />
    </target>

    <target name="clean-build" depends="clean,jar" />

</project>

它的主要作用是定义一个基类路径没有任何 SWT 库.然后它使用基础类路径创建平台特定的类路径并添加适当的平台 SWT 库.

What it basically does is define a base classpath without any SWT library. Then it creates platform specific classpaths using the base one and adding the appropriate platform SWT library.

jar 任务然后使用这些类路径和 jar-in-jar-loader 为每个平台创建一个单独的 jar.

The jar task then creates a separate jar for each platform using these classpaths and the jar-in-jar-loader.

就是这样,一种为每个(支持的)平台生成 jar 的全自动方式.

And that's it, a fully automated way of generating jars for each (supported) platform.

我创建了一个示例项目,人们可以下载和测试.这是多平台应用程序的简单起点.

I've created an example project that people can download and test out. It's an easy starting point for a multi-platform application.

https://www.dropbox.com/s/ianrbl4bn0fmsdi/SWTApplication.7z

更新:

通过正确使用 macrodef,我已经成功地显着缩短了 ant 脚本 :)

I've managed to significantly shorten the ant script by making proper use of macrodef :)

这篇关于为多平台 SWT 应用程序为每个平台构建一个 jar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆