第谷项目中的 Eclipse 不考虑 pom.xml 中的依赖项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12476451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 19:00:15  来源:igfitidea点击:

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

eclipsemaventycho

提问by SpaceTrucker

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

我创建了一个带有eclipse-plugin包装的 Tycho 项目。该项目包括一些通过 pom.xml 指定的依赖项。相关的 pom 部分是:

<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.annotationsto test that during compilation the dependency is included.

该项目仅包含默认包中的一个类,该类使用注释 fromorg.testng.annotations来测试在编译期间是否包含依赖项。

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.testngcan'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?

如果我使用 Maven 3.0.4 在命令行上构建项目,一切正常。在 Eclipse Juno 中导入项目后,出现多个错误。最重要的一个在清单中,它指出com.springsource.org.testng无法解析包。类中还有一个编译错误,因为注解的导入是不可能的。该项目已配置 Maven Nature。我是否遗漏了一些东西,以便 Eclipse Juno 也会考虑 pom 的依赖关系?

采纳答案by oberlies

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-featureand an eclipse-repositorymodule 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.
  • 首先,将 POM 依赖项聚合到 p2 存储库中。为此,您需要一个eclipse-feature和一个eclipse-repository模块,以及一个单独的父 POM,列出 POM 依赖项并配置pomDependencies=consider.
  • 在第二个构建中,将第一个构建中的 p2 存储库添加到目标平台,例如通过jar:file:指向本地 Maven 存储库中构建结果的URL。

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.

然后,您还可以在 Eclipse 中配置您的目标平台以包含第一次构建中的 p2 存储库(这取决于您当前的配置方式)。如果您使用所谓的目标定义文件,您将在 Tycho 和 Eclipse 之间获得最佳一致性,您可以将其用作 Eclipse 和 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.

我知道所有这些设置都需要付出很大的努力,但是 AFAIK 没有更好的解决方案可以完全有效。

回答by berezovskyi

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):

解决 maven-RCP 问题之间存在的所有问题的最优雅的解决方案是使用 p2-maven-plugin. 以下是这些问题的简要总结(从上面的链接中截取):

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.

为了向 Eclipse RCP 项目添加第三方依赖项,该依赖项必须驻留在 P2 更新站点中。

Eclipse(和其他提供者)提供了一组公共更新站点,但显然并非所有流行和公开可用的依赖项都在那里(这是问题编号 #1)。

由于 Eclipse RCP 是一个 OSGi 环境,以便向 p2 更新站点添加依赖项,因此依赖项必须是一个 OSGi 包(即问题编号 #2)。

所以,现在让我们总结一下:我们所有的工件都必须是 OSGi 包,但它们并不总是包,它们必须位于 P2 站点中,但我们没有那个站点。那我们如何进行呢?

这并不难,有一个由 Peter Kriens 编写的“bnd”工具,可以将您的 jars 转换为 bundle。Eclipse RCP 还提供了一个方便的工具,可以生成 P2 站点(尽管以繁琐和痛苦的方式)。这两种工具都假定您的所有 jars/bundle 都位于本地文件夹中 - 这意味着您必须手动下载它们。您可以使用 Maven 使其自动化,但是 Maven 计算依赖树的方式存在显着差异,这与 OSGi 方式并不总是兼容(这是问题编号 #3)。让我们再详细说明一下。

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.

它允许您定义一个 pom 打包项目,该项目将解决所有 maven 依赖项,将所有非 OSGi 依赖项转换为包并从中生成 p2 站点。

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

下面是完整的最小 pom 文件,包括对 slf4j-log4j12 的依赖(隐式依赖于 slf4j 和 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.

PS 我通常不会发布旧问题和已回答问题的答案,但就我而言,以一种干净优雅的方式解决这个问题花了很长时间,所以我决定写下它。此外,该解决方案出现在 2013 年末。

回答by WeMakeSoftware

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

从命令行导航到 pom.xml 所在的文件夹。

Run mvn eclipse:eclipse.

运行mvn eclipse:eclipse

This should build a valid eclipse project.

这应该构建一个有效的 eclipse 项目。