Maven + Tycho,添加了Maven依赖项

qqi*_*ihq 7 java eclipse dependencies tycho maven

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

这些依赖项中最大的部分是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
Run Code Online (Sandbox Code Playgroud)

使用时,还有什么我需要照顾的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>
Run Code Online (Sandbox Code Playgroud)

qqi*_*ihq 13

无法得到它的工作,所以现在我们正在使用的maven-dependency-plugincopy-dependencies,这是我们在Maven的执行initialize阶段把所有必要的依赖关系(与我最初的感觉,这可以通过结合pom.xml使用的eclipse-plugin包装和"清单第一个"方法".相关部分如下所示:

<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>
Run Code Online (Sandbox Code Playgroud)

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

只有一个小问题:Bundle-ClassPathMANIFEST.MF更新Maven依赖项(例如,commons-io-2.4.jar变为commons-io-2.5.jar)时,如果JAR文件的名称发生更改,则需要手动更新.

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