Maven + Tycho,添加 Maven 依赖

Maven + Tycho, adding Maven dependencies

我们有一个使用 Maven 和 Tycho 构建的 Eclipse 插件。现在 但是,我们仍然通过一堆手动提供所有项目依赖项 添加 JAR 文件而不是 Maven。这是由于以下原因: (1) 依赖项不能通过标准的 Eclipse 更新站点获得(至少 不在当前版本中),(2) 依赖项不可作为捆绑包使用。

这些依赖项的最大部分是 Selenium 库(API、Remote、 特定于浏览器的库及其传递依赖项,例如 Guava 等)

我已经浪费了几个小时,试图在我们的 Maven 构建过程中提取这些依赖项。 在 this SO question, I tried the p2-maven-plugin 之后,创建了一个更新 带有我添加到 Eclipse 目标平台的依赖项的站点。然而, 在运行时,classes,它们被不同的 JAR 引用 已加载(我假设,根据我非常有限的 OSGi 知识,因为一些 MANIFEST.MF 文件中缺少必要的信息)。这是一个例子 的问题,当试图创建一个 RemoteWebDriver,它使用 DesiredCapabilities class(两个 class 在不同的包中):

Exception in thread "Thread-8" java.lang.NoClassDefFoundError: org/openqa/selenium/remote/DesiredCapabilities
    at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:243)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:126)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:153)
    …
Caused by: java.lang.ClassNotFoundException: org.openqa.selenium.remote.DesiredCapabilities cannot be found by org.seleniumhq.selenium.remote-driver_2.45.0
    at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:439)
    at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:352)
    at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:344)
    at org.eclipse.osgi.internal.loader.ModuleClassLoader.loadClass(ModuleClassLoader.java:160)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 7 more

使用 p2-maven-plugin 时还有什么需要注意的吗? pom.xml 的相关部分如下所示:

<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.seleniumhq.selenium:selenium-remote-driver:2.45.0</id>
                    </artifact>
                </artifacts>
            </configuration>
        </execution>
    </executions>
</plugin>

无法让它工作,所以我们现在使用 maven-dependency-plugincopy-dependencies,我们在 Maven initialize 阶段执行以提取所有必要的依赖项(与我最初的感觉相反,这可以与使用 eclipse-plugin 包装和 "manifest first" 方法的 pom.xml 相结合)。相关部分如下所示:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.10</version>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>initialize</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <includeScope>runtime</includeScope>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

然后将 Maven 依赖项复制到 target/dependency

唯一的小问题:MANIFEST.MF 中的 Bundle-ClassPath 需要手动更新,以防在更新 Maven 依赖项时 JAR 文件的名称发生变化(例如 commons-io-2.4.jar 变为 commons-io-2.5.jar).

[edit] 重新审视关于上面最后一句话的答案:可以通过以下选项方便地删除版本号:<stripVersion>true</stripVersion>。这意味着,上述库将重命名为 commons-io.jar,因此当版本号更改时无需更新路径。

另一种可能性:

一些 jar 文件可能已损坏(如果您使用的是 Eclipse,这很常见 hibernate-commons-annotations-4.0.1.Final.jar; invalid LOC header (bad signature)?)。要检查这种可能性,请尝试手动打开罐子看看是否可以。

我还使用 Maven 和 Tycho 构建了一个 Eclipse 插件。我有同样的问题:捆绑包 org.eclipse.team.svn.coreorg.eclipse.team.svn.ui 无法通过标准 Eclipse 更新站点获得。

也许你可以试试这个来解决这类问题:

  1. Dependencies 中,找到方框 Automated Management of 依赖关系.
  2. 使用 Add...
  3. 添加想要的插件
  4. 选择分析代码并通过以下方式将依赖项添加到 MANIFEST.MF:Import-Package
  5. 单击添加依赖项,以便在附近的导入包框中找到所需的包。

然后您可以运行 Maven 构建。