点进来读这篇文章的朋友,我假定你已经知道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>
熟悉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
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>
为了优化对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>
1、普通的Eclipse插件、OSGi模块
<groupId>Some-Group-Id</groupId> <artifactId>Bundle-SymbolicName</artifactId> <version>Bundle-Version</version> <packaging>eclipse-plugin</packaging>
<groupId>Some-Group-Id</groupId> <artifactId>FeatureId</artifactId> <version>FeatureVersion</version> <packaging>eclipse-feature</packaging>
<groupId>Some-Group-Id</groupId> <artifactId>RepositoryName</artifactId> <version>Version</version> <packaging>eclipse-repository</packaging>
<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>
把测试模块单独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