我正在尝试运行一个非常基本的 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 片段下方:
<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>