[Eclipse插件开发指南]2.4 构建一个产品

构建一个商业产品是为了在最终交付给用户一个可安装在用户环境的形式。你可以以多种不同的方式去构建产品,包括手动或者是用Windows batch script、UNIX shell脚本、Apache Ant script。在最终交付的时候就是一个单独的压缩文件或者是一个独立可执行文件。为了这个目的,Favorites plug-in(译者注:该书的一个示例项目)将源码作为一个单独压缩的zip文件交互给用户。

2.4.1 手工构建

手动构建涉及到运行一个Eclipse导出(Export)向导页,填写一些字段以及点击结束(Finish)按钮。选择文件(File)>导出(Export)去运行想要的导出(Export)向导页。第一个向导页页面(图2-14).选择Deployable plug-ins and fragments,然后点击下一步(Next)按钮。

图2-14 导出(Export)向导页1之导出类型的选择

image

在第二个向导页的页面(图2-15),选择将要被导出的插件,输入zip文件的名字以及选择显示的选项。另外,指定导出操作的Ant脚本在com.qualityeclipse.favorites项目中build-favorites.xml文件,然后点击结束(Finish)。

图2-15 导出(Export)向导页面2之指定zip文件内容

image

以创建的zip文件包括一个单独的JAR插件包(在Eclipse3.1中一个插件可以被部署成一个单独的JAR文件)

plugins/com.qualityeclipse.favorites_1.0.0.jar

而插件JAR文件包括了插件和源码就和导出(Export)向导页指定的一样:

favorites.jar favoritessrc.zip plugin.xml icons/sample.gif META-INF/MANIFEST.MF

不幸的是,这个手动过程很容易出错。手动构建成功需要一次或者两次,但是要是在公司有不同的人需要构建产品呢?当产品规模在扩大将会发生什么呢?一个商业产品需要一个可重复以及稳定的方法去构建它。

2.4.2 用Apache Ant去构建

一个Apache Ant脚本提供了可靠的、灵活的以及可重复的构建商业插件项目的过程,为了创建一个Ant脚本,这之前我们要做多一点的工作,但是相比手动构建产品来说,随着时间的推移,它将更少的出错误,在第19章将做介绍。

Eclipse能够生产一个简单的Ant脚本。在之前部分用导出(Export)向导页生成的build-favorites.xml

<?xml version="1.0" encoding="UTF-8"?> <project default="plugin_export" name="build"> <target name="plugin_export"> <pde.exportPlugins destination="/Build/QualityEclipse" exportSource="true" exportType="zip" filename="FavoritesPlugin.zip" plugins="com.qualityeclipse.favorites" source="1.3" target="1.2" useJARFormat="true" /> </target> </project>

之前简单的脚本工作的很好;但是不幸的是,pde.exportPlugins以及其它的pde.export*尝试着异步的以及不能够用前端完全无外设的环境(在bugs.eclipse.org/bugs/show_bug.cgi?id=58413看Bugzilla登记的条目58413) 去创建它相比简单的脚本是很困难的。

如果你想要自己的构建脚本去做更多的,就需要跟复杂的Ant脚本。更多的Ant和构建脚本,去看19章 构建一个产品。

<?xml version="1.0" encoding="UTF-8"?> <project default="plugin_export" name="build"> <target name="plugin_export"> <!-- Define build directories --> <property name="build.root" location="/Build/QualityEclipse" /> <property name="build.temp" location="${build.root}/temp" /> <property name="build.out" location="${build.root}/product" /> <!-- Create build directories --> <delete dir="${build.temp}" /> <mkdir dir="${build.temp}" /> <mkdir dir="${build.out}" /> <!-- Read the MANIFEST.MF --> <copy file="META-INF/MANIFEST.MF" todir="${build.temp}" /> <replace file="${build.temp}/MANIFEST.MF"> <replacefilter token=":=" value="=" /> <replacefilter token=":" value="=" /> <replacetoken>;</replacetoken> <replacevalue> </replacevalue> </replace> <property file="${build.temp}/MANIFEST.MF"/> <!-- Plugin locations --> <property name="plugin.dir" value= "com.qualityeclipse.favorites_${Bundle-Version}" /> <property name="plugin.files" location= "${build.temp}/files/${plugin.dir}" /> <property name="plugin.<a name="iddle1543"></a>jar" location= "${build.temp}/jars/plugins/${plugin.dir}.jar" /> <property name="product.zip" value= "${build.out}/Favorites_v${Bundle-Version}.zip" /> <!-- Assemble the files --> <mkdir dir="${plugin.files}" /> <jar destfile="${plugin.files}/favorites.jar"> <fileset dir="bin" /> </jar> <jar destfile="${plugin.files}/favoritessrc.zip"> <fileset dir="src" /> </jar> <copy todir="${plugin.files}"> <fileset dir="." includes="META-INF/MANIFEST.MF" /> <fileset dir="." includes="plugin.xml" /> <fileset dir="." includes="icons/*.gif" /> </copy><a name="d0e12516"></a> <!-- Assemble plug-in jar --> <mkdir dir="${build.temp}/jars/plugins" /> <zip destfile="${plugin.jar}"> <zipfileset dir="${plugin.files}"> <include name="**/*.*" /> </zipfileset> </zip> <!-- Assemble the product zip --> <zip destfile="${product.zip}"> <fileset dir="${build.temp}/jars" /> </zip> </target> </project>

build-favorites.xml上点击右键,选择Run Ant…(图2-16)去执行这个Ant脚本。当Ant向导页显示的时候,点击JRE标签以及选择Run in the same JRE as the workspace选项(图2-17).点击运行(Run)按钮去构建产品。

图2-16 build.xml弹出菜单

image

图2-17 And向导页

image

附语:如果你的Ant脚本用的是Eclipse指定的Ant任务,比如pde.exportPlugins,为了让你的Ant脚本能够执行你必须选择Run in the same JRE as the workspace选项。

本文系eclipselight.org(日食之光)原创文章,转载请注明出处。

固定链接:http://www.eclipselight.org/general/658/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值