来自pom.xml的依赖项在Tycho项目中未被Eclipse考虑



我用eclipse-plugin包创建了一个Tycho项目。该项目包括一些通过pom.xml指定的依赖项

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

该项目仅由默认包中的一个类组成,该类使用org.testng.annotations中的注释来测试在编译过程中是否包含依赖项。

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

您可以避免将项目构建分为两部分的问题:

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

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

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

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

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

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

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

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

没有那么难,有一个由Peter编写的"bnd"工具Kriens可以把你的罐子变成捆。还有一个Eclipse RCP提供的便利工具,可以生成P2站点(尽管是以一种繁琐而痛苦的方式)。两种工具都假设您的罐子/包位于本地文件夹中,这意味着您必须手动下载。您可以使用Maven将其自动化但是Maven计算依赖关系树,这与OSGi方式(这是问题编号#3)。让我们详细说明一下多一点。

它允许您定义一个pom打包项目,该项目将解析所有maven依赖项,将所有非OSGi依赖项转换为bundle,并从中生成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>

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

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

运行mvn eclipse:eclipse

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

最新更新