Dependencies from pom.xml not considered by Eclipse in Tycho Project
Asked Answered
H

3

12

I created a Tycho project with an eclipse-plugin packaging. The project includes some dependencies that are specified via pom.xml. The relevant pom sections are:

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <tycho.version>0.15.0</tycho.version>
</properties>
<build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
        <plugin>
            <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>win32</os>
                        <ws>win32</ws>
                        <arch>x86</arch>
                    </environment>
                    <environment>
                        <os>linux</os>
                        <ws>gtk</ws>
                        <arch>x86_64</arch>
                    </environment>
                    <environment>
                        <os>macosx</os>
                        <ws>cocoa</ws>
                        <arch>x86_64</arch>
                    </environment>
                </environments>
            </configuration>
        </plugin>
    </plugins>
</build>
<repositories>
    <repository>
        <id>juno</id>
        <layout>p2</layout>
        <url>http://download.eclipse.org/releases/juno</url>
    </repository>
    <repository>
        <id>com.springsource.repository.bundles.release</id>
        <name>SpringSource Enterprise Bundle Repository - SpringSource Bundle Releases</name>
        <url>http://repository.springsource.com/maven/bundles/release</url>
    </repository>
    <repository>
        <id>com.springsource.repository.bundles.external</id>
        <name>SpringSource Enterprise Bundle Repository - External Bundle Releases</name>
        <url>http://repository.springsource.com/maven/bundles/external</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>com.springsource.org.testng</artifactId>
        <version>6.4.0</version>
    </dependency>
    <dependency>
        <groupId>com.google.guice</groupId>
        <artifactId>com.springsource.com.google.inject</artifactId>
        <version>2.0.0</version>
    </dependency>
    <dependency>
        <groupId>org.aopalliance</groupId>
        <artifactId>com.springsource.org.aopalliance</artifactId>
        <version>1.0.0</version>
    </dependency>
</dependencies>

And the Manifest is:

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Plugin-project-pure
Bundle-SymbolicName: plugin-project-pure
Bundle-Version: 1.0.0.qualifier
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Require-Bundle: org.eclipse.equinox.app,
 org.eclipse.uml2.uml;bundle-version="4.0.0",
 org.eclipse.uml2.uml.resources;bundle-version="4.0.0",
 org.junit;bundle-version="4.10.0",
 com.springsource.org.testng;bundle-version="[6.4.0,6.4.0]"

The project only consists of a class in the default package that uses an annotation from org.testng.annotations to test that during compilation the dependency is included.

If I'm building the project on the command line with Maven 3.0.4 everything works fine. After importing the project in Eclipse Juno, I get multiple errors. The most important one is in the manifest and it states that the bundle com.springsource.org.testng can't be resolved. There is also a compile error in the class, because the import of the annotation is not possible. The project has the Maven Nature configured. Am I missing something so that Eclipse Juno will also consider the dependencies of the pom?

Hern answered 18/9, 2012 at 11:50 Comment(0)
A
11

You can circumvent this problem splitting your project build into two parts:

  • First, aggregate your POM dependencies into a p2 repository. You'll need an eclipse-feature and an eclipse-repository module for this, plus a separate parent POM that lists the POM dependencies and configures pomDependencies=consider.
  • In the second build, add the p2 repository from the first build to the target platform, e.g. via a jar:file: URL pointing to the build result in your local Maven repository.

Then, you can also configure your target platform in Eclipse to include the p2 repository from the first build (which depends on how you currently configure it). You'll get the best consistency between Tycho and Eclipse if you use a so-called target definition file, which you can use both as target platform in Eclipse and in Tycho.

I am aware that all this is quite a bit of effort to set up, but AFAIK there are no better solutions that fully work.

Atlantic answered 18/9, 2012 at 17:44 Comment(4)
Isn't it possible to have another parent, which includes the first build and the second build as modules? The reactor build order should build the eclipse-repository first and you won't need a second build.Hern
This is another option: to build the eclipse-repository in the same reactor, and only use it for the target platform in Eclipse. This is the simplest approach if you anyway don't plan to share a target definition file between Eclipse and Tycho.Atlantic
I'm accepting this answer, because this also addresses the problem with the errors in the manifest when working in eclipse. But for the moment I'm going with Funtiks answer.Hern
I feel like these answers don't follow in the spirit of Eclipse builds. Shouldn't there be an external build tool that builds the Eclipse project using maven?Herniotomy
S
10

The most elegant solution to all problems that exist between maven-RCP problems is to use the p2-maven-plugin. Here is the brief summary of those problems (cuts from the link above):

