Eclipse插件全面了解

 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的最简单的插件,但其实我们的天气预报并不比它复杂多少,建完改插件以后的效果如下图。
  

 


  现在,将项目作为运行时工作台运行(run - run as runtime workbench),在一个全新的Eclipse窗口中,通过点击菜单 sample menu 的sample Action或者工具栏中的圆形Eclipse 图标,你将看到如下效果的对话框。
  

 


  到此为止,天气预报插件的原始版做成了,通过修改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>
  
  此时在运行时工作台,我们的菜单已经改变。
  

 


  2.2用VisualEditer制作天气预报对话框
  
  虽然菜单是天气预报,但是我们需要的不是hello Eclispe对话框,我们需要的是告诉我们天气的对话框,当然需要我们从头开始,于是我们需要重新构建一个对话框,这个就需要 Visual Editor来帮助进行界面的开发。我们将使用Visual Editor实现一个Swing对话框,当然只用VE做一个对话框是有点大材小用,但是作为起点,已经合适了。
  
  首先构建Visual Editer开发环境(读者可参考相关资料),当一切准备齐全,鼠标右键点击PackgeExpoler中的 "muplugin.actions"java文件,从弹出式菜单中选择 new->other->VisualClass,新建一个可视化的类,弹出界面如下图:
  

 


  选择next,然后在name中输入WeatherDialog,这个就是我们用来显示天气预报的dialog
  
  选择该对话框的超类为javax.swing.JDiaog,点击Finish按钮。等待一段时间后,我们的对话框就基本生成了,鼠标点击左上角图标,直接输入天气预报就是对话框的标题,同时 我们可以看到左侧的VisualEditor面板。
  

 


  然后我们将该对话框于与刚才的天气预报菜单连接找到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();
  }
  
  此时,点击菜单运行,我们的对话框看起来象这个样子,在此基础上我们还要在上面增加天气预报信息。
  

 


  2.3增加天气预报功能
  

 


  下面的部分是重点,我们将使用具有解析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();
  }
  
  现在以运行时工作台的方式运行,点击天气预报菜单,可以看到下图:
  


  如果你在上海或者其他城市,试着修改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如下:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <feature  
  3.       id="com.newautovideo.impclient.feature"  
  4.       label="Impclient Feature"  
  5.       version="1.0.0">  
  6.    <description url="http://www.example.com/description">  
  7.       [Enter Feature Description here.]  
  8.    </description>  
  9.    <copyright url="http://www.example.com/copyright">  
  10.       [Enter Copyright Description here.]  
  11.    </copyright>  
  12.    <license url="http://www.example.com/license">  
  13.       [Enter License Description here.]  
  14.    </license>  
  15.    <plugin  
  16.          id="com.newautovideo.impclient"  
  17.          download-size="0"  
  18.          install-size="0"  
  19.          version="0.0.0"  
  20.          unpack="false"/>  
  21.    <plugin  
  22.          id="org.eclipse.core.runtime"  
  23.          download-size="0"  
  24.          install-size="0"  
  25.          version="0.0.0"  
  26.          unpack="false"/>  
  27.    <plugin  
  28.          id="org.eclipse.core.runtime.compatibility.auth"  
  29.          download-size="0"  
  30.          install-size="0"  
  31.          version="0.0.0"  
  32.          unpack="false"/>  
  33.    <plugin  
  34.          id="org.eclipse.gef"  
  35.          download-size="0"  
  36.          install-size="0"  
  37.          version="0.0.0"  
  38.          unpack="false"/>  
  39.    <plugin  
  40.          id="org.eclipse.ui"  
  41.          download-size="0"  
  42.          install-size="0"  
  43.          version="0.0.0"  
  44.          unpack="false"/>  
  45.    <plugin  
  46.          id="org.eclipse.ui.intro"  
  47.          download-size="0"  
  48.          install-size="0"  
  49.          version="0.0.0"  
  50.          unpack="false"/>  
  51.    <plugin  
  52.          id="org.eclipse.ui.views"  
  53.          download-size="0"  
  54.          install-size="0"  
  55.          version="0.0.0"  
  56.          unpack="false"/>  
  57. </feature>  

build.properties里的内容就一行:bin.includes = feature.xml

 

