Time for action – building with Tycho

Now that Maven is installed, it's time to build a plug-in with Tycho. Tycho is a set of plug-ins for Maven 3 that emulates the older PDE build used by Eclipse. The Eclipse platform has moved to building with Tycho and Maven 3 under the name Common Build Infrastructure (http://wiki.eclipse.org/CBI).

  1. Change into the com.packtpub.e4.clock.ui project created in Chapter 2, Creating Views with SWT. (If you don't have this project, see the book's GitHub repository for sample code.)
  2. Create a file called pom.xml at the root of the project, with the following empty contents:
    <?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>
    </project>

    This can be copied from the pom.xml file generated in the previous section, since every pom.xml has this same signature.

  3. Give the project a unique groupId, artifactId, and version, by placing the following after the modelVersion tag:
    <groupId>com.packtpub.e4</groupId>
    <artifactId>com.packtpub.e4.clock.ui</artifactId>
    <version>1.0.0-SNAPSHOT</version>

    The version here has to match the one in the plugin.xml file, with .qualifier replaced with -SNAPSHOT.

    The artifactId has to be the name of the fully qualified plug-in name (the Bundle-SymbolicName in the MANIFEST.MF file)

  4. Define the packaging type to be eclipse-plugin:
    <packaging>eclipse-plugin</packaging>
  5. If the build is now run with mvn package, an error message, Unknown packaging: eclipse-plugin, will be displayed. To fix this, add Tycho as a build plugin:
    <build>
      <plugins>
        <plugin>
          <groupId>org.eclipse.tycho</groupId>
          <artifactId>tycho-maven-plugin</artifactId>
          <version>0.18.0</version>
          <extensions>true</extensions>
        </plugin>
      </plugins>
    </build>
  6. Run the build again. This time it will complain of an "unsatisfiable" build error:
    [ERROR] Internal error: java.lang.RuntimeException:
      "No solution found because the problem is unsatisfiable.": 
      ["Unable to satisfy dependency from 
       com.packtpub.e4.clock.ui 1.0.0.qualifier to bundle 
       org.eclipse.ui 0.0.0."] -> [Help 1]
  7. Add the juno (or kepler, luna) release repository:
    <repositories>
      <repository>
        <id>juno</id>
        <layout>p2</layout>
        <url>http://download.eclipse.org/releases/juno</url>
      </repository>
      <!-- repository>
        <id>kepler</id>
        <layout>p2</layout>
        <url>http://download.eclipse.org/releases/kepler</url>
      </repository -->
      <!-- repository>
        <id>luna</id>
        <layout>p2</layout>
        <url>http://download.eclipse.org/releases/luna</url>
      </repository -->
    </repositories>
  8. Now run mvn clean package and the plug-in should be built.

What just happened?

All Maven projects have a pom.xml file that controls their build process, and Eclipse plug-ins are no different. The header for a pom.xml file doesn't change, and so generally this is copied from an existing one (or auto-generated by tools) rather than being typed in by hand.

Each Maven pom.xml file needs to have a unique groupId / artifactId / version. For eclipse-plugin projects, the name of the artifactId must be the same as the Bundle-SymbolicName in MANIFEST.MF, otherwise an error is thrown:

[ERROR] Failed to execute goal
  org.eclipse.tycho:tycho-packaging-plugin:0.18.0:validate-id 
  (default-validate-id) on project com.packtpub.e4.clock.uix: 
  The Maven artifactId (currently: "com.packtpub.e4.clock.uix")
  must be the same as the bundle symbolic name
  (currently: "com.packtpub.e4.clock.ui") -> [Help 1]

The same is true for the version in the pom.xml file, which must match the version in MANIFEST.MF. Without this, the build will fail with a different error:

[ERROR] Failed to execute goal
  org.eclipse.tycho:tycho-packaging-plugin:0.18.0:validate-version
  (default-validate-version) on project com.packtpub.e4.clock.ui: 
  Unqualified OSGi version 1.0.0.qualifier must match unqualified 
  Maven version 1.0.1-SNAPSHOT for SNAPSHOT builds -> [Help 1]

Tycho knows how to build Eclipse plug-ins by setting the packaging type to eclipse-plugin. However, in order for Maven to know about the eclipse-plugin type, Tycho has to be defined as a Maven plugin for the build. Importantly, it needs to be defined as an extension, otherwise it doesn't contribute to the packaging type:

<plugin>
  <groupId>org.eclipse.tycho</groupId>
  <artifactId>tycho-maven-plugin</artifactId>
  <version>0.18.0</version>
  <extensions>true</extensions>
</plugin>

Although it's possible to hard-code the version of the Tycho plug-in like this, it's conventional to replace it with a property instead. We will replace the version number with a property when we create the parent project later.

Finally, an Eclipse repository was added to pom.xml so that the build could resolve any additional plug-ins and features. This needs to be defined as a p2 repository type, to distinguish it from the default type, which stores Maven artifacts.

Note that it is best practice to not put repositories in pom.xml files in general. Instead, this can be extracted to a settings.xml file. This allows the same project to be built against different versions of Eclipse in the future without changing the source, or to run against a closer mirror of the same. A settings file can be passed to Maven with mvn -s /path/to/settings.xml that allows the plug-in's dependencies to be varied over time without mutating the pom.xml file.

Have a go hero – using target platforms

Although it is possible to build an Eclipse-based application by pointing to a repository, this does not provide reproducibility of the build. For example, a build might be run against the Kepler release repository in July 2013 when Kepler is released as 4.3.0, and the same build can be run again in December 2013 when Kepler 4.3.1 is released. The results of these two builds will be different, even if no source code changes have occurred in the meantime.

A target platform can be defined in Eclipse by going to the Target Platform preferences page by navigating to Window | Preferences | Plug-in Development. Create a new target definition, consisting of the Base RCP, and then click on Share to allow the .target file to be saved on the filing system. A corresponding eclipse-target-definition packaging type can be used to define a GAV co-ordinate for the .target file, and when combined with the target-platform-configuration plug-in, it can be specified to build just against those components. See the Tycho documentation or the book's GitHub repository for more examples.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset