Maven和Tycho

点进来读这篇文章的朋友,我假定你已经知道maven的基本知识,也知道Eclipse,知道Eclipse是基于OSGi,初步了解Eclipse插件开发或者说OSGi模块化开发。如果对这些知识不了解,甚至完全没有听过,那请绕开本文。

Maven简单来说是Java世界的一种新型的build工具,比ant的最大好处是依赖的管理,以及配置文件的可读性,可复用性,可扩展性。Maven的配置文件称为POM,即Project Object Model。在Maven中,每一个插件或者模块都由groupId,artifactId,version唯一标示。还有两个可选的标示元素,一个是packaging,默认支持的选项有pom,jar,maven-plugin,ejb,war,ear,rar,par等,maven会根据packaging设置的不同为模块执行不同的目标(goal);另一个是classifier,一般用不上。

最简单的pom

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.app</groupId>
  <artifactId>my-app</artifactId>
  <version>1</version>
</project>

关于Maven的废话不多说,直接开始Tycho。如果说Maven的出现是一群Java程序员受不了繁琐的插件依赖管理,受不了冗长的ant build.xml文件而创造出来的,那Tycho则是一群Eclipse、OSGi插件开发人员受不了重复地配置类似的Maven pom.xml而创造出来的。Tycho大大简化了Eclipse、OSGi插件中的pom.xml,它实际上是一系列专用于build Eclipse插件和OSGi模块的maven插件的集合。

熟悉Eclipse插件和OSGi模块开发的程序员都知道,他们有自己的一套metadata用来描述自己的依赖,自己的各项配置。

比如MANIFEST.MF文件,就包含了模块名称,版本,依赖等丰富的信息

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Dialog Editor
Bundle-SymbolicName: com.company.app; singleton:=true
Bundle-Version: 1.0.0.qualifier
Bundle-Vendor: Company
Require-Bundle: org.eclipse.ui,
 org.eclipse.graphiti,
 org.eclipse.graphiti.ui,
 org.eclipse.core.runtime;bundle-version="3.8.0",
 javax.inject;bundle-version="1.0.0",
 org.eclipse.e4.core.di;bundle-version="1.1.0",
 org.eclipse.e4.ui.workbench;bundle-version="0.10.2",
 org.eclipse.e4.ui.di;bundle-version="0.10.1",
 org.eclipse.e4.ui.services;bundle-version="0.10.1",
 org.eclipse.core.resources;bundle-version="3.8.0"
Bundle-RequiredExecutionEnvironment: JavaSE-1.6

还有build.properties,定义了所有需要在runtime用到的非代码文件(比如图片,配置文件等)

source.. = src/
output.. = bin/
bin.includes = plugin.xml,\
               META-INF/,\
               .,\
               icons/,\
               MyModel.e4xmi


以及product文件

<?xml version="1.0" encoding="UTF-8"?>
<?pde version="3.5"?>

<product name="e4 SDK" uid="org.eclipse.e4.workbench.sdk" id="org.eclipse.e4.ui.examples.legacy.workbench.product" application="org.eclipse.ui.ide.workbench" version="0.9.0.@qualifier@" useFeatures="true" includeLaunchers="true">

   <aboutInfo>
      <image path="/org.eclipse.e4.ui.examples.legacy.workbench/eclipse_lg.gif"/>
   </aboutInfo>

   <configIni use="default">
   </configIni>

   <launcherArgs>
      <programArgs>--launcher.XXMaxPermSize 256m</programArgs>
      <vmArgs>-Xms256m -Xmx512m</vmArgs>
      <vmArgsMac>-Xdock:icon=../Resources/Eclipse.icns -XstartOnFirstThread -Dorg.eclipse.swt.internal.carbon.smallFonts</vmArgsMac>
   </launcherArgs>

   <windowImages i16="/org.eclipse.e4.ui.examples.legacy.workbench/eclipse.gif" i32="/org.eclipse.e4.ui.examples.legacy.workbench/eclipse32.gif" i48="/org.eclipse.e4.ui.examples.legacy.workbench/eclipse48.gif"/>

   <splash
      location="org.eclipse.e4.ui.examples.legacy.workbench" />
   <launcher name="eclipse">
      <solaris/>
      <win useIco="false">
         <bmp/>
      </win>
   </launcher>

   <vm>
   </vm>

   <plugins>
   </plugins>

   <features>
      <feature id="org.eclipse.sdk"/>
      <feature id="org.eclipse.equinox.p2.user.ui"/>
      <feature id="org.eclipse.equinox.p2.user.ui.source"/>
      <feature id="org.eclipse.emf.sdk"/>
      <feature id="org.eclipse.wst.xml_ui.feature"/>
      <feature id="org.eclipse.gef"/>
      <feature id="org.eclipse.rcp.configuration"/>
      <feature id="org.eclipse.e4.sdk.runtime.feature"/>
      <feature id="org.eclipse.e4.sdk.source.feature"/>
      <feature id="org.eclipse.releng.tools"/>
   </features>

   <configurations>
      <plugin id="org.eclipse.core.runtime" autoStart="true" startLevel="4" />
      <plugin id="org.eclipse.equinox.common" autoStart="true" startLevel="2" />
      <plugin id="org.eclipse.equinox.ds" autoStart="true" startLevel="2" />
      <plugin id="org.eclipse.equinox.p2.reconciler.dropins" autoStart="true" startLevel="4" />
      <plugin id="org.eclipse.equinox.simpleconfigurator" autoStart="true" startLevel="1" />
      <plugin id="org.eclipse.update.configurator" autoStart="true" startLevel="3" />
      <property name="eclipse.buildId" value="@qualifier@" />
   </configurations>

