Eclipse 简介和插件开发示例
Eclipse 是一个很让人着迷的开发环境,它提供的核心框架和可扩展的插件机制给广大的程序员提供了无限的想象和创造空间。目前网上流传相当丰富且全面的开发工具方面的插件,但是Eclipse已经超越了开发环境的概念,可以想象Eclipse将成为未来的集成的桌面环境。目前的Eclipse本身就具备资源管理和外部程序的功能,加上无所不能的插件,将构成一个丰富多彩的工作环境而不仅仅是一个IDE。
1.Eclipse简介和插件开发
Eclipse 是一个很让人着迷的开发环境,它提供的核心框架和可扩展的插件机制给广大的程序员提供了无限的想象和创造空间。目前网上流传相当丰富且全面的开发工具方面的插件,但是Eclipse已经超越了开发环境的概念,可以想象Eclipse将成为未来的集成的桌面环境。目前的Eclipse本身就具备资源管理和外部程序的功能,加上无所不能的插件,将构成一个丰富多彩的工作环境而不仅仅是一个IDE。对于程序员来说,没有什么比可以随心所欲的定制的工作环境更重要,你的决心,勇气和创造力在与别人分享成果的过程中一览无余。好了,你是不是心动了,如果你已经对Eclipse有一定的认识,那么,和我一起打造自己的个性化工作环境吧,首先我们一起开发一个天气预报的插件,然后我们打造属于自己的邮件快速监控功能。
以下的工作基于一定的前提,那就是你是一名Java程序员,你很欣赏并正开始使用Eclipse这个超酷的工作环境,别忘了下载最新版的Eclipse3.0,本文基于Eclipse3.0开发。
2.天气预报插件
如果你已经厌倦了总是要登录某些网站从相关网页上获取信息,这里有一个让你通过Eclipse快速获取信息的新方法。让我们从头开始,做一个属于自己的天气预报插件吧,你的Eclipse将具有天气预报功能,是不是很酷呢?
在这一部分,我们将要实现一个Eclipse插件,他可以在任何我们想知道的时候通过简单的点击鼠标告诉我们本地区的天气预报,这当然很刺激。对于一个程序员而言,事情就应该如此。让我们开始吧,我们首先要定义一个插件,把他加到菜单和工具栏中。对于没有插件开发经验的你,可以参考《开发 Eclipse 插件》,树立基本的插件开发意识,当然,本文将详细的辅助你完成这一创造性的工作。
2.1最基础的插件
打开菜单 File -> New-> Other ->Plug-in Project,输入项目名称,next出现对话框,只要在插件名处输入"muplugin",next 以后选择 "Hello,World"的插件模板你可以直接新建一个名为myplugin的最简单的插件,但其实我们的天气预报并不比它复杂多少,建完改插件以后的效果如下图。
![](http://www.chinaitlab.com/www/imgfiles/2004.12.20.10.53.44.1.gif)
现在,将项目作为运行时工作台运行(run - run as runtime workbench),在一个全新的Eclipse窗口中,通过点击菜单 sample menu 的sample Action或者工具栏中的圆形Eclipse 图标,你将看到如下效果的对话框。
![](http://www.chinaitlab.com/www/imgfiles/2004.12.20.10.53.54.2.gif)
到此为止,天气预报插件的原始版做成了,通过修改plugin.xml,我们将菜单改成中文形式,需要修改的地方就2处,详见表格。
<actionSet label="Sample Action Set" visible="true" id="myplugin.actionSet">
<menu label="我的空间" id="sampleMenu">
<separator name="sampleGroup">
</separator>
</menu>
<action label="天气预报" icon="icons/sample.gif" class="myplugin.actions.SampleAction"
tooltip="Hello, Eclipse world" menubarPath="sampleMenu/sampleGroup"
toolbarPath="sampleGroup" id="myplugin.actions.SampleAction">
</action>
此时在运行时工作台,我们的菜单已经改变。
![](http://www.chinaitlab.com/www/imgfiles/2004.12.20.10.54.9.3.gif)
2.2用VisualEditer制作天气预报对话框
虽然菜单是天气预报,但是我们需要的不是hello Eclispe对话框,我们需要的是告诉我们天气的对话框,当然需要我们从头开始,于是我们需要重新构建一个对话框,这个就需要 Visual Editor来帮助进行界面的开发。我们将使用Visual Editor实现一个Swing对话框,当然只用VE做一个对话框是有点大材小用,但是作为起点,已经合适了。
首先构建Visual Editer开发环境(读者可参考相关资料),当一切准备齐全,鼠标右键点击PackgeExpoler中的 "muplugin.actions"java文件,从弹出式菜单中选择 new->other->VisualClass,新建一个可视化的类,弹出界面如下图:
![](http://www.chinaitlab.com/www/imgfiles/2004.12.20.10.54.20.4.gif)
选择next,然后在name中输入WeatherDialog,这个就是我们用来显示天气预报的dialog
选择该对话框的超类为javax.swing.JDiaog,点击Finish按钮。等待一段时间后,我们的对话框就基本生成了,鼠标点击左上角图标,直接输入天气预报就是对话框的标题,同时 我们可以看到左侧的VisualEditor面板。
![](http://www.chinaitlab.com/www/imgfiles/2004.12.20.10.54.29.5.gif)
然后我们将该对话框于与刚才的天气预报菜单连接找到SampleAction的run函数,如下所示:
public void run(IAction action) {
MessageDialog.openInformation(
window.getShell(),"Myplugin Plug-in", "Hello, Eclipse world");
}
替换成如下代码
public void run(IAction action)
{
WeatherDialog wd=new WeatherDialog();
wd.setSize(400, 335);
wd.show();
}
此时,点击菜单运行,我们的对话框看起来象这个样子,在此基础上我们还要在上面增加天气预报信息。
![](http://www.chinaitlab.com/www/imgfiles/2004.12.20.10.54.38.6.gif)
2.3增加天气预报功能
![](http://www.chinaitlab.com/www/imgfiles/2004.12.20.10.54.49.7.gif)
下面的部分是重点,我们将使用具有解析Html功能的Swing组件JEditPane,来获取网络上的现成的天气预报信息,根据上图,从 VisualEditor的面板中Swing Components组点击JEditPane,加入到对话框中。并修改对话框代码使得最终的代码如下:
/*
* Created on 2004-9-23
* */
package myplugin;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import javax.swing.JDialog;
import javax.swing.JEditorPane;
/**
* <p>Title: WatherDialog</p>
* <p>Description: 这个是对话框类,用于显示指定城市的当天的天气预报</p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company:UF SOFT</p>
* @author 赵勇
* @version 1.0
*/
public class WatherDialog extends JDialog
{
String city="北京";
private JEditorPane jEditorPane = null;
/**
* This method initializes
* /
public WatherDialog(String city)
{
super();
this.city=city;
initialize();
}
/**
* This method initializes this
* @return void
*/
private void initialize()
{
this.setContentPane(getJEditorPane());
try
{
//构建URL对象
URL url =new URL("http://weather.news.sina.com.cn//cgi-bin/figureWeather/simpleSearch.cgi?city="+city);
String temp="";
BufferedReader in
= new BufferedReader(new InputStreamReader(url.openStream()));
//使用openStream得到一输入流并由此构造一个BufferedReader对象
String inputLine;
//从输入流不断的读数据,直到读完为止
while ((inputLine = in.readLine()) != null)
temp=temp+inputLine+"/n";
//关闭输入流
in.close();
String weather
=temp.substring ( temp.indexOf( "<body"),
temp.lastIndexOf( "body>")+5);
this.jEditorPane .setText(weather);
}
catch (Exception e)
{
e.printStackTrace();
}
this.setTitle("天气预报");
this.setSize(400, 166);
}
/**
* This method initializes jEditorPane
*
* @return javax.swing.JEditorPane
*/
private JEditorPane getJEditorPane()
{
if (jEditorPane == null)
{
jEditorPane = new JEditorPane();
jEditorPane.setContentType( "text/html");
}
return jEditorPane;
}
} // @jve:decl-index=0:visual-constraint="70,19"
以上代码中最关键的部分就是对话框中的JEditorPane对象,在初始化时,从一个URL 获取天气预报信息,表现为Html标记片段,不用解析,直接调用JEditorPane的setText 方法,就可以将Html格式的信息直接按解析过的方式显示,也就是天气预报信息了,
此时Action中的调用需要做修改
public void run(IAction action)
{
WeatherDialog wd=new WeatherDialog("北京");
wd.setSize(400, 335);
wd.show();
}
现在以运行时工作台的方式运行,点击天气预报菜单,可以看到下图:
![](http://www.chinaitlab.com/www/imgfiles/2004.12.20.10.55.1.8.gif)
如果你在上海或者其他城市,试着修改city参数为"上海",再次运行,你将发现,你仍然能够得到该城市的天气预报(这里我们从网站上提取的信息,有点投机取巧了)。值得注意的是,Xmethod网站提供了一个天气预报的WebService,可惜只有美国的城市,不然我们可以使用Web Service调用获取天气预报,将会更酷。
现在运行是工作台已经具备了天气预报的功能,还需要更进一步,将改插件导出发布,拷贝到Eclipse根目录的plugins目录中,重新启动(具体参见Eclipse帮助)。现在你自己的Eclipse,就具备了天气预报的功能,只要你点击鼠标,就可以在编程之余轻松的获取天气信息。 除非你的老板认为你在工作时间随时了解天气情况不是一个好主意,我认为你完全可以将这个插件纳入个人收藏的插件之列。你也可以在此基础上扩展,增加一些配置文件和属性设置,定制出满足自己要求的插件。如果能够增加信息的自动过滤和筛选,那将是一次很愉快的体验,如果你有时间和兴趣,不妨一试。
3.邮件快速监控插件
现在你的工作因为Eclipse而更加惬意,更具创造力,那么你还有什么不满?你是否厌倦了各种邮件客户端随时随地的骚扰你呢?你希望你在高兴的时候适时的了解一下邮件的概况?好了,既然想到了为什么犹豫呢,因为你是程序员,你就是要用Eclipse享受完全DIY的乐趣。
3.1生成插件
本部分我们将在以上myplugin插件的基础上增加一个邮件过滤显示的对话框,类似的我们通过VisualEditer创建一个名为MailDialog的对话框,并增加一个JEditPane用来显示邮箱中我们关注的信息。
修改plugin.xml,增加一个"我的邮件"菜单
<action
label="邮件信息"
icon="icons/sample.gif"
class="myplugin.actions.MailAction"
tooltip="邮件信息"
menubarPath="sampleMenu/sampleGroup"
toolbarPath="sampleGroup"
id="myplugin.actions.MailAction">
</action>
现在,你知道要创建一个MailAction的Action类,并在在Run中增加如下代码
MailConfig mail=new MailConfig();
String popServer="server";
String popUser="zhaoyong";
String popPassword="1234";
//设置需要过滤的关键字:发件人和邮件主题
String [] strFrom=new String[] {"zhaoyong"};
String [] strSubject=new String[] {"测试"};
MailConfig[] mc =new MailConfig [] { mail };
MailDialog md=new MailDialog(mc);
System.err.println("run run run ") ;
md.setSize(400, 335);
md.show();
以上的代码编译不会通过,但是别着急,慢慢来,很快了。
3.2构建邮件监控对话框
当然你需要建立一个MailConfig类用来表示一个邮箱的具体设置已及相关信息,这里就不在累述说明,详情参见参考资料中的代码。需要说明的式MailConfig除了要记录一个邮箱的地址,用户名和密码外,还提供2个关键字数组,如果为空,不加过滤,如果关键字有值,系统会根据发件人和邮件标题中是否包含关键字来进行显示邮件信息,已保证你的绝对自由。
首先我们需要实现一个MailConfig类,表示邮件配置,每个MailConfig的对象代表一个邮件帐户,我们的系统将能显示多个邮箱的配置,每个MailConfig中使用一个数组来保存需要过滤的收件人和邮件地址。
MailConfig类的中的变量如下:
String popServer;
String popUser;
String popPassword;
//设置需要过滤的关键字:发件人和邮件主题
String [] strFrom;
String [] strSubject;
//是否显示邮件内容
boolean isViewContent=false;
同样,我们将使用一个对话框来显示邮件信息,MailDialog需要引用javaMail.jar,和activation.jar这两个类包,确保已经有这两个类包并加入到项目的类路径中。最后的MailDialog代码如下:
package myplugin;
import java.io.IOException;
import java.util.Properties;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.internet.InternetAddress;
import javax.swing.JDialog;
import javax.swing.JEditorPane;
import javax.swing.JTextPane;
/**
* @author zhaoyong
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
?/
public class MailDialog extends JDialog
{
private JEditorPane jEditorPane = null;
private JTextPane jTextPane = null;
//可以显示多个邮件配置
MailConfig[] mc= null;
/**
* This method initializes
* 构造函数
* @param mc : 需要显示的多个邮箱配置对象。
*/
public MailDialog(MailConfig[] mc)
{
super();
if(mc!=null)
this.mc = mc;
else
System.err.println("邮件配置错误!") ;
initialize();
}
/**
* This method initializes this
* 初始化
* @return void
*/
private void initialize()
{
try
{
//设定显示内容的面板
this.setContentPane(getJTextPane());
//取得所有的新邮件信息
String s= getAllMailInfo();
//将信息显示在对话框中
this.jTextPane .setText(s);
this.setTitle("邮件信息");
this.setSize(251, 100);
}
catch (Exception e)
{
//发生错误显示错误信息
this.jTextPane .setText(e.toString());
e.printStackTrace();
}
}
/**取得所有的邮箱的需要监控的邮件信息
*
* @return String
*/
private String getAllMailInfo()
{
String allMailInfo="";
if (mc.length <1)
allMailInfo="没有配置邮箱!";
else
{
for(int i=0;i<mc.length;i++)
{
//循环获取每个邮箱的邮件信息
allMailInfo=allMailInfo+getMailInfo(mc[i]);
}
}
//还没有收到相关的邮件
if (allMailInfo.trim().length() ==0)
allMailInfo="未检测到相关新邮件!";
return allMailInfo;
}
/*
*得到一个邮箱中满足条件的所有新邮件的字符串形式
**/
private String getMailInfo(MailConfig mc)
{
//最终输出的邮件信息
String mailInfo="";
//每个邮箱服务器上的Store和Folder对象
Store store=null;
Folder folder=null;
try
{
Properties props = System.getProperties();
//与邮件服务器生成一个Session
Session session = Session.getDefaultInstance( props,null);
//给出服务器,用户名,密码连接服务器
store = session.getStore("pop3");
store.connect(mc.getPopServer(), mc.getPopUser(),mc.getPopPassword());
//取得默认的邮件Folder
folder = store.getDefaultFolder();
if (folder == null)
throw new Exception("No default folder");
//取得收件箱
folder = folder.getFolder("INBOX");
if (folder == null)
throw new Exception("No POP3 INBOX");
//以只读方式打开收件箱
folder.open(Folder.READ_ONLY);
//获取所有新邮件并处理
Message[] msgs = folder.getMessages();
for (int i = 0; i < msgs.length; i++)
{
Message message= msgs[i];
//取得每封邮件的信息,需要引用MailConfig对象进行关键字过滤
mailInfo = mailInfo+ getMessageInfo( message,mc);
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
finally
{
//安全的关闭邮件服务器资源
try
{
if (folder!=null) folder.close(true);
if (store!=null) store.close();
}
catch (Exception ex2) {ex2.printStackTrace();}
}
return mailInfo;
}
/**
* 得到一封邮件的信息,需要根据MailConfig过滤
* @param mailInfo
* @param message
* @return 邮件信息
* @throws MessagingException
* @throws IOException
*/
private String getMessageInfo( final Message message ,final MailConfig mc)
throws MessagingException, IOException
{
//返回的改邮件信息
String mailInfo="";
String from=((InternetAddress)message.getFrom()[0]).getPersonal();
if (from==null)
from=((InternetAddress)message.getFrom()[0]).getAddress();
String subject=message.getSubject();
//如果满足过滤信息则显示,否则返回空
if(isElementinString(from,mc.getStrFrom())
||isElementinString(subject,mc.getStrSubject()) )
{
mailInfo=mailInfo+"发件人 : "+from+"/n";
mailInfo=mailInfo+"邮件主题 : "+subject+"/n";
mailInfo=mailInfo+"发送时间 : "+message.getSentDate() +"/n";
//如果显示内容,则打印内容
if(mc.isViewContent)
mailInfo=mailInfo+message.getContent() +"/n";
mailInfo=mailInfo+"------------------------------------/n";
}
return mailInfo;
}
private JTextPane getJTextPane()
{
if (jTextPane == null)
{
jTextPane = new JTextPane();
}
return jTextPane;
}
/**
* 判断目标关键字数组中是否有指定的字符串,进行过滤
* @param targetStr :
* @param keys :
* @return 如果有,返回true, 否则返回false
*/
private boolean isElementinString(String targetStr,String [] keys)
{
//没指定过滤条件,显示所有
if (keys==null)
return true;
//指定字符串为空,直接返回false
if (targetStr==null)
return false;
for(int i=0;i<keys.length ;i++)
{
if (targetStr.indexOf(keys[i])>-1)
return true;
}
return false;
}
}
// @jve:decl-index=0:visual-constraint="10,10"--说明,这是Visual Editor添加的控制信息
以上代码的注释已经保证你能够看清楚,这里就不加累述,有兴趣的可以自己试试,体验一切尽在掌握的快感。当然这个例子做的实在简单,因此也为你的进一步开发留有足够的余地。
3.3 打包和发布
到此,在mypulgin中增加了邮件信息菜单和对话框,系统的plugin.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.0"?>
<plugin
id="myplugin"
name="Myplugin Plug-in"
version="1.0.0"
provider-name=""
class="myplugin.MypluginPlugin">
<runtime>
<library name="myplugin.jar">
<export name="*"/>
</library>
<library name="lib/javaMail.jar">
<export name="*"/>
</library>
<library name="lib/activation.jar">
<export name="*"/>
</library>
</runtime>
<requires>
<import plugin="org.eclipse.ui"/>
<import plugin="org.eclipse.core.runtime"/>
</requires>
<extension
point="org.eclipse.ui.actionSets">
<actionSet
label="Sample Action Set"
visible="true"
id="myplugin.actionSet">
<menu
label="我的空间"
id="sampleMenu">
<separator
name="sampleGroup">
</separator>
</menu>
<action
label="天气预报"
icon="icons/sample.gif"
class="myplugin.actions.SampleAction"
tooltip="Hello, Eclipse world"
menubarPath="sampleMenu/sampleGroup"
toolbarPath="sampleGroup"
id="myplugin.actions.SampleAction">
</action>
<action
label="邮件信息"
icon="icons/sample.gif"
class="myplugin.actions.MailAction"
tooltip="邮件信息"
menubarPath="sampleMenu/sampleGroup"
toolbarPath="sampleGroup"
id="myplugin.actions.MailAction">
</action>
</actionSet>
</extension>
</plugin>
实际上,我们在一个插件中加入了2个功能,因此就实现了我们的开发环境的自我扩展和定制。同样,参考Eclipse的帮助,你可以轻松的再次将插件打包导出,并将其加入自己的Eclipse 的plugins目录(可能需要解压缩),或通过help菜单的Update选项进行安装,注意导出时需要选定相关的类包。重新启动,你将发现自己的IDE已经多了自己的菜单,开发环境已经随着自己的意愿在改变了,程序员天生的满足感油然而生。
现在,你可以在需要的时候点击菜单,了解你希望监控的邮件情况或者最近的天气情况,一切轻松的尽在掌握,Eclipse的插件,就是这样全能。
4.总结
那么,聪明的你有没有发现什么问题,对,上面的东西太粗糙,太简单了,你可以做进一步的优化设计和功能加强,比如,自己增加邮件配置文件而不是写在代码里面,动态监控弹出邮件预警(通过事先设定的紧急状态),你也许还会想起来很多的新的主意,比如我为什么不能有个能看电影的插件?或是Eclipse中飘出动听的音乐?别急,这些不一定需要你亲手去做,http://sourceforge.net/projects/rocexwang/ 有一个播放器插件,但是现在仅能播放音乐,国外已经有人用Eclipse开发游戏,也有人用Eclipse来做MIS系统的。http://www.matrix.org.cn/forum_view.asp?forum_id=25&view_id=10510 有一个国人开发的俄罗斯方块游戏,虽然简单了一点。当然,通过网址http://eclipse-plugins.2y.net/eclipse/index.jsp和http://www.eclipseplugincentral.com/你可以找到很多的插件,
使用Ant导出Eclipse RCP全攻略
虽然Eclipse RCP使用他的Product向导可以方便的导出软件,但对于统一的java build来说,使用ant或maven来完成这样的工作很为方便。现在把我使用的方法写在这里,以备忘。
1.开始前的准备(我使用的是Eclipse 3.3.2,也就是Eclipse europa)
- 当然是开发好的RCP工程,我这里使用com.rcpquickstart.helloworld;
- 下载eclipse-RCP-3.3.2-win32.zip和eclipse-RCP-3.3.2-delta-pack.zip,先解压eclipse-RCP-3.3.2-win32,再解压eclipse-RCP-3.3.2-delta-pack,并进行覆盖;如路径:D:/Apps-build/eclipse
- ant环境,暂时使用的是apache-ant-1.7.1,路径:D:/DevTools/apache-ant-1.7.1
- Java环境,使用的jdk1.6.0_19,路径:D:/DevTools/jdk1.6.0_12
2.在rcp工程(com.rcpquickstart.helloworld)上创建一个一个.product文件,这就不累述了,如helloworld.product
3.创建一个Project,导航栏右键-->New-->Project-->Plug-in Project-->Feature Project,工程名为com.rcpquickstart.helloworld.feature,在选择插件时将需要的插件都选上,当然com.rcpquickstart.helloworld是必须的。工程里里面两个文件,一个build.properties和feature.xml,feature.xml如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <feature
- id="com.newautovideo.impclient.feature"
- label="Impclient Feature"
- version="1.0.0">
- <description url="http://www.example.com/description">
- [Enter Feature Description here.]
- </description>
- <copyright url="http://www.example.com/copyright">
- [Enter Copyright Description here.]
- </copyright>
- <license url="http://www.example.com/license">
- [Enter License Description here.]
- </license>
- <plugin
- id="com.newautovideo.impclient"
- download-size="0"
- install-size="0"
- version="0.0.0"
- unpack="false"/>
- <plugin
- id="org.eclipse.core.runtime"
- download-size="0"
- install-size="0"
- version="0.0.0"
- unpack="false"/>
- <plugin
- id="org.eclipse.core.runtime.compatibility.auth"
- download-size="0"
- install-size="0"
- version="0.0.0"
- unpack="false"/>
- <plugin
- id="org.eclipse.gef"
- download-size="0"
- install-size="0"
- version="0.0.0"
- unpack="false"/>
- <plugin
- id="org.eclipse.ui"
- download-size="0"
- install-size="0"
- version="0.0.0"
- unpack="false"/>
- <plugin
- id="org.eclipse.ui.intro"
- download-size="0"
- install-size="0"
- version="0.0.0"
- unpack="false"/>
- <plugin
- id="org.eclipse.ui.views"
- download-size="0"
- install-size="0"
- version="0.0.0"
- unpack="false"/>
- </feature>
build.properties里的内容就一行:bin.includes = feature.xml
4.创建一个一般工程,名为com.rcpquickstart.helloworld.build,里面创建两个文件,build.properties和build.xml,这两个文件是最关键的文件,最终编译就靠这两个文件,也就是ant脚本。首先说build.properties,这个文件的原本是在一个官方例子中,如下:
- ###############################################################################
- # Copyright (c) 2003, 2006 IBM Corporation and others.
- # All rights reserved. This program and the accompanying materials
- # are made available under the terms of the Eclipse Public License v1.0
- # which accompanies this distribution, and is available at
- # http://www.eclipse.org/legal/epl-v10.html
- #
- # Contributors:
- # IBM Corporation - initial API and implementation
- ###############################################################################
- #####################
- # Parameters describing how and where to execute the build.
- # Typical users need only update the following properties:
- # baseLocation - where things you are building against are installed
- # bootclasspath - The base jars to compile against (typicaly rt.jar)
- # configs - the list of {os, ws, arch} configurations to build.
- #
- # Of course any of the settings here can be overridden by spec'ing
- # them on the command line (e.g., -DbaseLocation=d:/eclipse
- ############# PLUG-IN VERSIONS ######################
- #
- # Look in the plugins directory of your Eclipse
- # installation to determine the version numbers
- # the correct version numbers. These version numbers
- # are used to create the correct paths when launching
- # PDE Build.
- #
- #####################################################
- # Version of org.ecilpse.pdebuild
- pdeBuildPluginVersion=3.3.2.v20071019
- # Version of org.eclipse.equinox.launcher
- equinoxLauncherPluginVersion=1.0.1.R33x_v20080118
- ############# BASE LOCATION #########################
- #
- # Specify the directory of the base under which your
- # your build target is located. This directory should
- # contain the RCP Runtime Binary that you want to
- # compile against.
- #
- #####################################################
- ###编译的根目录
- base=D:/apps-build
- ############# ECLIPSE LOCATION ######################
- #
- # Specify the directory of the Eclipse installation
- # that will be used to execute PDE Build.
- #
- #####################################################
- ###Eclipse的根目录
- eclipseLocation=D:/DevTools/eclipse-jee-europa-winter-win32/eclipse
- ############# PRODUCT/PACKAGING CONTROL #############
- ###刚才创建的.product在工程中的位置
- product=/com.rcpquickstart.helloworld/helloworld.product
- runPackager=true
- #Set the name of the archive that will result from the product build.
- #archiveNamePrefix=
- # The prefix that will be used in the generated archive.
- ###目标文件夹的名称
- archivePrefix=helloworld
- # The location underwhich all of the build output will be collected.
- collectingFolder=${archivePrefix}
- # The list of {os, ws, arch} configurations to build. This
- # value is a '&' separated list of ',' separate triples. For example,
- # configs=win32,win32,x86 & linux,motif,x86
- # By default the value is *,*,*
- #configs = *, *, *
- ###根据系统来选择
- configs=win32, win32, x86
- # & /
- # win32,win32,x86_64 & /
- # win32,win32,wpf & /
- # linux, gtk, ppc & /
- # linux, gtk, x86 & /
- # linux, gtk, x86_64 & /
- # linux, motif, x86 & /
- # solaris, motif, sparc & /
- # solaris, gtk, sparc & /
- # aix, motif, ppc & /
- # hpux, motif, ia64_32 & /
- # macosx, carbon, ppc & /
- # macosx, carbon, x86 & /
- # macosx, cocoa, ppc & /
- # macosx, cocoa, x86 & /
- # macosx, cocoa, x86_64
- # By default PDE creates one archive (result) per entry listed in the configs property.
- # Setting this value to true will cause PDE to only create one output containing all
- # artifacts for all the platforms listed in the configs property.
- # To control the output format for the group, add a "group, group, group - <format>" entry to the
- # archivesFormat.
- #groupConfigurations=true
- #The format of the archive. By default a zip is created using antZip.
- #The list can only contain the configuration for which the desired format is different than zip.
- #archivesFormat=win32, win32, x86 - antZip& /
- # linux, gtk, ppc - antZip &/
- # linux, gtk, x86 - antZip& /
- # linux, gtk, x86_64 - antZip& /
- # linux, motif, x86 - antZip& /
- # solaris, motif, sparc - antZip& /
- # solaris, gtk, sparc - antZip& /
- # aix, motif, ppc - antZip& /
- # hpux, motif, PA_RISC - antZip& /
- # macosx, carbon, ppc - antZip
- #Allow cycles involving at most one bundle that needs to be compiled with the rest being binary bundles.
- allowBinaryCycles = true
- #Sort bundles depenedencies across all features instead of just within a given feature.
- flattenDependencies = true
- #Parallel compilation, requires flattenedDependencies=true
- #parallelCompilation=true
- #parallelThreadCount=
- #parallelThreadsPerProcessor=
- #Set to true if you want the output to be ready for an update jar (no site.xml generated)
- #outputUpdateJars = false
- #Set to true for Jnlp generation
- #codebase should be a URL that will be used as the root of all relative URLs in the output.
- #generateJnlp=false
- #jnlp.codebase=<codebase url>
- #jnlp.j2se=<j2se version>
- #jnlp.locale=<a locale>
- #jnlp.generateOfflineAllowed=true or false generate <offlineAllowed/> attribute in the generated features
- #jnlp.configs=${configs} #uncomment to filter the content of the generated jnlp files based on the configuration being built
- #Set to true if you want to sign jars
- #signJars=false
- #sign.alias=<alias>
- #sign.keystore=<keystore location>
- #sign.storepass=<keystore password>
- #sign.keypass=<key password>
- #Arguments to send to the zip executable
- zipargs=
- #Arguments to send to the tar executable
- tarargs=
- #Control the creation of a file containing the version included in each configuration - on by default
- #generateVersionsLists=false
- ############## BUILD NAMING CONTROL ################
- # The directory into which the build elements are fetched and where
- # the build takes place.
- ###打包的目标文件夹
- buildDirectory=d:/apps-build/build
- # Type of build. Used in naming the build output. Typically this value is
- # one of I, N, M, S, ...
- ###形成的zip文件前缀
- buildType=I
- # ID of the build. Used in naming the build output.
- ###打包后的zip文件名,这里可以加上一些版本的信息
- buildId=HelloWorld
- # Label for the build. Used in naming the build output
- buildLabel=${buildType}.${buildId}
- # Timestamp for the build. Used in naming the build output
- timestamp=007
- #The value to be used for the qualifier of a plugin or feature when you want to override the value computed by pde.
- #The value will only be applied to plugin or features indicating build.properties, qualifier = context
- #forceContextQualifier=<the value for the qualifier>
- #Enable / disable the generation of a suffix for the features that use .qualifier.
- #The generated suffix is computed according to the content of the feature
- #generateFeatureVersionSuffix=true
- ############# BASE CONTROL #############
- # Settings for the base Eclipse components and Java class libraries
- # against which you are building.
- # Base location for anything the build needs to compile against. For example,
- # in most RCP app or a plug-in, the baseLocation should be the location of a previously
- # installed Eclipse against which the application or plug-in code will be compiled and the RCP delta pack.
- ###就是eclipse-RCP-3.3.2-delta-pack和eclipse-RCP-3.3.2-win32的解压位置
- baseLocation=${base}/eclipse
- #Folder containing repositories whose content is needed to compile against
- #repoBaseLocation=${base}/repos
- #Folder where the content of the repositories from ${repoBaseLocation} will be made available as a form suitable to be compiled against
- #transformedRepoLocation=${base}/transformedRepos
- #Os/Ws/Arch/nl of the eclipse specified by baseLocation
- baseos=win32
- basews=win32
- basearch=x86
- #this property indicates whether you want the set of plug-ins and features to be considered during the build to be limited to the ones reachable from the features / plugins being built
- filteredDependencyCheck=false
- #this property indicates whether the resolution should be done in development mode (i.e. ignore multiple bundles with singletons)
- resolution.devMode=false
- #pluginPath is a list of locations in which to find plugins and features. This list is separated by the platform file separator (; or :)
- #a location is one of:
- #- the location of the jar or folder that is the plugin or feature : /path/to/foo.jar or /path/to/foo
- #- a directory that contains a /plugins or /features subdirectory
- #- the location of a feature.xml, or for 2.1 style plugins, the plugin.xml or fragment.xml
- #pluginPath=
- skipBase=true
- eclipseURL=<url for eclipse download site>
- eclipseBuildId=<Id of Eclipse build to get>
- eclipseBaseURL=${eclipseURL}/eclipse-platform-${eclipseBuildId}-win32.zip
- ############# MAP FILE CONTROL ################
- # This section defines CVS tags to use when fetching the map files from the repository.
- # If you want to fetch the map file from repository / location, change the getMapFiles target in the customTargets.xml
- skipMaps=true
- mapsRepo=:pserver:anonymous@example.com/path/to/repo
- mapsRoot=path/to/maps
- mapsCheckoutTag=HEAD
- #tagMaps=true
- mapsTagTag=v${buildId}
- ############ REPOSITORY CONTROL ###############
- # This section defines properties parameterizing the repositories where plugins, fragments
- # bundles and features are being obtained from.
- # The tags to use when fetching elements to build.
- # By default thebuilder will use whatever is in the maps.
- # This value takes the form of a comma separated list of repository identifier (like used in the map files) and the
- # overriding value
- # For example fetchTag=CVS=HEAD, SVN=v20050101
- # fetchTag=HEAD
- skipFetch=true
- ############# JAVA COMPILER OPTIONS ##############
- # The location of the Java jars to compile against. Typically the rt.jar for your JDK/JRE
- #bootclasspath=${java.home}/lib/rt.jar
- # specific JRE locations to compile against. These values are used to compile bundles specifying a
- # Bundle-RequiredExecutionEnvironment. Uncomment and set values for environments that you support
- #CDC-1.0/Foundation-1.0= /path/to/rt.jar
- #CDC-1.1/Foundation-1.1=
- #OSGi/Minimum-1.0=
- #OSGi/Minimum-1.1=
- #JRE-1.1=
- #J2SE-1.2=
- #J2SE-1.3=
- #J2SE-1.4=
- #J2SE-1.5=
- #JavaSE-1.6=
- #PersonalJava-1.1=
- #PersonalJava-1.2=
- #CDC-1.0/PersonalBasis-1.0=
- #CDC-1.0/PersonalJava-1.0=
- #CDC-1.1/PersonalBasis-1.1=
- #CDC-1.1/PersonalJava-1.1=
- # Specify the output format of the compiler log when eclipse jdt is used
- logExtension=.log
- # Whether or not to include debug info in the output jars
- javacDebugInfo=false
- # Whether or not to fail the build if there are compiler errors
- javacFailOnError=true
- # Enable or disable verbose mode of the compiler
- javacVerbose=true
- # Extra arguments for the compiler. These are specific to the java compiler being used.
- #compilerArg=
- # Default value for the version of the source code. This value is used when compiling plug-ins that do not set the Bundle-RequiredExecutionEnvironment or set javacSource in build.properties
- ####这里可以指定你的jdk版本
- #javacSource=1.3
- # Default value for the version of the byte code targeted. This value is used when compiling plug-ins that do not set the Bundle-RequiredExecutionEnvironment or set javacTarget in build.properties.
- #javacTarget=1.1
接下来是build.xml
- <!--
- This program and the accompanying materials are made available
- under the terms of the Eclipse Public License v1.0 which
- accompanies this distribution, and is available at
- http://www.eclipse.org/legal/epl-v10.html
- This build script creates a build directory containing the plugins
- and features to be built, and then kicks off the PDE build process.
- You could just as easily do this from a shell script or cron job.
- Also, the script can be run inside the Eclipse IDE by choosing
- Run As -> Ant Build from the context menu. It could obviously be
- run outside of the IDE if you have ANT installed on your path.
- If you have any questions about this build, feel free to contact me
- at patrick@rcpquickstart.com.
- -->
- <project name="com.rcpquickstart.helloworld.build" default="build">
- <property file="build.properties" />
- <!--
- PDE Build expects that the build directory contains a "plugins"
- directory and a "features" directory. These directories should contain
- the various plug-ins and features to be built.
- It's possible to use the CVS checkout process that is built into
- PDE Build. This is done with map files and is beyond the scope of
- this tutorial.
- This tutorial simply copies the projects directly from your workspace
- into the appropriate build directory folders.
- -->
- <target name="init">
- <mkdir dir="${buildDirectory}" />
- <mkdir dir="${buildDirectory}/plugins" />
- <mkdir dir="${buildDirectory}/features" />
- <copy todir="${buildDirectory}/plugins">
- <fileset dir="../">
- <include name="com.rcpquickstart.helloworld/**" />
- </fileset>
- </copy>
- <copy todir="${buildDirectory}/features">
- <fileset dir="../">
- <include name="com.rcpquickstart.helloworld.feature/**" />
- </fileset>
- </copy>
- </target>
- <!--
- This target actually executes the PDE Build process by launching the
- Eclipse antRunner application.
- NOTE: If you are using Eclipse 3.2, switch out the pathelement below
- with the one that is commented out.
- -->
- <target name="pde-build">
- <java classname="org.eclipse.equinox.launcher.Main" fork="true" failonerror="true">
- <!-- replace with following for Eclipse 3.2 -->
- <!--<java classname="org.eclipse.core.launcher.Main" fork="true" failοnerrοr="true">-->
- <arg value="-application" />
- <arg value="org.eclipse.ant.core.antRunner" />
- <arg value="-buildfile" />
- <arg value="${eclipseLocation}/plugins/org.eclipse.pde.build_${pdeBuildPluginVersion}/scripts/productBuild/productBuild.xml" />
- <arg value="-Dtimestamp=${timestamp}" />
- <classpath>
- <pathelement location="${eclipseLocation}/plugins/org.eclipse.equinox.launcher_${equinoxLauncherPluginVersion}.jar" />
- <!-- replace with following for Eclipse 3.2 -->
- <!-- <pathelement location="${eclipseLocation}/startup.jar" />-->
- </classpath>
- </java>
- </target>
- <target name="clean">
- <delete dir="${buildDirectory}" />
- </target>
- <target name="build" depends="clean, init, pde-build" />
- </project>
完成了上面的步骤,基本上就快大功告成了,在com.rcpquickstart.helloworld.build工程的build.xml单击右键Run as-->Ant Build可以进行调试,如果没有错误就可以在目标目录下生成到处打包的文件了。
5.编写运行脚本,以windows的bat为例。
在windows中进入com.rcpquickstart.helloworld.build的目录,创建一个bat文件,内容如下:
set JAVA_HOME=D:/DevTools/jdk1.6.0_19
set ANT_HOME=D:/DevTools/apache-ant-1.7.1
%ANT_HOME%/bin/ant.bat -file build.xml
以后每次打包就可以只运行这个bat文件了。
这种打包的方式是采用eclipse的pde工具来完成的,当然如果ant写得好除了在上述build.properties中可以在cvs或是svn上获取代码后打包外,还可以调用如inno setup的软件完成安装程序的制作,大大地减少了打包的工作量,而且还可靠稳定。