问题描述
有几个问题,这个话题了,但他们都不能够正常工作。
There are a couple of questions to this topic already, but none of them seem to work properly.
下面就是其中的列表:
- 一个多平台的SWT应用程序
- 针对不同的平台
- Bulding an multi-platform SWT application using Ant
- Build multi-platform executable for a SWT application (Elipse)
- Build multi-platform executable for a SWT application using maven
- SWT jar for different platform
- Create cross platform Java SWT Application
我的要求是建立创建的一个每个平台罐子,即一个用于Windows的x86,一个用于Windows的x64,Linux的在x86 / x64等一个Ant脚本。
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.
如果有人能拿出一个工作示例(最好包括完整的源代码code),这将是盛大!
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-在罐子装载机和适当的构建脚本。
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-在罐装载每个平台单独的罐子。
The jar
task then creates a separate jar for each platform using these classpaths and the jar-in-jar-loader.
就是这样,产生罐子每个(支持)平台的一个完全自动化的方式。
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.
更新
我已经设法通过适当地使用 macrodef
以显著缩短ant脚本:)
I've managed to significantly shorten the ant script by making proper use of macrodef
:)
这篇关于构建以平台一罐子多平台SWT应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!