Tycho - 用Maven Build Eclipse插件

Tycho是一个Maven插件,旨在简化使用Maven构建Eclipse插件,OSGI Bundle等项目。html

1、插件项目的构建

有了Tycho,构建一个Eclipse插件工程变的很是简单:apache

新建一个Eclipse插件项目me.dollyn.tycho.example.plugin,这里利用了Eclipse New Plugin Project Wizard中的Hello, World Command模板 新建一个pom.xml文件,Tycho提供了一个命令,能够方便地为一个插件项目生成pom文件: mvn org.eclipse.tycho:tycho-pomgenerator-plugin:generate-poms -DgroupId=me.dollyn.tycho.example 生成的文件内容:eclipse

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <modelVersion>4.0.0</modelVersion>
 <groupId>me.dollyn.tycho.example</groupId>
 <artifactId>me.dollyn.tycho.example.plugin</artifactId>
  <version>1.0.0-SNAPSHOT</version>
  <packaging>eclipse-plugin</packaging>
</project>

这只是基本信息,还须要配置tycho插件:maven

<properties>
   <tycho-version>0.16.0</tycho-version>
</properties>
<repositories>
  <repository>
    <id>indigo</id>
    <layout>p2</layout>
    <url>http://download.eclipse.org/releases/indigo</url>
  </repository>
</repositories>
<build>
  <plugins>
    <plugin>
      <groupId>org.eclipse.tycho</groupId>
      <artifactId>tycho-maven-plugin</artifactId>
      <version>${tycho-version}</version>
      <extensions>true</extensions>
    </plugin>
   </plugins>
</build>

是的,就是这么简单的一个pom,就能够构建一个插件项目了! 运行下面的命令:ui

mvn clean install 第一次运行,可能须要下载的东西比较多,之后就行了。运行完成后,能够看到在target目录下生成的构建产物,其中包括插件jar包:me.dollyn.tycho.example.plugin–1.0.0-SNAPSHOT.jarthis

2、Feature项目的构建

Feautre项目的构建也很简单:url

建立一个简单的Feature项目,包含上面的插件 同上,用下面的命令自动生成一个pom文件: mvn org.eclipse.tycho:tycho-pomgenerator-plugin:generate-poms -DgroupId=me.dollyn.tycho.example 而后添加上tycho插件:插件

<properties>
  <tycho-version>0.16.0</tycho-version>
</properties>

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

<build>
  <plugins>
    <plugin>
      <groupId>org.eclipse.tycho</groupId>
      <artifactId>tycho-maven-plugin</artifactId>
      <version>${tycho-version}</version>
      <extensions>true</extensions>
    </plugin>
   </plugins>
</build>

运行mvn clean install 运行成功后,在target目录下,会生成对应feature的jar包 me.dollyn.tycho.example.feature–1.0.0-SNAPSHOT 3、p2 repository构建 建立一个普通项目(General):me.dollyn.tycho.example.repository,建立下面的pom:code

<groupId>me.dollyn.tycho.example</groupId>
<artifactId>me.dollyn.tycho.example.repository</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>eclipse-repository</packaging>
<properties>
  <tycho-version>0.16.0</tycho-version>
</properties>
<repositories>
  <repository>
    <id>indigo</id>
    <layout>p2</layout>
    <url>http://download.eclipse.org/releases/indigo</url>
  </repository>
</repositories>
<build>
  <plugins>
    <plugin>
      <groupId>org.eclipse.tycho</groupId>
      <artifactId>tycho-maven-plugin</artifactId>
      <version>${tycho-version}</version>
      <extensions>true</extensions>
    </plugin>
   </plugins>
</build>

构建repository还须要一个category.xml,在repository项目的根目录: <site> <feature url="features/me.dollyn.tycho.example.feature_1.0.0.qualifier.jar" id="me.dollyn.tycho.example.feature" version="1.0.0.qualifier"> <category name="CATE"/> </feature> <category-def name="CATE" label="CATE"/> </site>xml

执行mvn clean install,能够看到一个完整的repository就生成了。

4、Parent Pom 能够看到,前面的pom.xml中,大部份内容都是重复的,这个问题能够经过maven中的parent pom来解决,在eclipse中新建一个普通项目(General),创建pom.xml文件以下:

<modelVersion>4.0.0</modelVersion>
<groupId>me.dollyn.tycho.example</groupId>
<artifactId>me.dollyn.tycho.example.parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
    <module>../me.dollyn.tycho.example.feature</module>
<module>../me.dollyn.tycho.example.plugin</module>
<module>../me.dollyn.tycho.example.rcp</module>
</modules>
<properties>
  <tycho-version>0.16.0</tycho-version>
</properties>
<repositories>
  <repository>
    <id>indigo</id>
    <layout>p2</layout>
    <url>http://download.eclipse.org/releases/indigo</url>
  </repository>
</repositories>
<build>
  <plugins>
    <plugin>
      <groupId>org.eclipse.tycho</groupId>
      <artifactId>tycho-maven-plugin</artifactId>
      <version>${tycho-version}</version>
      <extensions>true</extensions>
    </plugin>
   </plugins>
</build>

同时,前面提到的几个pom就能够简化了,好比plugin项目的pom就能够简化成:

<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
<parent>
  <relativePath>../me.dollyn.tycho.example.parent/pom.xml</relativePath>
  <groupId>me.dollyn.tycho.example</groupId>
  <artifactId>me.dollyn.tycho.example.parent</artifactId>
  <version>1.0.0-SNAPSHOT</version>
</parent> 
<groupId>me.dollyn.tycho.example</groupId>
<artifactId>me.dollyn.tycho.example.feature</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>eclipse-feature</packaging>
</project>

5、 product的构建 Product的构建稍微复杂

首先,和PDE的product文件不同,你须要建立一个独立的项目,而后把product文件放到这个单独的项目中。 若是product是基于features的,那么文件中不能存在<plugins></plugins>这一段;反之,若是是基于plugin的,则不能包含<features>这一段。 product文件中,须要手动指定start level: 代码以下:

<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" />
   <!-- Disable update manager. It seems as if this could be achieved by the first line, but in 
       fact the second line sets reconcile to false (see org.eclipse.equinox.p2.publisher.eclipse.ConfigCUsAction#publishBundleCUs) -->
   <property name="org.eclipse.update.reconcile" value="false" />
   <plugin id="org.eclipse.update.configurator" autoStart="true" startLevel="4"/>
</configurations>

而后用下的pom.xml文件:

<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <relativePath>../me.dollyn.tycho.example.parent/pom.xml</relativePath>
    <groupId>me.dollyn.tycho.example</groupId>
    <artifactId>me.dollyn.tycho.example.parent</artifactId>
    <version>1.0.0-SNAPSHOT</version>
  </parent>
  <groupId>me.dollyn.tycho.example</groupId>
  <artifactId>me.dollyn.tycho.example.product</artifactId>
  <version>1.0.0-SNAPSHOT</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>
                    <!-- install the product for all configured os/ws/arch environments 
                        using p2 director -->
                    <id>materialize-products</id>
                    <goals>
                        <goal>materialize-products</goal>
                    </goals>
                </execution>
                <execution>
                    <!-- (optional) create product zips (one per os/ws/arch) -->
                    <id>archive-products</id>
                    <goals>
                        <goal>archive-products</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
    </build>
</project>

按照上面的步骤,这时候build一下parent这个project便可。

参考资料: http://eclipse.org/tycho/ http://wiki.eclipse.org/Tycho/Reference_Card http://www.vogella.com/articles/EclipseTycho/article.html

相关文章
相关标签/搜索