Search code examples
mavenbuildeclipse-rcptycho

Maven/Tycho takes the wrong bundle-version


I'm trying to build my eclipse-plugin using tycho.

My package com.mycompany.math requires org.apache.commons.math-1.2.0, which is installed in my p2-repository. The dependency is defined in the MANIFEST.MF of org.mycompany.math:

Require-Bundle: org.apache.commons.math;bundle-version="1.2.0",

During my build, i get the error-message that the org.apache.commons.math-classes could not resolved. Before the build start's, maven/tycho downloaded the 2.1.0-version. So, my question is, why maven/tycho download's 2.1.0, when i defined in the MANIFEST.MF that i use 1.2.0.

You can see in my parent pom.xml that i defined three p2-repository's. The last one, contains my required 1.2.0-version.

My parent pom.xml:

<project...>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId>
<artifactId>com.mycompany.build</artifactId>
<version>3.1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Build</name>
<description>Parent POM for full builds</description>

<modules>
    <!-- my modules -->
</modules>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <tycho-version>0.16.0</tycho-version>
</properties>

<repositories>
    <!-- configure p2 repository to resolve against -->
    <repository>
        <id>juno</id>
        <layout>p2</layout>
        <url>http://download.eclipse.org/releases/juno/</url>
    </repository>
    <repository>
        <id>orbit</id>
        <layout>p2</layout>
<url>http://download.eclipse.org/tools/orbit/downloads/drops/S20121021123453/repository/</url>
    </repository>
    <repository> <-- CONTAINS ORG.APACHE.COMMONS.MATH-1.2.0 !
        <id>comp</id>
        <layout>p2</layout>
        <url>http:our-adress.com/p2/</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>
                <pomDependencies>consider</pomDependencies>
                <environments>
                    <environment>
                        <os>linux</os>
                        <ws>gtk</ws>
                        <arch>x86</arch>
                    </environment>
                    <environment>
                        <os>win32</os>
                        <ws>win32</ws>
                        <arch>x86</arch>
                    </environment>
                </environments>
            </configuration>
        </plugin>
    </plugins>
</build>


<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-math</artifactId>
            <version>1.2</version>
        </dependency>
    </dependencies>
</dependencyManagement>

And my com.company.math pom.xml

<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId>
<artifactId>com.mycompany.math</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>Math</name>
<packaging>eclipse-plugin</packaging>

<parent>
    <groupId>com.mycompany</groupId>
    <artifactId>com.mycompany.build</artifactId>
    <version>3.1.0-SNAPSHOT</version>
    <relativePath>../com.mycompany.build</relativePath>
</parent>


<dependencies>
    <dependency>
        <groupId>commons-math</groupId>
        <artifactId>commons-math</artifactId>
        <version>1.2</version>
    </dependency>
</dependencies>


Solution

  • The problem is that your Require-Bundle statement is too general: With Require-Bundle: org.apache.commons.math;bundle-version="1.2.0" you actually specify that you need the math bundle in version 1.2.0 or any later version.

    You should specify that you only want 1.2.0 or compatible versions. This can be done with Require-Bundle: org.apache.commons.math;bundle-version="[1.2.0,2.0.0)". This statement prevents that your bundle will be wired against the (apparently incompatible) 2.1 version of the math bundle at runtime (which is also important!), and it will probably also fix your build problem.

    Tycho may still resolve against a higher 1.x version of the math bundle for building, if such a version is present in the target platform (i.e. in your case any of the configured p2 repository or amongst the POM dependencies). If this is the case, but you want to enforce that the 1.2 version is used in the build, you need to control the content of your target platform. (The Maven <dependencyManagement> is not enough, because it doesn't have an effect on the p2 repositories you configured.) You can do this by specifying filters in Tycho's target platform configuration:

    <plugin>
       <groupId>org.eclipse.tycho</groupId>
       <artifactId>target-platform-configuration</artifactId>
       <version>${tycho-version}</version>
       <configuration>
          <filters>
             <filter>
                <type>eclipse-plugin</type>
                <id>org.apache.commons.math</id>
                <restrictTo>
                   <version>1.2.0</version>
                </restrictTo>
             </filter>
          </filters>
       </configuration>
    </plugin>