4.创建一个一般工程,名为com.rcpquickstart.helloworld.build,里面创建两个文件,build.properties和build.xml,这两个文件是最关键的文件,最终编译就靠这两个文件,也就是ant脚本。首先说build.properties,这个文件的原本是在一个官方例子中,如下:

[c-sharp] view plain copy print ?
  1. ###############################################################################  
  2. # Copyright (c) 2003, 2006 IBM Corporation and others.  
  3. # All rights reserved. This program and the accompanying materials  
  4. # are made available under the terms of the Eclipse Public License v1.0  
  5. # which accompanies this distribution, and is available at  
  6. # http://www.eclipse.org/legal/epl-v10.html  
  7.  
  8. # Contributors:  
  9. #     IBM Corporation - initial API and implementation  
  10. ###############################################################################  
  11. #####################  
  12. # Parameters describing how and where to execute the build.  
  13. # Typical users need only update the following properties:  
  14. #    baseLocation - where things you are building against are installed  
  15. #    bootclasspath - The base jars to compile against (typicaly rt.jar)  
  16. #    configs - the list of {os, ws, arch} configurations to build.    
  17. #  
  18. # Of course any of the settings here can be overridden by spec'ing   
  19. # them on the command line (e.g., -DbaseLocation=d:/eclipse  
  20. ############# PLUG-IN VERSIONS ######################  
  21. #  
  22. # Look in the plugins directory of your Eclipse  
  23. # installation to determine the version numbers  
  24. # the correct version numbers. These version numbers  
  25. # are used to create the correct paths when launching  
  26. # PDE Build.  
  27. #  
  28. #####################################################  
  29. # Version of org.ecilpse.pdebuild   
  30. pdeBuildPluginVersion=3.3.2.v20071019  
  31. # Version of org.eclipse.equinox.launcher   
  32. equinoxLauncherPluginVersion=1.0.1.R33x_v20080118  
  33. ############# BASE LOCATION #########################  
  34. #  
  35. # Specify the directory of the base under which your  
  36. # your build target is located. This directory should  
  37. # contain the RCP Runtime Binary that you want to   
  38. # compile against.  
  39. #  
  40. #####################################################  
  41. ###编译的根目录   
  42. base=D:/apps-build  
  43. ############# ECLIPSE LOCATION ######################  
  44. #  
  45. # Specify the directory of the Eclipse installation  
  46. # that will be used to execute PDE Build.  
  47. #  
  48. #####################################################  
  49. ###Eclipse的根目录   
  50. eclipseLocation=D:/DevTools/eclipse-jee-europa-winter-win32/eclipse  
  51. ############# PRODUCT/PACKAGING CONTROL #############  
  52. ###刚才创建的.product在工程中的位置   
  53. product=/com.rcpquickstart.helloworld/helloworld.product  
  54. runPackager=true 
  55. #Set the name of the archive that will result from the product build.  
  56. #archiveNamePrefix=  
  57. # The prefix that will be used in the generated archive.  
  58. ###目标文件夹的名称   
  59. archivePrefix=helloworld  
  60. # The location underwhich all of the build output will be collected.   
  61. collectingFolder=${archivePrefix}  
  62. # The list of {os, ws, arch} configurations to build.  This   
  63. # value is a '&' separated list of ',' separate triples.  For example,   
  64. #     configs=win32,win32,x86 & linux,motif,x86  
  65. # By default the value is *,*,*  
  66. #configs = *, *, *  
  67. ###根据系统来选择   
  68. configs=win32, win32, x86  
  69. # & /  
  70. #   win32,win32,x86_64 & /  
  71. #   win32,win32,wpf & /  
  72. #   linux, gtk, ppc & /  
  73. #   linux, gtk, x86 & /  
  74. #   linux, gtk, x86_64 & /  
  75. #   linux, motif, x86 & /  
  76. #   solaris, motif, sparc & /  
  77. #   solaris, gtk, sparc & /  
  78. #   aix, motif, ppc & /  
  79. #   hpux, motif, ia64_32 & /  
  80. #   macosx, carbon, ppc & /  
  81. #   macosx, carbon, x86 & /  
  82. #   macosx, cocoa, ppc & /  
  83. #   macosx, cocoa, x86 & /  
  84. #   macosx, cocoa, x86_64  
  85. # By default PDE creates one archive (result) per entry listed in the configs property.  
  86. # Setting this value to true will cause PDE to only create one output containing all   
  87. # artifacts for all the platforms listed in the configs property.  
  88. # To control the output format for the group, add a "group, group, group - <format>" entry to the  
  89. # archivesFormat.   
  90. #groupConfigurations=true  
  91. #The format of the archive. By default a zip is created using antZip.  
  92. #The list can only contain the configuration for which the desired format is different than zip.  
  93. #archivesFormat=win32, win32, x86 - antZip& /  
  94. #   linux, gtk, ppc - antZip &/  
  95. #    linux, gtk, x86 - antZip& /  
  96. #   linux, gtk, x86_64 - antZip& /  
  97. # linux, motif, x86 - antZip& /  
  98. #   solaris, motif, sparc - antZip& /  
  99. #   solaris, gtk, sparc - antZip& /  
  100. #   aix, motif, ppc - antZip& /  
  101. #   hpux, motif, PA_RISC - antZip& /  
  102. #   macosx, carbon, ppc - antZip  
  103. #Allow cycles involving at most one bundle that needs to be compiled with the rest being binary bundles.   
  104. allowBinaryCycles = true 
  105. #Sort bundles depenedencies across all features instead of just within a given feature.   
  106. flattenDependencies = true 
  107. #Parallel compilation, requires flattenedDependencies=true  
  108. #parallelCompilation=true  
  109. #parallelThreadCount=  
  110. #parallelThreadsPerProcessor=  
  111.      
  112. #Set to true if you want the output to be ready for an update jar (no site.xml generated)  
  113. #outputUpdateJars = false  
  114. #Set to true for Jnlp generation  
  115. #codebase should be a URL that will be used as the root of all relative URLs in the output.  
  116. #generateJnlp=false  
  117. #jnlp.codebase=<codebase url>  
  118. #jnlp.j2se=<j2se version>  
  119. #jnlp.locale=<a locale>  
  120. #jnlp.generateOfflineAllowed=true or false generate <offlineAllowed/> attribute in the generated features  
  121. #jnlp.configs=${configs}            #uncomment to filter the content of the generated jnlp files based on the configuration being built  
  122. #Set to true if you want to sign jars  
  123. #signJars=false  
  124. #sign.alias=<alias>  
  125. #sign.keystore=<keystore location>  
  126. #sign.storepass=<keystore password>  
  127. #sign.keypass=<key password>  
  128. #Arguments to send to the zip executable   
  129. zipargs=  
  130. #Arguments to send to the tar executable   
  131. tarargs=  
  132. #Control the creation of a file containing the version included in each configuration - on by default   
  133. #generateVersionsLists=false  
  134. ############## BUILD NAMING CONTROL ################  
  135. # The directory into which the build elements are fetched and where  
  136. # the build takes place.  
  137. ###打包的目标文件夹   
  138. buildDirectory=d:/apps-build/build  
  139. # Type of build.  Used in naming the build output.  Typically this value is  
  140. # one of I, N, M, S, ...  
  141. ###形成的zip文件前缀   
  142. buildType=I  
  143. # ID of the build.  Used in naming the build output.  
  144. ###打包后的zip文件名,这里可以加上一些版本的信息   
  145. buildId=HelloWorld  
  146. # Label for the build.  Used in naming the build output   
  147. buildLabel=${buildType}.${buildId}  
  148. # Timestamp for the build.  Used in naming the build output   
  149. timestamp=007  
  150. #The value to be used for the qualifier of a plugin or feature when you want to override the value computed by pde.  
  151. #The value will only be applied to plugin or features indicating build.properties, qualifier = context   
  152. #forceContextQualifier=<the value for the qualifier>  
  153. #Enable / disable the generation of a suffix for the features that use .qualifier.   
  154. #The generated suffix is computed according to the content of the feature     
  155. #generateFeatureVersionSuffix=true  
  156. ############# BASE CONTROL #############  
  157. # Settings for the base Eclipse components and Java class libraries   
  158. # against which you are building.  
  159. # Base location for anything the build needs to compile against.  For example,  
  160. # in most RCP app or a plug-in,  the baseLocation should be the location of a previously  
  161. # installed Eclipse against which the application or plug-in code will be compiled and the RCP delta pack.  
  162. ###就是eclipse-RCP-3.3.2-delta-pack和eclipse-RCP-3.3.2-win32的解压位置   
  163. baseLocation=${base}/eclipse  
  164. #Folder containing repositories whose content is needed to compile against  
  165. #repoBaseLocation=${base}/repos  
  166. #Folder where the content of the repositories from ${repoBaseLocation} will be made available as a form suitable to be compiled against  
  167. #transformedRepoLocation=${base}/transformedRepos  
  168. #Os/Ws/Arch/nl of the eclipse specified by baseLocation   
  169. baseos=win32  
  170. basews=win32  
  171. basearch=x86  
  172. #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   
  173. filteredDependencyCheck=false 
  174. #this property indicates whether the resolution should be done in development mode (i.e. ignore multiple bundles with singletons)   
  175. resolution.devMode=false 
  176. #pluginPath is a list of locations in which to find plugins and features.  This list is separated by the platform file separator (; or :)  
  177. #a location is one of:    
  178. #- the location of the jar or folder that is the plugin or feature : /path/to/foo.jar or /path/to/foo  
  179. #- a directory that contains a /plugins or /features subdirectory  
  180. #- the location of a feature.xml, or for 2.1 style plugins, the plugin.xml or fragment.xml  
  181. #pluginPath=   
  182. skipBase=true  
  183. eclipseURL=<url for eclipse download site>  
  184. eclipseBuildId=<Id of Eclipse build to get>  
  185. eclipseBaseURL=${eclipseURL}/eclipse-platform-${eclipseBuildId}-win32.zip  
  186. ############# MAP FILE CONTROL ################  
  187. # This section defines CVS tags to use when fetching the map files from the repository.  
  188. # If you want to fetch the map file from repository / location, change the getMapFiles target in the customTargets.xml   
  189. skipMaps=true  
  190. mapsRepo=:pserver:anonymous@example.com/path/to/repo  
  191. mapsRoot=path/to/maps  
  192. mapsCheckoutTag=HEAD  
  193. #tagMaps=true   
  194. mapsTagTag=v${buildId}  
  195. ############ REPOSITORY CONTROL ###############  
  196. # This section defines properties parameterizing the repositories where plugins, fragments  
  197. # bundles and features are being obtained from.   
  198. # The tags to use when fetching elements to build.  
  199. # By default thebuilder will use whatever is in the maps.    
  200. # This value takes the form of a comma separated list of repository identifier (like used in the map files) and the   
  201. # overriding value  
  202. # For example fetchTag=CVS=HEAD, SVN=v20050101  
  203. # fetchTag=HEAD   
  204. skipFetch=true 
  205. ############# JAVA COMPILER OPTIONS ##############  
  206. # The location of the Java jars to compile against.  Typically the rt.jar for your JDK/JRE  
  207. #bootclasspath=${java.home}/lib/rt.jar  
  208. # specific JRE locations to compile against. These values are used to compile bundles specifying a   
  209. # Bundle-RequiredExecutionEnvironment. Uncomment and set values for environments that you support  
  210. #CDC-1.0/Foundation-1.0= /path/to/rt.jar  
  211. #CDC-1.1/Foundation-1.1=  
  212. #OSGi/Minimum-1.0=  
  213. #OSGi/Minimum-1.1=  
  214. #JRE-1.1=  
  215. #J2SE-1.2=  
  216. #J2SE-1.3=  
  217. #J2SE-1.4=  
  218. #J2SE-1.5=  
  219. #JavaSE-1.6=  
  220. #PersonalJava-1.1=  
  221. #PersonalJava-1.2=  
  222. #CDC-1.0/PersonalBasis-1.0=  
  223. #CDC-1.0/PersonalJava-1.0=  
  224. #CDC-1.1/PersonalBasis-1.1=  
  225. #CDC-1.1/PersonalJava-1.1=  
  226. # Specify the output format of the compiler log when eclipse jdt is used   
  227. logExtension=.log  
  228. # Whether or not to include debug info in the output jars   
  229. javacDebugInfo=false   
  230. # Whether or not to fail the build if there are compiler errors   
  231. javacFailOnError=true 
  232. # Enable or disable verbose mode of the compiler   
  233. javacVerbose=true 
  234. # Extra arguments for the compiler. These are specific to the java compiler being used.  
  235. #compilerArg=  
  236. # 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  
  237. ####这里可以指定你的jdk版本  
  238. #javacSource=1.3  
  239. # 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.  
  240. #javacTarget=1.1  

 

