服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

为项目自动添加构建路径


cownew 开源团队网站 www.cownew.com 

论坛 http://www.cownew.com/newpeng/  

转载请保留此信息 

     有一些插件会自动将需要的jar包自动设置到构建路径上,比如使用wtp的新建向导新建web项目的时候就会把web开发需要的jar包自动放入项目的构建路径,使用pde的“将项目转换为插件项目”功能后项目的构建路径中就增加了插件依赖项的库。我这里来演示一下其实现:
在这个例子中,我们想要做一个“为项目添加lucene支持”的功能,用户在项目上点击右键,选择菜单中的“为项目添加lucene支持”以后,插件把lucene的jar包和源码包拷贝到项目的lib目录下,并且将jar包加入构建路径。如下图:
这是增加lucene支持前的项目结构:
 
用户在项目上点击右键,选择菜单中的“为项目添加lucene支持”后的项目结构
 
这是工程构建路径
 
①新建一个插件工程,并将jdt相关的依赖项加入。
②添加一个“org.eclipse.ui.popupmenus”的扩展点,如果不熟悉怎么添加,可以使用插件向导中的“弹出菜单”向导。
需要注意contribution的配置, 
此插件只针对java项目才起作用,因此“objectclass”中填入“org.eclipse.jdt.core.ijavaproject”;
adaptable选择“true”(后边讲解为什么);
如果是用向导生成的那么请记得清空“namefilter”。
③下面是核心类actionaddlucene 的实现代码
public class actionaddlucene implements iobjectactiondelegate
{
 private static final string fileseparator = system.getproperty("file.separator","/");
 private static final string lucenesrcjar = "lucene-1.4.3-src.jar";
 private static final string lucenejar = "lucene-1.4.3.jar";
 private static final string lib = "lib";
 private static final string resoucelib = "resoucelib";
 private istructuredselection structselection;

 public actionaddlucene()
 {
  super();
 }

 public void setactivepart(iaction action, iworkbenchpart targetpart)
 {
 }

 public void run(iaction action)
 {
  object selectobj = structselection.getfirstelement();
  if (selectobj instanceof iproject)
  {
   iproject project = (iproject) selectobj;
   ijavaproject javaproject = javacore.create(project);
   iclasspathentry[] oldpaths = javaproject.readrawclasspath();
   iclasspathentry lucenelibentry = javacore.newlibraryentry(project
     .getfile(lib + fileseparator + lucenejar).getfullpath(), project
     .getfile(lib + fileseparator + lucenesrcjar).getfullpath(), null,
     false);  
   
   if(classpathexists(oldpaths,lucenelibentry))
   {
    return;
   }

   url lucenelib = activator.getdefault().getbundle().getentry(
     resoucelib + fileseparator + lucenejar);
   url lucenesrc = activator.getdefault().getbundle().getentry(
     resoucelib + fileseparator + lucenesrcjar);
   iclasspathentry[] newpaths = new iclasspathentry[oldpaths.length + 1];
   system.arraycopy(oldpaths, 0, newpaths, 0, oldpaths.length);

   ifolder libfolder = project.getfolder(lib);
   if (!libfolder.exists())
   {
    try
    {
     libfolder.create(true, true, null);
    } catch (coreexception e)
    {
     handleexception(e);
    }
   }

   copyurltofile(lucenelib, project, lib + fileseparator + lucenejar);
   copyurltofile(lucenesrc, project, lib + fileseparator + lucenesrcjar);
   
   newpaths[oldpaths.length] = lucenelibentry;
   try
   {
    javaproject.setrawclasspath(newpaths, null);
   } catch (javamodelexception e)
   {
    handleexception(e);
   }
  }
 }

 private static boolean  classpathexists(iclasspathentry[] entrys,iclasspathentry entry)
 {
  for(int i=0,n=entrys.length;i<n;i++)
  {
   if(entrys[i].getpath().equals(entry.getpath()))
   {
    return true;
   }
  }
  return false;
 }
 private static void handleexception(exception e)
 {
  activator.getdefault().getlog().log(
    new status(istatus.error, activator.plugin_id, 0, e
      .getmessage(), e));
 }

 private static void copyurltofile(url url, iproject project,
   string destfilename)
 {
  inputstream instream = null;
  try
  {
   instream = url.openstream();
   ifile file = project.getfile(destfilename);
   if (!file.exists())
   {
    file.create(instream, true, null);
   }

  } catch (ioexception e)
  {
   handleexception(e);
  } catch (coreexception e)
  {
   handleexception(e);
  } finally
  {
   try
   {
    if (instream != null)
     instream.close();
   } catch (ioexception e)
   {
    handleexception(e);
   }
  }

 }

 public void selectionchanged(iaction action, iselection selection)
 {
  structselection = (istructuredselection) selection;
 }
}
下面解释一下代码中的重点部分:
①iclasspathentry[] oldpaths = javaproject.readrawclasspath();
读取项目原有的构建路径条目。
②      
iclasspathentry lucenelibentry = javacore.newlibraryentry(project
     .getfile(lib + fileseparator + lucenejar).getfullpath(), project
     .getfile(lib + fileseparator + lucenesrcjar).getfullpath(),null,
     false);  
这一句构建lucene的jar包。
第一个参数是二进制jar包的位置,我们的二进制jar包的位置为项目路径下的lib/lucene-1.4.3-src.jar;
第二个参数是jar包对应的源码包的位置;
第三个参数为源码包的根路径,lucene的源码包的源码根路径就是jar包的根路径,因此我们置此参数为null;
第四个参数表示是否导出,我们置为false;
③url lucenelib = activator.getdefault().getbundle().getentry(resoucelib + fileseparator + lucenejar);
我们把“lucene-1.4.3.jar”、“lucene-1.4.3-src.jar”放到我们插件的“resoucelib”目录下,当用户点击“为项目添加lucene支持”的时候我们要把这两个文件拷贝到项目的lib目录下,因此我们需要首先读取插件路径“resoucelib”目录下的这两个jar包。
读取插件路径下的文件我们使用插件activator类提供的方法即可,比如:
activator.getdefault().getbundle().getentry(“config/my.xml”)
就可以读取到插件根目录下的文件“config/my.xml”,返回类型是java.net.url。
④copyurltofile(lucenelib, project, lib + fileseparator + lucenejar);
activator.getdefault().getbundle().getentry读取到的文件位置是url类型的,我们需要把这个url对应的文件拷贝到项目的"lib"下。下面看一下copyurltofile的主干代码:
 instream = url.openstream();
 ifile file = project.getfile(destfilename);
 if (!file.exists())
 {
  file.create(instream, true, null);
 }

   url类有一个openstream可以打开文件的输入流,ifile也有一个接受输入流的create方法用来创建文件,因此我们只需要把url的输入流输出给ifile的create方法即可。
     这里我们也可以由url得到其对应的磁盘上的路径,也可以得到ifile对应的磁盘上的路径,然后使用java io来进行文件复制操作。但是这样做不仅代码数量变多了,而且由于不是使用的eclipse的资源管理api,会带来无法自动刷新等问题,因此建议读者尽量使用eclipse提供的api来完成功能。

扫描关注微信公众号