Sunday, January 6, 2013

Tycho build 3: Creating a global build project

During the first tycho tutorial we created a pom file to store our build instructions. Some of them will repeat for all the other things we will build later on. Therefore we will refactor our first project to extract common settings to a global pom file.

Actually tycho already did something very similar for us. Open up com.codeandme.tycho.plugin/pom.xml and look at the Effective POM tab. Our pom file is augmented with a lot of additional settings. So pom files support a cascaded approach.

Tycho Tutorials

For a list of all tycho related tutorials see Tycho Tutorials Overview

Source code for this tutorial is available on github as a single zip archive, as a Team Project Set or you can browse the files online.

Step 1: Create a generic build project

Create a new General/Project named com.codeandme.tycho.releng and convert it to a maven project (select Configure/Convert to Maven Project from the context menu). use the same Group ID as before, set the Version to 1.0.0-SNAPSHOT and the Packaging to pom. These steps will be pretty much the same for all the projects we will build with tycho.

Step 2: Refactor pom files

Move the properties, repositories and build section from our com.codeandme.tycho.plugin/pom.xml to the new one. (Remember the source code formatter works on xml files too). Of course our plug-in is unhappy now as information is missing in its pom file. Therefore we need to connect those poms somehow.

Step 3: Adding modules to poms

Open the Overview tab of com.codeandme.tycho.releng/pom.xml and Add... a Module. Select the com.codeandme.tycho.plugin.

Do not forget to check Update POM parent section in selected projects! This will tell the module where to get its additional information from.


After refactoring your poms should look like this:

com.codeandme.tycho.releng/pom.xml
<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>
 <groupId>tycho_example</groupId>
 <artifactId>com.codeandme.tycho.releng</artifactId>
 <version>1.0.0-SNAPSHOT</version>
 <packaging>pom</packaging>

 <properties>
  <tycho.version>0.23.0</tycho.version>

  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 </properties>

 <repositories>
  <!-- add Mars repository to resolve dependencies -->
  <repository>
   <id>Mars</id>
   <layout>p2</layout>
   <url>http://download.eclipse.org/releases/mars/</url>
  </repository>
 </repositories>

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

   <plugin>
    <groupId>org.eclipse.tycho</groupId>
    <artifactId>target-platform-configuration</artifactId>
    <version>${tycho.version}</version>
    <configuration>
     <environments>
      <environment>
       <os>win32</os>
       <ws>win32</ws>
       <arch>x86</arch>
      </environment>
     </environments>
    </configuration>
   </plugin>
  </plugins>
 </build>
 <modules>
  <module>../com.codeandme.tycho.plugin</module>
 </modules>
</project>
There is a new module section telling maven which modules to build when this project is built. As maven does not know anything about an eclipse workspace you cannot use variables like ${workspace_loc} here. Remember there will not be anything like a workspace when you trigger a build from command line anyway.

com.codeandme.tycho.plugin/pom.xml
<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>
 <artifactId>com.codeandme.tycho.plugin</artifactId>
 <packaging>eclipse-plugin</packaging>

 <parent>
  <groupId>tycho_example</groupId>
  <artifactId>com.codeandme.tycho.releng</artifactId>
  <version>1.0.0-SNAPSHOT</version>
  <relativePath>../com.codeandme.tycho.releng</relativePath>
 </parent>
</project>
The parent section tells maven to integrate the parent pom when building this project.

Now we can separate global settings from project specific ones and are ready to add additional projects to our build.

Verify that your setup is correct by triggering a maven build on com.codeandme.tycho.releng. The goals are clean install. You can still build individual plug-ins by triggering a maven run on those projects.

Step 4: Fix build warnings

Our build log reveals two different warnings we have to fix.

[WARNING] No explicit target runtime environment configuration. Build is platform dependent.
...
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
First we configure an environment to build for. Open your master pom file and add the following code to the plugins section:
   <plugin>
    <groupId>org.eclipse.tycho</groupId>
    <artifactId>target-platform-configuration</artifactId>
    <version>${tycho.version}</version>
    <configuration>
     <environments>
      <environment>
       <os>win32</os>
       <ws>win32</ws>
       <arch>x86</arch>
      </environment>
     </environments>
    </configuration>
   </plugin>
Of course this is only valid if you build for windows 32 bit. If you need to build for another platform you need to adjust os, ws and arch parameters accordingly. Remember that the build target does not depend on the platform you are developing on. I am writing this tutorial on a linux 64 bit machine and can still build for windows 32 bit.

The second warning is even easier to fix. Add a property defining the source encoding:
 <properties>
  ...
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 </properties>

2 comments:

  1. Yaghtzee mother fuckers..Stand down NOW

    ReplyDelete
  2. Thanks for this tutorial, it's interesting.

    ReplyDelete