java – 为每个平台构建一个jar用于多平台SWT应用程序
作者:互联网
这个主题已经有几个问题,但它们似乎都没有正常工作.
以下是它们的列表:
> Bulding an multi-platform SWT application using Ant
> Build multi-platform executable for a SWT application (Eclipse)
> Build multi-platform executable for a SWT application using maven
> SWT jar for different platform
> Create cross platform Java SWT Application
我的要求是构建一个ant脚本,每个平台创建一个jar,即一个用于Windows x86,一个用于Windows x64,Linux x86 / x64等等.
有没有人有任何进一步的见解?
使用上述方法,我无法生成可行的解决方案.它要么以自动加载的SWT jar文件结束,要么不包含在类路径中.
如果有人能够提出一个有效的例子(理想情况下包括完整的源代码),那就太棒了!
解决方法:
是的,所以我终于想出了一个我在三个平台上成功测试的解决方案.
两个神奇的组件是jar-in-jar-loader和一个正确的构建脚本.
可以在此处找到带注释的构建脚本:
<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库.
然后,jar任务使用这些类路径和jar-in-jar-loader为每个平台创建一个单独的jar.
就是这样,一种为每个(支持)平台生成jar的全自动方式.
我创建了一个人们可以下载和测试的示例项目.这是多平台应用程序的一个简单起点.
https://www.dropbox.com/s/ianrbl4bn0fmsdi/SWTApplication.7z
更新:
我通过正确使用macrodef设法显着缩短了蚂蚁脚本:)
标签:java,jar,ant,swt,multiplatform 来源: https://codeday.me/bug/20190624/1279009.html