In order to add a third-party dependency to an Eclipse RCP project the dependency has to reside in a P2 update site.

Eclipse (and other providers) provide a set of public update sites, but obviously not all popular and publicly available dependencies are there (that is the problem number #1).

Since Eclipse RCP is an OSGi environment in order to add a dependency to a p2 update site the depenedncy has to be an OSGi bundle (that is the problem number #2).

So, let’s sum up for now: all our artifacts have to be OSGi bundles, but they are not always bundles and they have to be located in a P2 site, but we do not have that site. How do we proceed then?

It is not that difficult, there is a ‘bnd’ tool written by Peter Kriens that can transform your jars into bundles. There is also a convenience tool provided by Eclipse RCP that can generate a P2 site (in a cumbersome and painful way though). Both tools assume that all your jars/bundles are located in a local folder - which means that you have to download them by-hand. You could use Maven to automate it a bit, but there is a significant difference in the way how Maven calculates a dependency tree and this is not alwyas compatible with the OSGi way (that is the problem number #3). Let us elaborate on it a bit more.

It allows you to define a pom-packaged project that will resolve all maven dependencies, convert all non-OSGi ones to bundles and generate a p2 site from them.

Below is the full minimal pom file including the dependency on slf4j-log4j12 (which implicitly depends on both slf4j and log4j v1.2):

<?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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>me.berezovskiy.project</groupId>
  <artifactId>p2</artifactId>
  <packaging>pom</packaging>
  <version>1.0.0</version>
  <build>
    <plugins>
      <plugin>
        <groupId>org.reficio</groupId>
        <artifactId>p2-maven-plugin</artifactId>
        <version>1.1.1-SNAPSHOT</version>
        <executions>
          <execution>
            <id>default-cli</id>
            <configuration>
              <artifacts>
                <artifact>
                  <id>org.slf4j:slf4j-log4j12:1.7.7</id>
                </artifact>
              </artifacts>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>jetty-maven-plugin</artifactId>
        <version>8.1.12.v20130726</version>
        <configuration>
          <scanIntervalSeconds>10</scanIntervalSeconds>
          <webAppSourceDirectory>${basedir}/target/repository/</webAppSourceDirectory>
          <webApp>
            <contextPath>/site</contextPath>
          </webApp>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <pluginRepositories>
    <pluginRepository>
      <id>reficio</id>
      <url>http://repo.reficio.org/maven/</url>
    </pluginRepository>
  </pluginRepositories>
</project>

P.S. I usually do not post answers to old and answered questions, but in my case it took so long to resolve this issue in a clean and elegant way that I decided to write about it. Additionally, the solution has appeared in late 2013.

Sivie answered 16/7, 2014 at 9:47 Comment(2)
If someone is going to use this plugin: the newer version is: 1.1.0Penurious
@jachinte my example was already using 1.1.1-SNAPSHOT. Perhaps, you meant 1.2.0-SNAPSHOT?Sivie
I
6

from the command line navigate to the folder where the pom.xml is located.

Run mvn eclipse:eclipse.

This should build a valid eclipse project.

Icao answered 18/9, 2012 at 13:16 Comment(8)
This works nearly for all the problems I had. The only problem left is that in the manifest the bundle com.springsource.org.testng is still marked as not resolvable, even if it is now listed under Referenced Libraries in the Project Explorer.Hern
try refreshing everything with F5 and cleaning up all projects in the eclipse. Project -> Clean...Icao
The problem is still there. Even after removing and reimporting the project as an already existing project.Hern
try to simply delete the MANIFEST. No file - no problem :)Icao
Have already tried that at least a dozen times. But apperently there are some people, who think that this whole osgi stuff is so important. ;)Hern
yeah, I'm an Eclipse RCP developer as well. All this OSGI stuff gets really difficult to configure. How did you install com.springsource.org.testng bundle? Is it installed or just referenced?Icao
I'm not an RCP developer. Just have to build a small tool that analyses some UML Diagrams. I don't know what you exactly mean with installed or referenced. The bundle is included as a dependency in the maven build by tycho. It is downloaded from SpringSource Enterprise Bundle repository. So it's not installed. Tycho builds a target platform during the build that includes the bundle.Hern
I guess you have to install the bundle, so that Eclipse can also see it. Maven is a standalone building tool. It does not know anything about Eclipse. Same stands for Eclipse. By default it does not know anything about maven dependencies. It's often very painful to integrate these two together.Icao

© 2022 - 2024 — McMap. All rights reserved.