`
dearwolf
  • 浏览: 339534 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

使用Ant,Maven构建Eclipse RCP Product

阅读更多
通常我们打包Product的时候,一般都是通过Product的export操作来进行的,但这样的做法,则限制了Continous Integration的自动化执行,那么,应该如何编写自动化构建脚本呢?

一、首先,让我们看一下Eclipse官方的解决方案:

http://help.eclipse.org/help32/index.jsp?topic=/org.eclipse.pde.doc.user/guide/tasks/pde_product_build.htm

从 文中我们可以看出,PDE已经为从product文件构建RCP应用做好了一切准备,我们所需要做的,就是搭建目录结构以及构建脚本。但是如果完全按照官 方方案去做的话,只会使得构建更加复杂,更加不可自动化,那么,让我们跟随一个简单的例子来看一下如何完成自动构建吧(在下使用的是 Eclipse3.2,pde的版本号为3.2.0.v20060603,使用其他版本的朋友请自行修改):

1。 使用Hello RCP模版构建一个RCP工程。命名为hellorcp,创建到G:\eclipse\workspace\hellorcp

在这一步,有一个很关键的问题需要注意,我们先来看一下上文中build directory的结构:

  1. buildDirectory/  
  2.     plugins/  
  3.         pluginToBuildA  
  4.         pluginToBuildB  
  5.             myProduct.product  
  6.         ...  
  7.     features/  
  8.         featureToBuild  

要想成功build工程的话,那么工程就必须要放到<builddirectory>/plugins/的目录结构下,不然pde是找不到工程的plugin id的!我努力了很长时间才无奈的认可了这个事实。

如果我们不想把工程放到一个很复杂的目录结构下,或者对于既有的工程不想做这样的改动,那么就只好用到ant来拷贝了。我的做法是,在工程目录下,创建名为dist\plugins\hellorcp的目录,待一切就绪后,使用ant来把整个工程拷贝到这个目录下。

2。准备脚本

在 工程下,新建buildConfiguration目录,并把<eclipse_install_dir>\plugins\ org.eclipse.pde.build_3.2.0.v20060603\templates\headless-build下的 build.properties文件拷贝到此目录下,并修改如下设置项:

1).product—— 官方的说法是:the location of your product configuration file in the form "/ <plugin or="" feature="" id="">/path/to/.product",而我们的buildConfiguration目录和 product文件同处于一个目录下,所以只需要写上product文件的名字即可,如hellorcp.product。
2).baseLocation——官方的说法是:the location of an eclipse install containing all the pre-built features and plug-ins that your product requires in features/ and plugins/ subdirectories. The RCP delta pack (it is available from the eclipse download page) is mandatory as it includes the org.eclipse.platform.launchers feature which contains the launchers and root files necessary for a product. 我们要做的就是把RCP delta pack安装到Eclipse目录下,然后把base location指向Eclipse根目录即可,这里需要绝对路径。
3).buildDirectory——官方的说法是:the directory the build will take place in. Set this to the full path of the build directory created previously. 这里因为最后的目录指向是ant拷贝完成以后的路径,所以应当是G:\eclipse\workspace\hellorcp\dist
4).configs—— list the configurations for which you want your product to be built. You can uncomment the configuration(s) provided (be careful of the line continuations).
5).archivePrefix—— the name of the directory of your product once installed on disk. 如hellorcp
以上为官方给出的配置项,而我们如果使用了1.5或1.6中的新功能的话,还需要到文件的最底部,找到javacSource和javacTarget两个选项,进行对应的修改。

3。ant脚本

在工程目录下新建一个ant脚本,把下面的代码粘贴进去,运行,就可以到dist/I.TestBuild下面找构建后的产品了。</plugin> </eclipse_install_dir></builddirectory>

 
  1. xml version="1.0"?>  
  2. <project name="project" default="rcpAutomatedBuild">  
  3.     <property name="eclipse.install" value="G:\eclipse">  
  4.     <!---->property>  
  5.     <property name="project.dir" value="G:\eclipse\workspace\hellorcp">  
  6.     <!---->property>  
  7.     <property name="dist.dir" value="${project.dir}\dist\plugins\hellorcp" />  
  8.     <target name="prepare">  
  9.         <mkdir dir="${dist.dir}" />  
  10.         <copy todir="${dist.dir}">  
  11.             <fileset dir="${project.dir}">  
  12.             <!---->fileset>  
  13.         <!---->copy>  
  14.     <!---->target>  
  15.     <target name="rcpAutomatedBuild" depends="prepare">  
  16.         <java classname="org.eclipse.core.launcher.Main" fork="true" failonerror="true">  
  17.             <arg value="-application" />  
  18.             <arg value="org.eclipse.ant.core.antRunner" />  
  19.             <arg value="-buildfile" />  
  20.             <arg value="${eclipse.install}\plugins\org.eclipse.pde.build_3.2.0.v20060603\scripts\productBuild\productBuild.xml" />  
  21.             <arg value="-Dbuilder=${dist.dir}\buildConfiguration" />  
  22.             <classpath>  
  23.                 <pathelement location="${eclipse.install}\startup.jar" />  
  24.             <!---->classpath>  
  25.         <!---->java>  
  26.     <!---->target>  
  27. <!---->project>  

构建后的产品是以zip形式存在的,我们还可以根据实际的需要,对build.properties进行更多的修改。

上面的脚本还有一些缺陷,比如绝对位置的使用,但这是无法避免的了.......

二、接下来是使用Maven的构建:

使用Maven的准备工作包括下载,配置环境变量,安装Maven plugin for Eclipse,并在Preferences中,Java/Build Path/Classpath Variables/下面配置M2_REPO变量,使其指向userhome\.m2\repository

关于Maven的入门知识在此略过,请自行阅读Maven官方站点的相关资料。

在创建工程时,请将src目录指向src/main/java,output目录指向target/classes

工 程创建完毕后,右击工程名,在弹出菜单中选中“Maven2“,然后选择”Enable“,则会生成对应的pom文件,我们可以自己设置对应的 groupid,artifactId,以及相应的dependencies,在这里,我们只需要为Eclipse插件依赖以外的第三方包设置 dependency就可以了。如果该包在官网上无法访问到,则可以通过运行

mvn install:install-file -Dfile=path-to-your-artifact-jar \
-DgroupId=your.groupId \
-DartifactId=your-artifactId \
-Dversion=version \
-Dpackaging=jar \
-DgeneratePom=true

来把第三方包安装到local repository里面去。

然 而,在plugin project运行时,除了Eclipse插件依赖以外的第三方包是需要位于plugin目录下的,否则在运行时无法找到对应路径,我们也可以看到,在 plugin.xml视图的runtime页面中,根本无法把plugin目录以外的jar文件加入到classpath中来。所以,我们还需要另外的 ant脚本来完成拷贝工作。脚本如下所示:(此处参考了江南白衣,有一点不同的是,此处把ant的classpath指向了local repository,这样发布的时候,就无需发布maven-artifact-ant.jar了)

 
  1. xml version="1.0" encoding="UTF-8"?>  
  2. <project name="build-base" default="copy" xmlns:artifact="urn:maven-artifact-ant">  
  3.     <typedef resource="org/apache/maven/artifact/ant/antlib.xml" uri="urn:maven-artifact-ant">  
  4.         <classpath>  
  5.             <pathelement location="${user.home}/.m2/repository/org/apache/maven/maven-artifact-ant/2.0.4/maven-artifact-ant-2.0.4.jar" />  
  6.         <!---->classpath>  
  7.     <!---->typedef>  
  8.     <artifact:pom id="maven.project" file="pom.xml" />  
  9.     <artifact:dependencies filesetId="dependency.fileset" useScope="runtime">  
  10.         <pom refid="maven.project"/>  
  11.     <!---->artifact:dependencies>  
  12.     <target name="copy">  
  13.         <copy todir="lib">  
  14.             <fileset refid="dependency.fileset" />  
  15.             <mapper type="flatten" />  
  16.         <!---->copy>  
  17.     <!---->target>  
  18. <!---->project>  


接下来的目录结构及脚本的准备工作和前面一样,除了两处需要修改:

1)buildConfiguration\build.properties, 这里的buildDirectory可以注释掉。
2)第3步中的ant脚本,因为我们这里需要用maven来构建,所以只需要执行prepare任务就可以了。

再有就是pom.xml文件中,要加入构建的任务:

 
  1. <build>  
  2.     <plugins>  
  3.       <plugin>  
  4.         <groupId>org.codehaus.mojo<!---->groupId>  
  5.         <artifactId>pde-maven-plugin<!---->artifactId>  
  6.         <extensions>true<!---->extensions>  
  7.         <configuration>  
  8.           <eclipseInstall>G:\eclipse<!---->eclipseInstall>  
  9.           <pdeProductFilename>hellorcp.product<!---->pdeProductFilename>  
  10.           <pdeBuildVersion>3.2.0.v20060603<!---->pdeBuildVersion>  
  11.         <!---->configuration>  
  12.       <!---->plugin>  
  13.     <!---->plugins>  
  14.   <!---->build>  


最后运行第3步中的ant脚本,然后到dist\plugins\hellorcp目录下,运行mvn install就可以了。构建后的文件可以在local repository中找到。

常见错误:

使用ant build的时候,出现plugin:xxx找不到的错误,请检查buidConfiguration\build.properties中buildDirectory的配置项
使用Maven build的时候,出现编译错误,请检查pom.xml中是否少了

 
  1. <packaging>zip</packaging>  

<packaging></packaging> 这一项
分享到:
评论
4 楼 hnlzwaq 2007-07-04  
     [java] BUILD FAILED
     [java] D:\Program Files\eclipse_rcp\plugins\org.eclipse.pde.build_3.3.0.v20
070612\scripts\productBuild\productBuild.xml:24: The following error occurred wh
ile executing this line:
     [java] D:\Program Files\eclipse_rcp\plugins\org.eclipse.pde.build_3.3.0.v20
070612\scripts\productBuild\productBuild.xml:51: Unable to find plug-in: org.ecl
ipse.equinox.launcher.*.*.*. Please check the error log for more details.

按照作者所述,出现上面的错误
引用
使用ant build的时候,出现plugin:xxx找不到的错误,请检查buidConfiguration\build.properties中buildDirectory的配置项

这个的配置也没错啊 ,但是老是出错,请求指点一下!
3 楼 dearwolf 2007-03-06  
确实非常的烦人,我也是用了好几天才搞清楚的,最开始的时候,还经历了把一个普通的maven project一步步改造成plugin project的过程,修改.classpath和.project文件等等,不是一般的头大...
2 楼 partech 2007-03-06  
共同学习,曾经考察过,觉得很烦人,回头再试一下.
1 楼 lyx_2709 2007-03-06  
学习了

相关推荐

Global site tag (gtag.js) - Google Analytics