专注于互联网--专注于架构

最新标签
网站地图
文章索引
Rss订阅

首页 »Java教程 » eclipsewtp:Eclipse WTP Projects Facets实战指南 »正文

eclipsewtp:Eclipse WTP Projects Facets实战指南

来源: 发布时间:星期四, 2008年12月18日 浏览:2次 评论:0
JAVA中文站社区门户X5o+B8zO#[F这个文章基本是"Building Project Facets"中文版

6pvc S#V!Bl@ M E0TCWi在WTP中创建Dynamic Web Project工程时候提供了Project Facets功能,它让用户在创建web项目时候,可以像搭积木下给我们创建project添加各种功能模块,下面我们通过个例子来讲解下WTP中Project Facets使用方法

;NBEbVZ/X*h JAVA中文站社区门户w9y?"x z通常我们给创建工程可能添加以下元素:资源文件, Builder, Nature标识等, 我们知道Eclipse本身提供了个Nature功能,但是Facets跟Nature是有区别, 给工程添加Nature是用来给Eclipse底层工具使用,而Facets是用来给最终用户使用, 通常Facets提供模块都是自描述,这样可以保证用户在使用时候,避免添加不必要Facet模块, 而且对于我们开发者而言也避免了在新建向导中插入新wizard page以及避免通过右键菜单来给工程添加或者去掉某模块,从而大大简化了开发人员工作量, 因此Facets对用户和开发者来说都是个不错选择JAVA中文站社区门户uDWyL