接下来是build.xml

  1. <!--  
  2.     This program and the accompanying materials are made available  
  3.     under the terms of the Eclipse Public License v1.0 which  
  4.     accompanies this distribution, and is available at  
  5.     http://www.eclipse.org/legal/epl-v10.html  
  6.       
  7.     This build script creates a build directory containing the plugins  
  8.     and features to be built, and then kicks off the PDE build process.  
  9.     You could just as easily do this from a shell script or cron job.  
  10.       
  11.     Also, the script can be run inside the Eclipse IDE by choosing   
  12.     Run As -> Ant Build from the context menu. It could obviously be  
  13.     run outside of the IDE if you have ANT installed on your path.  
  14.       
  15.     If you have any questions about this build, feel free to contact me  
  16.     at patrick@rcpquickstart.com.  
  17. -->  
  18. <project name="com.rcpquickstart.helloworld.build" default="build">  
  19.     <property file="build.properties" />  
  20.       
  21.     <!--  
  22.         PDE Build expects that the build directory contains a "plugins"   
  23.         directory and a "features" directory. These directories should contain  
  24.         the various plug-ins and features to be built.  
  25.           
  26.         It's possible to use the CVS checkout process that is built into   
  27.         PDE Build. This is done with map files and is beyond the scope of   
  28.         this tutorial.   
  29.           
  30.         This tutorial simply copies the projects directly from your workspace  
  31.         into the appropriate build directory folders.  
  32.     -->  
  33.     <target name="init">  
  34.         <mkdir dir="${buildDirectory}" />  
  35.         <mkdir dir="${buildDirectory}/plugins" />  
  36.         <mkdir dir="${buildDirectory}/features" />  
  37.         <copy todir="${buildDirectory}/plugins">  
  38.             <fileset dir="../">  
  39.                 <include name="com.rcpquickstart.helloworld/**" />  
  40.                             </fileset>  
  41.         </copy>  
  42.         <copy todir="${buildDirectory}/features">  
  43.             <fileset dir="../">  
  44.                 <include name="com.rcpquickstart.helloworld.feature/**" />  
  45.             </fileset>  
  46.         </copy>  
  47.     </target>  
  48.       
  49.     <!--  
  50.         This target actually executes the PDE Build process by launching the   
  51.         Eclipse antRunner application.  
  52.           
  53.         NOTE: If you are using Eclipse 3.2, switch out the pathelement below  
  54.         with the one that is commented out.  
  55.     -->  
  56.     <target name="pde-build">  
  57.         <java classname="org.eclipse.equinox.launcher.Main" fork="true" failonerror="true">  
  58.         <!-- replace with following for Eclipse 3.2 -->  
  59.         <!--<java classname="org.eclipse.core.launcher.Main" fork="true" failοnerrοr="true">-->           
  60.             <arg value="-application" />  
  61.             <arg value="org.eclipse.ant.core.antRunner" />  
  62.             <arg value="-buildfile" />  
  63.             <arg value="${eclipseLocation}/plugins/org.eclipse.pde.build_${pdeBuildPluginVersion}/scripts/productBuild/productBuild.xml" />  
  64.             <arg value="-Dtimestamp=${timestamp}" />  
  65.             <classpath>  
  66.                 <pathelement location="${eclipseLocation}/plugins/org.eclipse.equinox.launcher_${equinoxLauncherPluginVersion}.jar" />  
  67.                 <!-- replace with following for Eclipse 3.2 -->  
  68.                 <!-- <pathelement location="${eclipseLocation}/startup.jar" />-->  
  69.             </classpath>  
  70.         </java>  
  71.     </target>  
  72.           
  73.     <target name="clean">  
  74.         <delete dir="${buildDirectory}" />  
  75.     </target>  
  76.     <target name="build" depends="clean, init, pde-build" />  
  77. </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的软件完成安装程序的制作,大大地减少了打包的工作量,而且还可靠稳定。

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值