我们有一个Eclipse插件,我们使用Maven和第cho构建。现在但是,我们仍然通过一堆手动提供所有项目依赖项添加了JAR文件,而不是Maven。这是由于以下原因:(1(该依赖关系无法通过标准 Eclipse 更新站点获得(至少不在当前版本中(,(2( 依赖项不作为捆绑包提供。
这些依赖项中最大的部分是Selenium库(API,远程,特定于浏览器的库及其传递依赖项,例如 Guava 等(
我浪费了几个小时,试图在我们的Maven构建过程中拉取这些依赖项。在这个SO问题之后,我尝试了p2-maven-plugin
,创建了一个更新站点以及我添加到我的 Eclipse 目标平台的依赖项。然而在运行时,跨不同 JAR 引用的类不能加载(我假设,根据我非常有限的OSGi知识,因为一些MANIFEST.MF
文件中缺少必要的信息(。下面是一个示例的问题,当尝试创建一个RemoteWebDriver
时,它使用 DesiredCapabilities
类(两个类在不同的捆绑包中(:
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-plugin
与 copy-dependencies
一起使用,我们在 Maven initialize
阶段执行它以拉取所有必要的依赖项(与我最初的感觉相反,这可以与使用 eclipse-plugin
打包和"清单优先"方法的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
(。
[编辑] 关于上面最后一句话,重新审视这个答案:版本号可以通过以下选项方便地剥离:<stripVersion>true</stripVersion>
。这意味着,上述库将重命名为 commons-io.jar
,因此当版本号更改时无需更新路径。
另一种可能性:
一些jar文件可能被破坏了(如果你使用的是Eclipse,它是常见的休眠-commons-annotations-4.0.1.Final.jar;无效的LOC头(错误的签名(?(。要检查这种可能性,请尝试手动打开罐子以查看是否可以。
我还用 Maven 和第谷构建了一个 Eclipse 插件。我有同样的问题:捆绑org.eclipse.team.svn.core
和org.eclipse.team.svn.ui
无法通过标准 Eclipse 更新站点获得。
也许你可以试试这个来解决这种问题:
- 在">依赖项">中,找到">自动管理依赖关系。
- 使用添加所需的插件...
- 选择"分析代码"并将依赖项添加到清单。MF 通过: 导入包
- 单击">添加依赖项",以便在附近的"导入的包"框中找到所需的包。
然后,您可以运行 Maven 构建。