Tycho

那年仲夏 提交于 2019-11-28 20:13:26

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

一、插件项目的构建

有了Tycho,构建一个Eclipse插件工程变的非常简单:

新建一个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 生成的文件内容:

<?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插件:

<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,就可以构建一个插件项目了! 运行下面的命令:

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

二、Feature项目的构建

Feautre项目的构建也很简单:

创建一个简单的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 三、p2 repository构建 创建一个普通项目(General):me.dollyn.tycho.example.repository,创建下面的pom:

<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>

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

四、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>

五、 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!