12

我创建了一个带有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>

清单是:

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]"

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

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

4

3 回答 3

11

您可以规避这个问题,将您的项目构建分为两部分:

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

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

我知道所有这一切都需要付出相当大的努力来设置,但是 AFAIK 没有更好的解决方案可以完全发挥作用。

于 2012-09-18T17:44:57.887 回答
10

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

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

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

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

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

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

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

下面是完整的最小 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>

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

于 2014-07-16T09:47:23.127 回答
6

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

运行mvn eclipse:eclipse

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

于 2012-09-18T13:16:48.633 回答