</product>



Tycho牛逼之处就在于它可以读懂这些metadata,这样程序员就不需要在pom文件里面重新定义一遍。不止如此,为了避免程序员乌龙地在MANIFEST文件和pom文件里面定义的依赖出现冲突,Tycho甚至规定所有的依赖必须定义于MANIFEST文件中(不然没法编译),出现在pom文件中的依赖一律直接被忽略。

为了优化对Eclipse插件和OSGi模块的build,Tycho提供了专门的packaging,用来处理专门的模块生命周期,最典型的比如eclipse-repository,配合tycho-p2-director-plugin,直接就可以读懂product文件,build整个Eclipse RCP的product。


启用Tycho很简单,把下面的properties和build标签加入到parent pom文件即可,其中0.15.0是截止2012.8.26 Tycho最新的版本。

 <properties>
      <tycho-version>0.15.0</tycho-version>
   </properties>

   <build>
      <plugins>
         <plugin>
            <groupId>org.eclipse.tycho</groupId>
            <artifactId>tycho-maven-plugin</artifactId>
            <version>${tycho-version}</version>
            <extensions>true</extensions>
         </plugin>
      </plugins>
   </build>
下面的repository标签用来设定一个Eclipse Indigo (视需要改变)的官方p2仓库,从这个仓库可以获取Tycho以及Eclipse插件或者OSGi模块化开发所需的大量编译好的模块

<repository>
      <id>eclipse-indigo</id>
      <layout>p2</layout>
      <url>http://download.eclipse.org/releases/indigo</url>
   </repository>

因为子pom可以从父pom继承到插件的配置信息,所以一般推荐在parent pom中定义插件配置,另外子pom也有重写(override)配置的权利。

比如我们来定义target-platform-configuration插件(用来build最后的product)的配置,告诉Tycho的这个maven插件我想要分别对windows,linux,macos三种操作系统build 专门的product,这样Tycho在build product的时候就会根据不同的os,选用不同的环境包并入最后的product中。

<plugin>
        <groupId>org.eclipse.tycho</groupId>
        <artifactId>target-platform-configuration</artifactId>
        <version>${tycho-version}</version>
        <configuration>
          <environments>
            <environment>
              <os>win32</os>
              <ws>win32</ws>
              <arch>x86</arch>
            </environment>
            <environment>
              <os>linux</os>
              <ws>gtk</ws>
              <arch>x86_64</arch>
            </environment>
            <environment>
              <os>macosx</os>
              <ws>cocoa</ws>
              <arch>x86_64</arch>
            </environment>
          </environments>
        </configuration>
      </plugin>



接下来介绍build Eclipse插件、OSGi模块的各种专门的packaging

1、普通的Eclipse插件、OSGi模块

<groupId>Some-Group-Id</groupId>
  <artifactId>Bundle-SymbolicName</artifactId>
  <version>Bundle-Version</version>
  <packaging>eclipse-plugin</packaging>

2、feature模块(定义feature,feature是插件的集合,我们在给Eclipse安装插件的时候往往安装的是feature,而不是安装一个个的plugin jar)
 <groupId>Some-Group-Id</groupId>
  <artifactId>FeatureId</artifactId>
  <version>FeatureVersion</version>
  <packaging>eclipse-feature</packaging>

3、p2仓库(为了发布,以及未来更新的各种方便,一般建议创建专门的一个模块定义仓库)

<groupId>Some-Group-Id</groupId>
  <artifactId>RepositoryName</artifactId>
  <version>Version</version>
  <packaging>eclipse-repository</packaging> 

4、product产品

<groupId>Some-Group-Id</groupId>
  <artifactId>RepositoryName</artifactId>
  <version>Version</version>
  <packaging>eclipse-repository</packaging>
  <build>
    <plugins>
      <plugin>
        <groupId>org.eclipse.tycho</groupId>
        <artifactId>tycho-p2-director-plugin</artifactId>
        <version>${tycho-version}</version>
        <executions>
          <execution>
            <id>materialize-products</id>
            <goals>
              <goal>materialize-products</goal>
            </goals>
          </execution>
          <execution>
            <id>archive-products</id>
              <goals>
                <goal>archive-products</goal>
              </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build> 

5、测试模块

把测试模块单独build是Tycho的一个特色,也是OSGi模块化开发的特色。很多人习惯把源代码放到main/src底下,把测试代码放到同一个项目的test文件夹底下,而在OSGi中,测试代码应该写在单独的模块里。这样最大的好处是执行的时候测试模块只有在测试阶段才执行,在打包阶段不执行,就不会被打进最后的产品包里。

只要把packaging设置成下面的 eclipse-test-plugin,Tycho就知道这是一个测试模块,只有在测试阶段才build它。

<groupId>Some-Group-Id</groupId>
  <artifactId>Bundle-SymbolicName</artifactId>
  <version>Bundle-Version</version>
  <packaging>eclipse-test-plugin</packaging> 



最后放几个官方的链接:

http://maven.apache.org/guides/introduction/introduction-to-the-pom.html

http://www.eclipse.org/tycho/

http://wiki.eclipse.org/Tycho/Reference_Card




你可能感兴趣的:(eclipse,maven,eclipse插件,Build,osgi,plugins)