【问题标题】:Pax Exam 4 and multiple Maven repositories not workingPax Exam 4 和多个 Maven 存储库无法正常工作
【发布时间】:2015-03-22 11:08:41
【问题描述】:

我正在尝试运行一个非常基本的 Pax Exam 4 单元测试,但它需要访问多个 Maven 存储库(不是 Maven Central)。代码如下:

@RunWith(PaxExam.class)
public class ExamTest {
    @Inject
    private BundleContext bundleContext;

    @Configuration
    public Option[] config() {        
        return options(
            repositories(
                repository("http://maven.wso2.org/nexus/content/groups/wso2-public").id("wso2"),
                repository("http://nexus.codehaus.org/snapshots").id("nexus.public.repo").allowSnapshots(),
            ),
            mavenBundle("commons-httpclient.wso2", "commons-httpclient").version("3.1.0.wso2v2"),
            mavenBundle("org.codehaus.woodstox", "stax2-api").version("3.0.1-SNAPSHOT"),

            cleanCaches(),
            junitBundles()
        );
    }

    @Test
    public void testInjection() {
        Assert.assertNotNull(bundleContext);
        Bundle[] bundles = bundleContext.getBundles();
        for (Bundle bundle : bundles) {
            System.out.println(bundle.getSymbolicName() + ", state = " + bundle.getState());
        }
    }
}

实际测试是微不足道的,但只是为了测试目的,所以不要介意(回购和库也只是为了测试目的而选择的)。问题是上述方法不起作用,Pax Exam 抱怨存储库 URL 无效。运行此测试时的输出如下:

[main] ERROR org.ops4j.pax.url.mvn.internal.AetherBasedResolver - invalid repository URLs
java.net.MalformedURLException: no protocol: +http://nexus.codehaus.org/snapshots/
    at java.net.URL.<init>(URL.java:585)
    at java.net.URL.<init>(URL.java:482)
    at java.net.URL.<init>(URL.java:431)
    at org.ops4j.pax.url.mvn.internal.config.MavenRepositoryURL.<init>(MavenRepositoryURL.java:191)
    at org.ops4j.pax.url.mvn.internal.config.MavenConfigurationImpl.getRepositories(MavenConfigurationImpl.java:303)
    at org.ops4j.pax.url.mvn.internal.AetherBasedResolver.selectRepositories(AetherBasedResolver.java:254)
    ...

如您所见,由于某种原因,“+”被添加到第二个 URL 前,这会导致 MavenConfigurationImpl 中出现异常。奇怪的是,当我调试代码时,第一个 URL 也有一个“+”,但是那个被 Pax 代码剥离了。但是,第二个不会被剥离,然后在将字符串传递给 MavenRepositoryURL 构造函数时导致 MalformedURLException:

if (repositoriesProp != null && repositoriesProp.trim().length() > 0) {
    String[] repositories = repositoriesProp.split(REPOSITORIES_SEPARATOR);
    for (String repositoryURL : repositories) {
        repositoriesProperty.add(new MavenRepositoryURL(repositoryURL.trim()));
    }
}

现在这对我来说似乎是一个错误,但我真的不敢相信这样一个基本选项(能够处理多个 Maven 存储库)不起作用,所以我可能做错了什么。那么,我的问题是:如何让 Pax Exam 从多个 Maven 存储库下载 maven 包?

另外:如果您只添加 1 个存储库,则一切正常,而您是否使用 repositories() 方法并不重要。如果像这样多次使用 repository() 方法,结果是一样的:

@Configuration
    public Option[] config() {        
        return options(
            repository("http://maven.wso2.org/nexus/content/groups/wso2-public").id("wso2"),
            repository("http://nexus.codehaus.org/snapshots").id("nexus.public.repo").allowSnapshots(),         
            mavenBundle("commons-httpclient.wso2", "commons-httpclient").version("3.1.0.wso2v2"),
            mavenBundle("org.codehaus.woodstox", "stax2-api").version("3.0.1-SNAPSHOT"),

            cleanCaches(),
            junitBundles()
        );
    }

在 POM 的 sn-p 下方显示了我正在使用的依赖项(和版本):

<dependencies>        
    <dependency>
        <groupId>org.osgi</groupId>
        <artifactId>org.osgi.core</artifactId>
        <version>4.3.1</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>1.7.10</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.ops4j.pax.exam</groupId>
        <artifactId>pax-exam-container-native</artifactId>
        <version>4.4.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.ops4j.pax.url</groupId>
        <artifactId>pax-url-aether</artifactId>
        <version>2.3.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.ops4j.pax.url</groupId>
        <artifactId>pax-url-link</artifactId>
        <version>2.3.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.ops4j.pax.url</groupId>
        <artifactId>pax-url-classpath</artifactId>
        <version>2.3.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.ops4j.pax.exam</groupId>
        <artifactId>pax-exam-junit4</artifactId>
        <version>4.4.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.geronimo.specs</groupId>
        <artifactId>geronimo-atinject_1.0_spec</artifactId>
        <version>1.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.ops4j.pax.exam</groupId>
        <artifactId>pax-exam-link-assembly</artifactId>
        <version>4.4.0</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.eclipse.tycho</groupId>
        <artifactId>org.eclipse.osgi</artifactId>
        <version>3.7.0.v20110613</version>
        <scope>test</scope>
    </dependency> 
</dependencies>

【问题讨论】:

    标签: maven osgi integration-testing pax-exam


    【解决方案1】:

    这个问题还有另一种解决方案。除了让 Pax 直接与 Maven 存储库交互之外,您还可以设置构建和单元测试,以便 Maven 构建获取工件。在大多数情况下,在单元测试中配置的存储库无论如何也会在 POM 中配置(例如,因为在编译期间也需要工件),因此这也将消除重复配置。

    这里描述了解决方案:

    http://veithen.github.io/alta/examples/pax-exam.html

    【讨论】:

      【解决方案2】:

      这只是一个错误。

      repository() 选项的 Pax 考试 regression tests 目前不使用多个存储库,显然没有其他人这样做过。 (事实上​​,处理多个外部存储库的首选方式是使用设置为镜像的存储库管理器,这就解释了为什么此功能可能根本不那么基本。)

      Pax URL mvn: protocol handler 的文档对系统属性 org.ops4j.pax.url.mvn.repositories 的语法有点过于含糊。它提到了一个前导加号,表示除了来自 Maven settings.xml 的存储库之外,还应使用给定的存储库。

      Pax Exam 目前为每个存储库添加一个加号,但 Pax URL 预计整个列表最多加一个加号。

      作为一种解决方法,您可以将存储库选项替换为系统属性选项,如下所示:

      systemProperty("org.ops4j.pax.url.mvn.repositories").value("+repo1,repo2")
      

      注意:“repo1”和“repo2”是存储库的实际 URL。

      【讨论】:

      • 谢谢,成功了(但应该删除“存储库”末尾的点)!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-23
      • 2015-05-22
      • 1970-01-01
      相关资源
      最近更新 更多