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

浪尽此生 提交于 2019-11-27 21:31:39

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.

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!