JAVA中文站社区门户a1K|,R9m:bf下面我们结合个名为FormGen例子来介绍, FormGen用来在我们创建web工程中,加入些我们自己jar包以及修改web.xml文件JAVA中文站社区门户r5|`)_4Z?/Po

JAVA中文站社区门户n8jL cw#?P%O废话少说,先在插件工程中添加org.eclipse.wst.common.project.facet.core.facets 扩展点, 当然这里需要在依赖中加入org.eclipse.wst.common.project.facet.core引用,该扩展点包罗万象,这里我们遵循循序渐进原则,从最最简单内容开始:
N8X OK@M

F5xq,[s {@{k xml 代码

    =dp-xml>
  1. =alt>=tag><=tag-name>extension =attribute>po==attribute-value>"org.eclipse.wst.common.project.facet.core.facets"=tag>>    
  2. ="">  
  3. =alt>  =tag><=tag-name>project-facet =attribute>id==attribute-value>"{}"=tag>> (0 or more)   
  4. ="">    =tag><=tag-name>label=tag>>{}=tag>=tag-name>label=tag>>  
  5. =alt>    =tag><=tag-name>description=tag>>{}=tag>=tag-name>description=tag>> (optional)   
  6. ="">  =tag>=tag-name>project-facet=tag>>  
  7. =alt>  
  8. ="">  =tag><=tag-name>project-facet-version =attribute>facet==attribute-value>"{}" =attribute>version==attribute-value>"{}"=tag>/> (0 or more)   
  9. =alt>     
  10. ="">=tag>=tag-name>extension=tag>>  

J%EMU-~这里加入了两个节点:project-facet是我们要加入facet, project-facet-version用来制定我们facet版本, 每个facet至少需要给它指定个版本,否则不可用, 后面大家就会知道,实际上我们大部分工作都集中在project-facet-version中配置, 配置后代码如下:
f(a;ZE%E

+^ Or$pf1Ln%LRu+X xml 代码

    =dp-xml>
  1. =alt>=tag><=tag-name>extension =attribute>po==attribute-value>"org.eclipse.wst.common.project.facet.core.facets"=tag>>  
  2. ="">  
  3. =alt>  =tag><=tag-name>project-facet =attribute>id==attribute-value>"formgen.core"=tag>>  
  4. ="">    =tag><=tag-name>label=tag>>FormGen Core=tag>=tag-name>label=tag>>  
  5. =alt>    =tag><=tag-name>description=tag>>  
  6. ="">      Enables generation of HTML forms based on XML definition files.   
  7. =alt>    =tag>=tag-name>description=tag>>  
  8. ="">  =tag>=tag-name>project-facet=tag>>  
  9. =alt>  
  10. ="">  =tag><=tag-name>project-facet-version =attribute>facet==attribute-value>"formgen.core" =attribute>version==attribute-value>"1.0"=tag>/>  
  11. =alt>  
  12. ="">  =tag><=tag-name>project-facet =attribute>id==attribute-value>"formgen.ext"=tag>>  
  13. =alt>    =tag><=tag-name>label=tag>>FormGen Extensions=tag>=tag-name>label=tag>>  
  14. ="">    =tag><=tag-name>description=tag>>  
  15. =alt>      Enables additional FormGen widgets.   
  16. ="">    =tag>=tag-name>description=tag>>  
  17. =alt>  =tag>=tag-name>project-facet=tag>>  
  18. ="">  
  19. =alt>  =tag><=tag-name>project-facet-version =attribute>facet==attribute-value>"formgen.ext" =attribute>version==attribute-value>"1.0"=tag>/>  
  20. ="">     
  21. =alt>=tag>=tag-name>extension=tag>>  

JAVA中文站社区门户F@ SQ j设置好后运行我们插件项目,创建个Dynamic Web Project, 在向导中第个页面Target Runtime设置为none,继续进入到下个页面,这时在下面列表中就会出现我们添加两个facetJAVA中文站社区门户JxJ.BwQa6a"_

z'C1R%P(H"F|(afxAJAVA中文站社区门户QD fpN)rA-A-H

JAVA中文站社区门户p v0Zh)O2Q6X0}b接下来工作就是给facet指定约束
R(Cl#mq/yK由于我们FormGen是基于Servlet,因此它应该适用于所有Java EE项目.下面我们通过增加约束让FormGen facet也能在EJB项目向导中使用.JAVA中文站社区门户9i%?$P0])a/qP5}6t o/_
下面是我们将要用到扩展节点相关介绍说明:
Fq H4Sd\JAVA中文站社区门户k,MV7n~Dh

xml 代码

    =dp-xml>
  1. =alt>=tag><=tag-name>extension =attribute>po==attribute-value>"org.eclipse.wst.common.project.facet.core.facets"=tag>>  
  2. ="">  =tag><=tag-name>project-facet-version=tag>>  
  3. =alt>    =tag><=tag-name>constra=tag>> (optional)   
  4. ="">      [expr]   
  5. =alt>    =tag>=tag-name>constra=tag>>  
  6. ="">  =tag>=tag-name>project-facet-version=tag>>  
  7. =alt>=tag>=tag-name>extension=tag>>  
  8. ="">  
  9. =alt>[expr] =   
  10. ="">  =tag><=tag-name>requires =attribute>facet==attribute-value>"{}" =attribute>version==attribute-value>"{version.expr}" =attribute>soft==attribute-value>"{boolean}"=tag>/> or   
  11. =alt>  =tag><=tag-name>conflicts =attribute>facet==attribute-value>"{}" =attribute>version==attribute-value>"{version.expr}"=tag>/> or   
  12. ="">  =tag><=tag-name>conflicts =attribute>group==attribute-value>"{}"=tag>/> or   
  13. =alt>  =tag><=tag-name>and=tag>>  
  14. ="">    [expr] (1 or more)   
  15. =alt>  =tag>=tag-name>and=tag>> or   
  16. ="">  =tag><=tag-name>or=tag>>  
  17. =alt>    [expr] (1 or more)   
  18. ="">  =tag>=tag-name>or=tag>>  

"i$Y,\Gnn8Ee_Y/M6\约束由树状结构表达式组成, 它包括4个部分,这里我们分别来加以介绍说明:JAVA中文站社区门户 hT}/qyYF&~z

S.X)W']V}wa\requires:是用最多部分,它表示当前facet还需要依赖其他facet, 如果对所依赖facet没有版本要求,那么不用设置version属性, version可以设置为表达式, soft属性用来创建种特定类型依赖,即如果依赖facet不存在,那么当前facet将不会在选项列表中出现,其本facet必须在引用facet的后被安装JAVA中文站社区门户m3V0EMSq

l0rs[)qfonflicts:冲突约束用来制定如果在同个工程中已经存在了这里指定facet,那么当前声明facet将不可用,它用两种形式:可以指定单个冲突或组(group)冲突facet.对于组冲突,这里需要解释下, 组冲突用来指定当前facet和没有在facet列表中列出类facet有冲突,比如说, WTP自己moudule facet都属于moudules组, 而这些facet在定义时候,都将组冲突设置为modules, 这样就可以保证两个module不会同时安装在同个工程中

Gw4]{~c*}0cM JAVA中文站社区门户|!b$I:G9d X E要让个facet属于某个组,需要这样定义扩展点:JAVA中文站社区门户$c [&ZR.q
JAVA中文站社区门户i9fmn3a*xI


    =dp-xml>
  1. =alt>=tag><=tag-name>extension =attribute>po==attribute-value>"org.eclipse.wst.common.project.facet.core.facets"=tag>>      
  2. ="">  =tag><=tag-name>project-facet-version=tag>>      
  3. =alt>    =tag><=tag-name>group-member =attribute>id==attribute-value>"{}"=tag>/> (0 or more)      
  4. ="">  =tag>=tag-name>project-facet-version=tag>>      
  5. =alt>=tag>=tag-name>extension=tag>>  

8buZw$n[(f7D*{PJand & or 是用来做逻辑判断,写都知道,这里不做介绍JAVA中文站社区门户$g0m\ @j at

e2ZJ*EP{%u下面是我们FormGenfacet定义,为了让我们创建项目是个Java EE项目,这里我们设置FormGen Core依赖jst.web facet. FormGen Ext facet需要建立在 FormGen Core基础上
[%U/qgzJAVA中文站社区门户#R k i(?8U:D

xml 代码

    =dp-xml>
  1. =alt>=tag><=tag-name>extension =attribute>po==attribute-value>"org.eclipse.wst.common.project.facet.core.facets"=tag>>  
  2. ="">  
  3. =alt>  =tag><=tag-name>project-facet-version =attribute>id==attribute-value>"formgen.core" =attribute>version==attribute-value>"1.0"=tag>>  
  4. ="">    =tag><=tag-name>constra=tag>>  
  5. =alt>      =tag><=tag-name>requires =attribute>facet==attribute-value>"jst.web" =attribute>version==attribute-value>"2.2,2.3,2.4"=tag>/>  
  6. ="">    =tag>=tag-name>constra=tag>>  
  7. =alt>  =tag>=tag-name>project-facet=tag>>  
  8. ="">  
  9. =alt>  =tag><=tag-name>project-facet-version =attribute>id==attribute-value>"formgen.ext" =attribute>version==attribute-value>"1.0"=tag>>  
  10. ="">    =tag><=tag-name>constra=tag>>  
  11. =alt>      =tag><=tag-name>requires =attribute>facet==attribute-value>"formgen.core" =attribute>version==attribute-value>"1.0"=tag>/>  
  12. ="">    =tag>=tag-name>constra=tag>>  
  13. =alt>  =tag>=tag-name>project-facet=tag>>  
  14. ="">     
  15. =alt>=tag>=tag-name>extension=tag>>  

e,IVa9}3e1k
+Tn i&i8_E+c4\实现Action
|+F k @ A Ja0z经过了上面设置的后,运行插件,在新建工程向导里面选择FormGen Core会出现提示信息,这是我们还没有实现对应Action,这里所说Action是指facet所要执行操作,这里有 3种类型action:INSTALL, UNINSTALL, and VERSION_CHANGE,下面工作就是实现FormGen Coreaction

ds|Jv,c*y1y9A JAVA中文站社区门户 KvR:^z8DJAVA中文站社区门户QF9p M3R:o*n v

|3d'Vry下面是action对应扩展点设置格式:JAVA中文站社区门户SLm}&a


faq3AlBR xml 代码

    =dp-xml>
  1. =alt>=tag><=tag-name>extension =attribute>po==attribute-value>"org.eclipse.wst.common.project.facet.core.facets"=tag>>  
  2. ="">  =tag><=tag-name>action =attribute>id==attribute-value>"{}" =attribute>facet==attribute-value>"{}" =attribute>version==attribute-value>"{version.expr}" =attribute>type==attribute-value>"INSTALL|UNINSTALL|VERSION_CHANGE"=tag>>  
  3. =alt>    =tag><=tag-name>delegate =attribute>==attribute-value>"{:org.eclipse.wst.common.project.facet.core.IDelegate}"=tag>/>  
  4. ="">    =tag><=tag-name>property =attribute>name==attribute-value>"{}" =attribute>value==attribute-value>"{}"=tag>/> (0 or more)   
  5. =alt>  =tag>=tag-name>action=tag>>  
  6. ="">=tag>=tag-name>extension=tag>>  

y#\1Bb:Oq5J6PJAVA中文站社区门户+S-sHxvvQ#U
下面我们对上述设置进行下介绍说明:JAVA中文站社区门户'VvH"VP
version属性可以是单个值也可以是表达式,如果action使用所有facet,那么不设置

+k.c7lG U7YN0{ JAVA中文站社区门户jNaO A~IH`id属性可选,如果不制定,系统将以"[facet-id]#[version-expression]#[action-type](#[prop-name]=[prop-value])*"格式创建个, 为了可读性,最好自己制定idJAVA中文站社区门户Bl8K+o`0S0P*f

JAVA中文站社区门户DN)H"O3i:Bys%k在action内部还可以在project-facet-version元素内部中使用,如果是这样话,那么facet和version属性将被忽略,如果同个action delegate实现被多个facet 版本使用,那么最好在外面单独设置个action节点,这样系统会进行优化JAVA中文站社区门户 {n/E(h]:V9Qf

:G A8FG6H.V对于VERSION_CHANGE类型Action而言, 那么制定了version信息的后,其行为将会收到些限制, 我们只需要在action下property中制定from.versions属性即可,其值可以是单个,也可以是表达式

??T5r|5gK`z#Z M z+x-\,cPh q我们FormGen Coreaction要做两件事:(1)copy formgen-core.jar 到项目WEB-INF/lib 目录下;(2)将FormGen servlet添加到web.xml中.而FormGen Ext会将copy formgen-ext.jar 到WEB-INF/lib 目录下JAVA中文站社区门户xR6z%S3pB-tX/f t
JAVA中文站社区门户{6Tf,b2~;T@9x

xml 代码

    =dp-xml>
  1. =alt>=tag><=tag-name>extension =attribute>po==attribute-value>"org.eclipse.wst.common.project.facet.core.facets"=tag>>  
  2. ="">  
  3. =alt>  =tag><=tag-name>project-facet-version =attribute>facet==attribute-value>"formgen.core" =attribute>version==attribute-value>"1.0"=tag>>  
  4. ="">    =tag><=tag-name>action =attribute>type==attribute-value>"INSTALL"=tag>>  
  5. =alt>      =tag><=tag-name>delegate =attribute>==attribute-value>"com.formgen.eclipse.FormGenCoreFacetInstallDelegate"=tag>/>  
  6. ="">    =tag>=tag-name>action=tag>>  
  7. =alt>  =tag>=tag-name>project-facet-version=tag>>  
  8. ="">  
  9. =alt>  =tag><=tag-name>project-facet-version =attribute>facet==attribute-value>"formgen.ext" =attribute>version==attribute-value>"1.0"=tag>>  
  10. ="">    =tag><=tag-name>action =attribute>type==attribute-value>"INSTALL"=tag>>  
  11. =alt>      =tag><=tag-name>delegate =attribute>==attribute-value>"com.formgen.eclipse.FormGenExtFacetInstallDelegate"=tag>/>  
  12. ="">    =tag>=tag-name>action=tag>>  
  13. =alt>  =tag>=tag-name>project-facet-version=tag>>  
  14. ="">     
  15. =alt>=tag>=tag-name>extension=tag>>  

(\HY~-K(v1QWZ JAVA中文站社区门户&fD:y'U ~ D)l

java 代码

    =dp-j>
  1. =alt>=keyword>package com.formgen.eclipse;   
  2. ="">  
  3. =alt>=keyword>import org.eclipse.core.resources.IFolder;   
  4. ="">=keyword>import org.eclipse.core.resources.IProject;   
  5. =alt>=keyword>import org.eclipse.core.runtime.CoreException;   
  6. ="">=keyword>import org.eclipse.core.runtime.IProgressMonitor;   
  7. =alt>=keyword>import org.eclipse.core.runtime.Path;   
  8. ="">=keyword>import org.eclipse.wst.common.project.facet.core.IDelegate;   
  9. =alt>=keyword>import org.eclipse.wst.common.project.facet.core.IProjectFacetVersion;   
  10. ="">  
  11. =alt>=keyword>public =keyword>final =keyword> FormGenCoreFacetInstallDelegate =keyword>implements IDelegate   
  12. ="">{   
  13. =alt>    =keyword>public =keyword>void execute( =keyword>final IProject pj,   
  14. ="">                         =keyword>final IProjectFacetVersion fv,   
  15. =alt>                         =keyword>final Object config,   
  16. ="">                         =keyword>final IProgressMonitor monitor )   
  17. =alt>  
  18. ="">        =keyword>throws CoreException   
  19. =alt>  
  20. ="">    {   
  21. =alt>        monitor.beginTask( =>""=number>2 );   
  22. ="">  
  23. =alt>        =keyword>try  
  24. ="">        {   
  25. =alt>            =keyword>final IFolder webInfLib = Utils.getWebInfLibDir( pj );   
  26. ="">  
  27. =alt>            Utils.copyFromPlugin( =keyword> Path( =>"libs/formgen-core.jar" ),   
  28. ="">                                  webInfLib.getFile( =>"formgen-core.jar" ) );   
  29. =alt>  
  30. ="">            monitor.worked( =number>1 );   
  31. =alt>  
  32. ="">            Utils.registerFormGenServlet( pj );   
  33. =alt>  
  34. ="">            monitor.worked( =number>1 );   
  35. =alt>        }   
  36. ="">        =keyword>finally  
  37. =alt>        {   
  38. ="">            monitor.done;   
  39. =alt>        }   
  40. ="">    }   
  41. =alt>}   
  42. ="">  

JAVA中文站社区门户*QrV(J{rjava 代码JAVA中文站社区门户%N2J f&J ]'v6F


    =dp-j>
  1. =alt>=keyword>package com.formgen.eclipse;   
  2. ="">  
  3. =alt>=keyword>import org.eclipse.core.resources.IFolder;   
  4. ="">=keyword>import org.eclipse.core.resources.IProject;   
  5. =alt>=keyword>import org.eclipse.core.runtime.CoreException;   
  6. ="">=keyword>import org.eclipse.core.runtime.IProgressMonitor;   
  7. =alt>=keyword>import org.eclipse.core.runtime.Path;   
  8. ="">=keyword>import org.eclipse.wst.common.project.facet.core.IDelegate;   
  9. =alt>=keyword>import org.eclipse.wst.common.project.facet.core.IProjectFacetVersion;   
  10. ="">  
  11. =alt>=keyword>public =keyword>final =keyword> FormGenExtFacetInstallDelegate =keyword>implements IDelegate   
  12. ="">{   
  13. =alt>    =keyword>public =keyword>void execute( =keyword>final IProject pj,   
  14. ="">                         =keyword>final IProjectFacetVersion fv,   
  15. =alt>                         =keyword>final Object config,   
  16. ="">                         =keyword>final IProgressMonitor monitor )   
  17. =alt>  
  18. ="">        =keyword>throws CoreException   
  19. =alt>  
  20. ="">    {   
  21. =alt>        monitor.beginTask( =>""=number>1 );   
  22. ="">  
  23. =alt>        =keyword>try  
  24. ="">        {   
  25. =alt>            =keyword>final IFolder webInfLib = Utils.getWebInfLibDir( pj );   
  26. ="">  
  27. =alt>            Utils.copyFromPlugin( =keyword> Path( =>"libs/formgen-ext.jar" ),   
  28. ="">                                  webInfLib.getFile( =>"formgen-ext.jar" ) );   
  29. =alt>  
  30. ="">            monitor.worked( =number>1 );   
  31. =alt>        }   
  32. ="">        =keyword>finally  
  33. =alt>        {   
  34. ="">            monitor.done;   
  35. =alt>        }   
  36. ="">  
  37. =alt>    }   
  38. ="">}   
  39. =alt>  

JAVA中文站社区门户}j9g5Jzg 

m'}8N$?U0HT(~)A V9w e$l|3ND j s#c+`JAVA中文站社区门户h.K"zF;E S)J


{_0t Z2hes A4JR xml 代码

    =dp-xml>
  1. =alt>=tag><=tag-name>extension =attribute>po==attribute-value>"org.eclipse.wst.common.project.facet.core.facets"=tag>>  
  2. ="">  =tag><=tag-name>category =attribute>id==attribute-value>"{}"=tag>>  
  3. =alt>    =tag><=tag-name>label=tag>>{}=tag>=tag-name>label=tag>>  
  4. ="">    =tag><=tag-name>description=tag>>{}=tag>=tag-name>description=tag>> (optional)   
  5. =alt>  =tag>=tag-name>category=tag>>  
  6. ="">  =tag><=tag-name>project-facet=tag>>  
  7. =alt>    =tag><=tag-name>category=tag>>{}=tag>=tag-name>category=tag>> (optional)   
  8. ="">  =tag>=tag-name>project-facet=tag>>  
  9. =alt>=tag>=tag-name>extension=tag>>  

JAVA中文站社区门户!V5X;iKuJAVA中文站社区门户S1a|-~J%I
设置如下:
lu:ue+EsJAVA中文站社区门户$z4T#r HQ2W&w

xml 代码

    =dp-xml>
  1. =alt>=tag><=tag-name>extension =attribute>po==attribute-value>"org.eclipse.wst.common.project.facet.core.facets"=tag>>  
  2. ="">  
  3. =alt>  =tag><=tag-name>category =attribute>id==attribute-value>"formgen.category"=tag>>  
  4. ="">    =tag><=tag-name>label=tag>>FormGen=tag>=tag-name>label=tag>>  
  5. =alt>    =tag><=tag-name>description=tag>>Enables generation of HTML forms based on XML definition files.=tag>=tag-name>description=tag>>  
  6. ="">  =tag>=tag-name>category=tag>>  
  7. =alt>  
  8. ="">  =tag><=tag-name>project-facet =attribute>id==attribute-value>"formgen.core"=tag>>  
  9. =alt>    =tag><=tag-name>category=tag>>formgen.category=tag>=tag-name>category=tag>>  
  10. ="">  =tag>=tag-name>project-facet=tag>>  
  11. =alt>  
  12. ="">  =tag><=tag-name>project-facet =attribute>id==attribute-value>"formgen.ext"=tag>>  
  13. =alt>    =tag><=tag-name>category=tag>>formgen.category=tag>=tag-name>category=tag>>  
  14. ="">  =tag>=tag-name>project-facet=tag>>  
  15. =alt>  
  16. ="">=tag>=tag-name>extension=tag>>  

JAVA中文站社区门户n#PG:r]Y
Z;Jh#lxwg bc/d效果如图JAVA中文站社区门户/x0g-}2\b@%Nm

2]9_| T B@O8J =magplus title=点击查看原始大小图片 style="WIDTH: 762px; HEIGHT: 460px" height=422 alt="" src="http://www.crazycoder.cn/WebFiles/200812/2d3f52e2-1bbb-40c3-b840-f7b828355179.g" width=700 _counted="und">

p;D \}R&eM.y


TAG: Eclipse 实战 Facets Projects WTP
="xspace-totlerecord">2="xspace-totlepages">1/2="xspace-current">12>

相关文章

读者评论

  • 共0条 分0页

发表评论

  • 昵称:
  • 内容: