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 PDE build system used by Eclipse. The Eclipse platform has moved to building with Tycho and Maven 3.

  1. Change into the com.packtpub.e4.clock.ui project created in Chapter 2, Creating Views with SWT.

    Tip

    The project can also be downloaded from the site's GitHub repository at https://github.com/alblue/com.packtpub.e4/

  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 xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                         http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <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>

    Note

    The version has to match the one in the plugin.xml, with .qualifier replaced with -SNAPSHOT, and 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 will be displayed: Unknown packaging: eclipse-plugin. To fix this, add Tycho as a build plugin:
    <build>
      <plugins>
        <plugin>
          <groupId>org.eclipse.tycho</groupId>
          <artifactId>tycho-maven-plugin</artifactId> <version>0.25.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 Neon (or Mars, Luna, and so on) release repository:
    <repositories>
      <repository>
        <id>neon</id>
        <layout>p2</layout>
        <url>http://download.eclipse.org/releases/neon/</url>
      </repository>
      <!-- repository>
        <id>mars</id>
        <layout>p2</layout>
        <url>http://download.eclipse.org/releases/mars/</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 to build the plug-in.

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 autogenerated by tools) rather than typed in by hand.

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

[ERROR] Failed to execute goal
org.eclipse.tycho:tycho-packaging-plugin:0.25.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 the MANIFEST.MF file. Without this, the build will fail with a different error:

[ERROR] Failed to execute goal
 org.eclipse.tycho:tycho-packaging-plugin:0.25.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 with <extensions>true</extensions>:

<plugin>
  <groupId>org.eclipse.tycho</groupId>
  <artifactId>tycho-maven-plugin</artifactId>
  <version>0.25.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. This will be shown in the next section when parent projects are covered.

Finally, an Eclipse repository was added to the pom.xml file 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, these can be extracted to a settings.xml file or to a target platform definition. This allows the same project to be built against different versions of Eclipse in 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, which allows the plug-in's dependencies to be varied over time without mutating the pom.xml file.

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

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