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

相關文章
相關標籤/搜索